From a3c3c3aef0bc5f412472898e8ca56bda52f4d9ea Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Wed, 12 Jun 2019 22:15:35 +0200 Subject: [PATCH] fix(rds): fix unresolved endpoint socket address Convert port to a string token before using it in `socketAddress` otherwise the number token is converted to a string and cannot be resolved. Fixes #2711 --- packages/@aws-cdk/aws-rds/lib/endpoint.ts | 2 +- .../@aws-cdk/aws-rds/test/test.instance.ts | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-rds/lib/endpoint.ts b/packages/@aws-cdk/aws-rds/lib/endpoint.ts index 89f658b603236..4b66d44d034d0 100644 --- a/packages/@aws-cdk/aws-rds/lib/endpoint.ts +++ b/packages/@aws-cdk/aws-rds/lib/endpoint.ts @@ -25,7 +25,7 @@ export class Endpoint { this.hostname = address; this.port = port; - const portDesc = Token.isUnresolved(port) ? '{IndirectPort}' : port; + const portDesc = Token.isUnresolved(port) ? Token.asString(port) : port; this.socketAddress = `${address}:${portDesc}`; } } diff --git a/packages/@aws-cdk/aws-rds/test/test.instance.ts b/packages/@aws-cdk/aws-rds/test/test.instance.ts index 01e1a08b46610..7bcdabbc26165 100644 --- a/packages/@aws-cdk/aws-rds/test/test.instance.ts +++ b/packages/@aws-cdk/aws-rds/test/test.instance.ts @@ -375,6 +375,37 @@ export = { statistic: 'Average' }); + test.done(); + }, + + 'can resolve endpoint port and socket address'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + + // WHEN + const instance = new rds.DatabaseInstance(stack, 'Instance', { + engine: rds.DatabaseInstanceEngine.Mysql, + instanceClass: new ec2.InstanceTypePair(ec2.InstanceClass.Burstable2, ec2.InstanceSize.Small), + masterUsername: 'admin', + vpc + }); + + test.deepEqual(stack.resolve(instance.instanceEndpoint.port), { + 'Fn::GetAtt': ['InstanceC1063A87', 'Endpoint.Port'] + }); + + test.deepEqual(stack.resolve(instance.instanceEndpoint.socketAddress), { + 'Fn::Join': [ + '', + [ + { 'Fn::GetAtt': ['InstanceC1063A87', 'Endpoint.Address'] }, + ':', + { 'Fn::GetAtt': ['InstanceC1063A87', 'Endpoint.Port'] }, + ] + ] + }); + test.done(); } };