Skip to content

Commit

Permalink
feat(aws-rds): ability to add an existing security group to RDS clust…
Browse files Browse the repository at this point in the history
…er (#2021)
  • Loading branch information
robertd authored and rix0rrr committed Mar 15, 2019
1 parent 5886bf6 commit 1f24336
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-rds/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ export class DatabaseCluster extends cdk.Construct implements IDatabaseCluster {
subnetIds: subnets.map(s => s.subnetId)
});

const securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
const securityGroup = props.instanceProps.securityGroup !== undefined ?
props.instanceProps.securityGroup : new ec2.SecurityGroup(this, 'SecurityGroup', {
description: 'RDS security group',
vpc: props.instanceProps.vpc
});
Expand Down
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export interface InstanceProps {
* Where to place the instances within the VPC
*/
vpcPlacement?: ec2.VpcPlacementStrategy;

/**
* Security group. If not specified a new one will be created.
*/
securityGroup?: ec2.ISecurityGroup;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions packages/@aws-cdk/aws-rds/test/test.cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,43 @@ export = {
test.done();
},

'can create a cluster with imported vpc and security group'(test: Test) {
// GIVEN
const stack = testStack();
const vpc = ec2.VpcNetwork.importFromContext(stack, 'VPC', {
vpcId: "VPC12345"
});
const sg = ec2.SecurityGroup.import(stack, 'SG', {
securityGroupId: "SecurityGroupId12345"
});

// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.Aurora,
instances: 1,
masterUser: {
username: 'admin',
password: 'tooshort',
},
instanceProps: {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.Burstable2, ec2.InstanceSize.Small),
vpc,
securityGroup: sg
}
});

// THEN
expect(stack).to(haveResource('AWS::RDS::DBCluster', {
Engine: "aurora",
DBSubnetGroupName: { Ref: "DatabaseSubnets56F17B9A" },
MasterUsername: "admin",
MasterUserPassword: "tooshort",
VpcSecurityGroupIds: [ "SecurityGroupId12345" ]
}));

test.done();
},

'cluster with parameter group'(test: Test) {
// GIVEN
const stack = testStack();
Expand Down

0 comments on commit 1f24336

Please sign in to comment.