Skip to content

Commit

Permalink
fix(aws-rds): fromDatabaseInstanceAttributes incorrectly stringifies …
Browse files Browse the repository at this point in the history
…ports with tokens (#16286)

Closes #11813. Fixes `port` with tokens being incorrectly stringified in `fromDatabaseInstanceAttributes()`.

----


*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
comcalvi authored Aug 31, 2021
1 parent d210bb8 commit 41b831a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as kms from '@aws-cdk/aws-kms';
import * as logs from '@aws-cdk/aws-logs';
import * as s3 from '@aws-cdk/aws-s3';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { ArnComponents, Duration, FeatureFlags, IResource, Lazy, RemovalPolicy, Resource, Stack, Token } from '@aws-cdk/core';
import { ArnComponents, Duration, FeatureFlags, IResource, Lazy, RemovalPolicy, Resource, Stack, Token, Tokenization } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { DatabaseSecret } from './database-secret';
Expand Down Expand Up @@ -124,7 +124,7 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase
});
public readonly instanceIdentifier = attrs.instanceIdentifier;
public readonly dbInstanceEndpointAddress = attrs.instanceEndpointAddress;
public readonly dbInstanceEndpointPort = attrs.port.toString();
public readonly dbInstanceEndpointPort = Tokenization.stringifyNumber(attrs.port);
public readonly instanceEndpoint = new Endpoint(attrs.instanceEndpointAddress, attrs.port);
public readonly engine = attrs.engine;
protected enableIamAuthentication = true;
Expand Down
29 changes: 29 additions & 0 deletions packages/@aws-cdk/aws-rds/test/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,35 @@ describe('instance', () => {


});

test('can create a new database instance with fromDatabaseInstanceAttributes using a token for the port', () => {
// GIVEN
const databasePort = new cdk.CfnParameter(stack, 'DatabasePort', {
type: 'Number',
default: 5432,
}).valueAsNumber;

// WHEN
const instance = rds.DatabaseInstance.fromDatabaseInstanceAttributes(stack, 'DatabaseInstance', {
instanceIdentifier: '',
securityGroups: [],
instanceEndpointAddress: '',
port: databasePort,
});

new cdk.CfnOutput(stack, 'portOutput', {
exportName: 'databaseUrl',
value: `${instance.dbInstanceEndpointPort}`,
});

// THEN
expect(stack).toHaveOutput({
exportName: 'databaseUrl',
outputValue: {
Ref: 'DatabasePort',
},
});
});
});

test('create a read replica in the same region - with the subnet group name', () => {
Expand Down

0 comments on commit 41b831a

Please sign in to comment.