Skip to content

Commit

Permalink
fix(rds): unable to use tokens as port in DatabaseInstance (aws#17995)
Browse files Browse the repository at this point in the history
In `DatabaseInstance` the port number can be specified using data type number.
If a token value (e.g. CloudFormation Parameter) was used here, the token was not resolved correctly.

The conversion of property `port` has been corrected by using method `Tokenization.stringifyNumber()`.

Fixes aws#17948.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jumic authored and TikiTDO committed Feb 21, 2022
1 parent 6dc53c1 commit f82876f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ abstract class DatabaseInstanceNew extends DatabaseInstanceBase implements IData
performanceInsightsRetentionPeriod: enablePerformanceInsights
? (props.performanceInsightRetention || PerformanceInsightRetention.DEFAULT)
: undefined,
port: props.port?.toString(),
port: props.port !== undefined ? Tokenization.stringifyNumber(props.port) : undefined,
preferredBackupWindow: props.preferredBackupWindow,
preferredMaintenanceWindow: props.preferredMaintenanceWindow,
processorFeatures: props.processorFeatures && renderProcessorFeatures(props.processorFeatures),
Expand Down
37 changes: 37 additions & 0 deletions packages/@aws-cdk/aws-rds/test/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,43 @@ describe('instance', () => {
},
});
});

test('instance with port provided as a number', () => {
// WHEN
new rds.DatabaseInstance(stack, 'Database', {
engine: rds.DatabaseInstanceEngine.MYSQL,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
port: 3306,
});

// THEN
expect(stack).toHaveResourceLike('AWS::RDS::DBInstance', {
Port: '3306',
});
});

test('instance with port provided as a CloudFormation parameter', () => {
// GIVEN
const port = new cdk.CfnParameter(stack, 'Port', {
type: 'Number',
}).valueAsNumber;

// WHEN
new rds.DatabaseInstance(stack, 'Database', {
engine: rds.DatabaseInstanceEngine.MYSQL,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
port,
});

// THEN
expect(stack).toHaveResourceLike('AWS::RDS::DBInstance', {
Port: {
Ref: 'Port',
},
});
});
});

test.each([
Expand Down

0 comments on commit f82876f

Please sign in to comment.