Skip to content

Commit 1f24336

Browse files
robertdrix0rrr
authored andcommitted
feat(aws-rds): ability to add an existing security group to RDS cluster (#2021)
1 parent 5886bf6 commit 1f24336

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

packages/@aws-cdk/aws-rds/lib/cluster.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ export class DatabaseCluster extends cdk.Construct implements IDatabaseCluster {
151151
subnetIds: subnets.map(s => s.subnetId)
152152
});
153153

154-
const securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
154+
const securityGroup = props.instanceProps.securityGroup !== undefined ?
155+
props.instanceProps.securityGroup : new ec2.SecurityGroup(this, 'SecurityGroup', {
155156
description: 'RDS security group',
156157
vpc: props.instanceProps.vpc
157158
});

packages/@aws-cdk/aws-rds/lib/props.ts

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export interface InstanceProps {
3030
* Where to place the instances within the VPC
3131
*/
3232
vpcPlacement?: ec2.VpcPlacementStrategy;
33+
34+
/**
35+
* Security group. If not specified a new one will be created.
36+
*/
37+
securityGroup?: ec2.ISecurityGroup;
3338
}
3439

3540
/**

packages/@aws-cdk/aws-rds/test/test.cluster.ts

+37
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,43 @@ export = {
8989
test.done();
9090
},
9191

92+
'can create a cluster with imported vpc and security group'(test: Test) {
93+
// GIVEN
94+
const stack = testStack();
95+
const vpc = ec2.VpcNetwork.importFromContext(stack, 'VPC', {
96+
vpcId: "VPC12345"
97+
});
98+
const sg = ec2.SecurityGroup.import(stack, 'SG', {
99+
securityGroupId: "SecurityGroupId12345"
100+
});
101+
102+
// WHEN
103+
new DatabaseCluster(stack, 'Database', {
104+
engine: DatabaseClusterEngine.Aurora,
105+
instances: 1,
106+
masterUser: {
107+
username: 'admin',
108+
password: 'tooshort',
109+
},
110+
instanceProps: {
111+
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.Burstable2, ec2.InstanceSize.Small),
112+
vpc,
113+
securityGroup: sg
114+
}
115+
});
116+
117+
// THEN
118+
expect(stack).to(haveResource('AWS::RDS::DBCluster', {
119+
Engine: "aurora",
120+
DBSubnetGroupName: { Ref: "DatabaseSubnets56F17B9A" },
121+
MasterUsername: "admin",
122+
MasterUserPassword: "tooshort",
123+
VpcSecurityGroupIds: [ "SecurityGroupId12345" ]
124+
}));
125+
126+
test.done();
127+
},
128+
92129
'cluster with parameter group'(test: Test) {
93130
// GIVEN
94131
const stack = testStack();

0 commit comments

Comments
 (0)