Skip to content

Commit

Permalink
feat(rds): rename DatabaseInstanceNewProps.vpcPlacement to vpcSubnets (
Browse files Browse the repository at this point in the history
…#10093)

Deprecate the `vpcPlacement` property in favor of `vpcSubnets` to be more
consistent with the Cluster naming (and general convention across the CDK).

fixes #9776


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
njlynch authored Sep 1, 2020
1 parent f2d77d1 commit ec423ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-rds/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Your cluster will be empty by default. To add a default database upon constructi
### Starting an instance database

To set up a instance database, define a `DatabaseInstance`. You must
always launch a database in a VPC. Use the `vpcPlacement` attribute to control whether
always launch a database in a VPC. Use the `vpcSubnets` attribute to control whether
your instances will be launched privately or publicly:

```ts
Expand All @@ -75,7 +75,7 @@ const instance = new rds.DatabaseInstance(this, 'Instance', {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
masterUsername: 'syscdk',
vpc,
vpcPlacement: {
vpcSubnets: {
subnetType: ec2.SubnetType.PRIVATE
}
});
Expand Down
17 changes: 14 additions & 3 deletions packages/@aws-cdk/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,18 @@ export interface DatabaseInstanceNewProps {
/**
* The type of subnets to add to the created DB subnet group.
*
* @deprecated use `vpcSubnets`
* @default - private subnets
*/
readonly vpcPlacement?: ec2.SubnetSelection;

/**
* The type of subnets to add to the created DB subnet group.
*
* @default - private subnets
*/
readonly vpcSubnets?: ec2.SubnetSelection;

/**
* The security groups to assign to the DB instance.
*
Expand Down Expand Up @@ -537,9 +545,12 @@ abstract class DatabaseInstanceNew extends DatabaseInstanceBase implements IData
super(scope, id);

this.vpc = props.vpc;
this.vpcPlacement = props.vpcPlacement;
if (props.vpcSubnets && props.vpcPlacement) {
throw new Error('Only one of `vpcSubnets` or `vpcPlacement` can be specified');
}
this.vpcPlacement = props.vpcSubnets ?? props.vpcPlacement;

const { subnetIds } = props.vpc.selectSubnets(props.vpcPlacement);
const { subnetIds } = props.vpc.selectSubnets(this.vpcPlacement);

const subnetGroup = new CfnDBSubnetGroup(this, 'SubnetGroup', {
dbSubnetGroupDescription: `Subnet group for ${this.node.id} database`,
Expand Down Expand Up @@ -611,7 +622,7 @@ abstract class DatabaseInstanceNew extends DatabaseInstanceBase implements IData
preferredBackupWindow: props.preferredBackupWindow,
preferredMaintenanceWindow: props.preferredMaintenanceWindow,
processorFeatures: props.processorFeatures && renderProcessorFeatures(props.processorFeatures),
publiclyAccessible: props.vpcPlacement && props.vpcPlacement.subnetType === ec2.SubnetType.PUBLIC,
publiclyAccessible: this.vpcPlacement && this.vpcPlacement.subnetType === ec2.SubnetType.PUBLIC,
storageType,
vpcSecurityGroups: securityGroups.map(s => s.securityGroupId),
maxAllocatedStorage: props.maxAllocatedStorage,
Expand Down
33 changes: 33 additions & 0 deletions packages/@aws-cdk/aws-rds/test/test.instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,39 @@ export = {
test.done();
},

'can specify subnet type'(test: Test) {
new rds.DatabaseInstance(stack, 'Instance', {
engine: rds.DatabaseInstanceEngine.mysql({
version: rds.MysqlEngineVersion.VER_8_0_19,
}),
masterUsername: 'syscdk',
vpc,
vpcPlacement: {
subnetType: ec2.SubnetType.PRIVATE,
},
});

expect(stack).to(haveResource('AWS::RDS::DBInstance', {
DBSubnetGroupName: {
Ref: 'InstanceSubnetGroupF2CBA54F',
},
PubliclyAccessible: false,
}));
expect(stack).to(haveResource('AWS::RDS::DBSubnetGroup', {
DBSubnetGroupDescription: 'Subnet group for Instance database',
SubnetIds: [
{
Ref: 'VPCPrivateSubnet1Subnet8BCA10E0',
},
{
Ref: 'VPCPrivateSubnet2SubnetCFCDAA7A',
},
],
}));

test.done();
},

'create an instance from snapshot'(test: Test) {
// WHEN
new rds.DatabaseInstanceFromSnapshot(stack, 'Instance', {
Expand Down

0 comments on commit ec423ef

Please sign in to comment.