Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rds): unable to use tokens as port in DatabaseInstance #17995

Merged
merged 3 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ? Tokenization.stringifyNumber(props.port) : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things:

  1. I think you want the condition to be props.port === undefined, as 0 is falsy in JavaScript.
  2. I believe you can use Token.toString(props.port) instead - it's more of the idiomatic way to do it (Tokenization is more of an internal class).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Good catch. 0 is a valid port number. I changed it.
  2. Method Token.toString() doesn't accept an attribute. So I can't use it. Should I try something else? Or is Tokenization.stringifyNumber(props.port) the only way to handle it?

preferredBackupWindow: props.preferredBackupWindow,
preferredMaintenanceWindow: props.preferredMaintenanceWindow,
processorFeatures: props.processorFeatures && renderProcessorFeatures(props.processorFeatures),
Expand Down
32 changes: 32 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,38 @@ describe('instance', () => {
},
});
});

test('instance with port', () => {
// 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 from token', () => {
// WHEN
new rds.DatabaseInstance(stack, 'Database', {
engine: rds.DatabaseInstanceEngine.MYSQL,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
port: cdk.Token.asNumber({ Ref: 'port' }),
});

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

test.each([
Expand Down