Skip to content
This repository was archived by the owner on Jan 31, 2024. It is now read-only.

Commit 8892837

Browse files
authored
fix(rds): allowing a no name rds (#1094)
1 parent 9ff96dc commit 8892837

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed

src/base/ApplicationRDSCluster.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,25 @@ describe('ApplicationRDSCluster', () => {
4141
});
4242
expect(synthed).toMatchSnapshot();
4343
});
44+
45+
it('renders a RDS cluster without a name', () => {
46+
const synthed = Testing.synthScope((stack) => {
47+
new ApplicationRDSCluster(stack, 'testRDSCluster', {
48+
prefix: 'bowling-',
49+
vpcId: 'rug',
50+
subnetIds: ['0', '1'],
51+
useName: false,
52+
rdsConfig: {
53+
masterUsername: 'walter',
54+
masterPassword: 'bowling',
55+
databaseName: 'walter',
56+
engine: 'aurora-mysql',
57+
},
58+
tags: {
59+
whodis: 'walter',
60+
},
61+
});
62+
});
63+
expect(synthed).toMatchSnapshot();
64+
});
4465
});

src/base/ApplicationRDSCluster.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ export interface ApplicationRDSClusterProps extends TerraformMetaArguments {
3131
prefix: string;
3232
vpcId: string;
3333
subnetIds: string[];
34+
useName?: boolean; // When importing resources we need to tell terraform to not try and change the name of the resource.
3435
rdsConfig: ApplicationRDSClusterConfig;
3536
tags?: { [key: string]: string };
3637
}
3738

39+
const defaults = {
40+
useName: true,
41+
};
42+
3843
/**
3944
* Generates an RDS cluster
4045
*
@@ -54,6 +59,9 @@ export class ApplicationRDSCluster extends Construct {
5459
) {
5560
super(scope, name);
5661

62+
// Apply defaults
63+
config = { ...defaults, ...config };
64+
5765
const appVpc = new DataAwsVpc(this, `vpc`, {
5866
filter: [
5967
{
@@ -104,15 +112,17 @@ export class ApplicationRDSCluster extends Construct {
104112
});
105113

106114
const subnetGroup = new DbSubnetGroup(this, 'rds_subnet_group', {
107-
namePrefix: config.prefix.toLowerCase(),
115+
namePrefix: config.useName ? config.prefix.toLowerCase() : undefined,
108116
subnetIds: config.subnetIds,
109117
provider: config.provider,
110118
tags: config.tags,
111119
});
112120

113121
this.rds = new RdsCluster(this, 'rds_cluster', {
114122
...config.rdsConfig,
115-
clusterIdentifierPrefix: config.prefix.toLowerCase(),
123+
clusterIdentifierPrefix: config.useName
124+
? config.prefix.toLowerCase()
125+
: undefined,
116126
tags: config.tags,
117127
copyTagsToSnapshot: true, //Why would we ever want this to false??
118128
masterPassword:

src/base/__snapshots__/ApplicationRDSCluster.spec.ts.snap

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,121 @@ exports[`ApplicationRDSCluster renders a RDS cluster with a database URL 1`] = `
239239
}
240240
}"
241241
`;
242+
243+
exports[`ApplicationRDSCluster renders a RDS cluster without a name 1`] = `
244+
"{
245+
"data": {
246+
"aws_vpc": {
247+
"testRDSCluster_vpc_F47EEEFE": {
248+
"filter": [
249+
{
250+
"name": "vpc-id",
251+
"values": [
252+
"rug"
253+
]
254+
}
255+
]
256+
}
257+
}
258+
},
259+
"resource": {
260+
"aws_db_subnet_group": {
261+
"testRDSCluster_rds_subnet_group_88022457": {
262+
"subnet_ids": [
263+
"0",
264+
"1"
265+
],
266+
"tags": {
267+
"whodis": "walter"
268+
}
269+
}
270+
},
271+
"aws_rds_cluster": {
272+
"testRDSCluster_rds_cluster_B5FD08B5": {
273+
"copy_tags_to_snapshot": true,
274+
"database_name": "walter",
275+
"db_subnet_group_name": "\${aws_db_subnet_group.testRDSCluster_rds_subnet_group_88022457.name}",
276+
"engine": "aurora-mysql",
277+
"lifecycle": {
278+
"ignore_changes": [
279+
"master_username",
280+
"master_password"
281+
]
282+
},
283+
"master_password": "bowling",
284+
"master_username": "walter",
285+
"tags": {
286+
"whodis": "walter"
287+
},
288+
"vpc_security_group_ids": [
289+
"\${aws_security_group.testRDSCluster_rds_security_group_4A9D257E.id}"
290+
]
291+
}
292+
},
293+
"aws_secretsmanager_secret": {
294+
"testRDSCluster_rds_secret_A2014138": {
295+
"depends_on": [
296+
"aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5"
297+
],
298+
"description": "Secret For \${aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5.cluster_identifier}",
299+
"name": "bowling-/\${aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5.cluster_identifier}",
300+
"tags": {
301+
"whodis": "walter"
302+
}
303+
}
304+
},
305+
"aws_secretsmanager_secret_version": {
306+
"testRDSCluster_rds_secret_version_55C44893": {
307+
"depends_on": [
308+
"aws_secretsmanager_secret.testRDSCluster_rds_secret_A2014138"
309+
],
310+
"secret_id": "\${aws_secretsmanager_secret.testRDSCluster_rds_secret_A2014138.id}",
311+
"secret_string": "{\\"engine\\":\\"aurora-mysql\\",\\"host\\":\\"\${aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5.endpoint}\\",\\"username\\":\\"\${aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5.master_username}\\",\\"password\\":\\"\${aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5.master_password}\\",\\"dbname\\":\\"\${aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5.database_name}\\",\\"port\\":3306,\\"database_url\\":\\"mysql://\${aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5.master_username}:\${aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5.master_password}@\${aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5.endpoint}:3306/\${aws_rds_cluster.testRDSCluster_rds_cluster_B5FD08B5.database_name}\\"}"
312+
}
313+
},
314+
"aws_security_group": {
315+
"testRDSCluster_rds_security_group_4A9D257E": {
316+
"description": "Managed by Terraform",
317+
"egress": [
318+
{
319+
"cidr_blocks": [
320+
"0.0.0.0/0"
321+
],
322+
"description": "required",
323+
"from_port": 0,
324+
"ipv6_cidr_blocks": [
325+
],
326+
"prefix_list_ids": [
327+
],
328+
"protocol": "-1",
329+
"security_groups": [
330+
],
331+
"self": null,
332+
"to_port": 0
333+
}
334+
],
335+
"ingress": [
336+
{
337+
"cidr_blocks": [
338+
"\${data.aws_vpc.testRDSCluster_vpc_F47EEEFE.cidr_block}"
339+
],
340+
"description": null,
341+
"from_port": 3306,
342+
"ipv6_cidr_blocks": null,
343+
"prefix_list_ids": null,
344+
"protocol": "tcp",
345+
"security_groups": null,
346+
"self": null,
347+
"to_port": 3306
348+
}
349+
],
350+
"name_prefix": "bowling-",
351+
"tags": {
352+
"whodis": "walter"
353+
},
354+
"vpc_id": "\${data.aws_vpc.testRDSCluster_vpc_F47EEEFE.id}"
355+
}
356+
}
357+
}
358+
}"
359+
`;

0 commit comments

Comments
 (0)