-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
subnet-group.ts
95 lines (81 loc) · 2.54 KB
/
subnet-group.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as ec2 from '@aws-cdk/aws-ec2';
import { IResource, RemovalPolicy, Resource, Token } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnDBSubnetGroup } from './rds.generated';
/**
* Interface for a subnet group.
*/
export interface ISubnetGroup extends IResource {
/**
* The name of the subnet group.
* @attribute
*/
readonly subnetGroupName: string;
}
/**
* Properties for creating a SubnetGroup.
*/
export interface SubnetGroupProps {
/**
* Description of the subnet group.
*/
readonly description: string;
/**
* The VPC to place the subnet group in.
*/
readonly vpc: ec2.IVpc;
/**
* The name of the subnet group.
*
* @default - a name is generated
*/
readonly subnetGroupName?: string;
/**
* Which subnets within the VPC to associate with this group.
*
* @default - private subnets
*/
readonly vpcSubnets?: ec2.SubnetSelection;
/**
* The removal policy to apply when the subnet group are removed
* from the stack or replaced during an update.
*
* @default RemovalPolicy.DESTROY
*/
readonly removalPolicy?: RemovalPolicy
}
/**
* Class for creating a RDS DB subnet group
*
* @resource AWS::RDS::DBSubnetGroup
*/
export class SubnetGroup extends Resource implements ISubnetGroup {
/**
* Imports an existing subnet group by name.
*/
public static fromSubnetGroupName(scope: Construct, id: string, subnetGroupName: string): ISubnetGroup {
return new class extends Resource implements ISubnetGroup {
public readonly subnetGroupName = subnetGroupName;
}(scope, id);
}
public readonly subnetGroupName: string;
constructor(scope: Construct, id: string, props: SubnetGroupProps) {
super(scope, id);
const { subnetIds } = props.vpc.selectSubnets(props.vpcSubnets ?? { subnetType: ec2.SubnetType.PRIVATE });
// Using 'Default' as the resource id for historical reasons (usage from `Instance` and `Cluster`).
const subnetGroup = new CfnDBSubnetGroup(this, 'Default', {
dbSubnetGroupDescription: props.description,
// names are actually stored by RDS changed to lowercase on the server side,
// and not lowercasing them in CloudFormation means things like { Ref }
// do not work correctly
dbSubnetGroupName: Token.isUnresolved(props.subnetGroupName)
? props.subnetGroupName
: props.subnetGroupName?.toLowerCase(),
subnetIds,
});
if (props.removalPolicy) {
subnetGroup.applyRemovalPolicy(props.removalPolicy);
}
this.subnetGroupName = subnetGroup.ref;
}
}