-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
296 lines (256 loc) · 9.63 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
//Copyright © 2024 ScaleReal Technologies Pvt Ltd.
//
// SPDX-License-Identifier: BSD 3-Clause
//
import { Construct } from "constructs";
import { TerraformOutput, Fn, TerraformIterator, Token } from "cdktf";
import { Eip } from "@cdktf/provider-aws/lib/eip";
import { DataAwsCallerIdentity } from "@cdktf/provider-aws/lib/data-aws-caller-identity";
import { Subnet } from "@cdktf/provider-aws/lib/subnet";
import { InternetGateway } from "@cdktf/provider-aws/lib/internet-gateway";
import { NatGateway } from "@cdktf/provider-aws/lib/nat-gateway";
import { Vpc } from "@cdktf/provider-aws/lib/vpc";
import { EgressOnlyInternetGateway } from "@cdktf/provider-aws/lib/egress-only-internet-gateway";
import { RouteTable } from "@cdktf/provider-aws/lib/route-table";
import { RouteTableAssociation } from "@cdktf/provider-aws/lib/route-table-association";
import { Route } from "@cdktf/provider-aws/lib/route";
import { DataAwsAvailabilityZones } from "@cdktf/provider-aws/lib/data-aws-availability-zones";
import { Op } from "cdktf";
import { StaticResource } from "@cdktf/provider-time/lib/static-resource";
import { TimeProvider } from "@cdktf/provider-time/lib/provider";
export interface VpcModuleConfig {
cidr: string;
service_name: string;
env: string;
enable_private_subnets?: boolean;
enable_database_subnets?: boolean;
enable_ip4_nat?: boolean;
enable_ip6_egw?: boolean;
tags?: { [key: string]: string };
}
export class VpcModule extends Construct {
public readonly vpc: Vpc;
private natGw?: NatGateway;
private egw?: EgressOnlyInternetGateway;
public readonly public_subnet_it: TerraformIterator;
public readonly private_subnet_it?: TerraformIterator;
public readonly database_subnet_it?: TerraformIterator;
// Outputs
public readonly vpc_id: string;
public readonly vpc_cidr: string;
public readonly vpc_ipv6_cidr: string;
public readonly public_subnet_ids: string[];
public readonly private_subnet_ids?: string[];
public readonly database_subnet_ids?: string[];
constructor(scope: Construct, name: string, config: VpcModuleConfig) {
super(scope, name);
const {
cidr,
service_name,
env,
enable_private_subnets = false,
enable_database_subnets = false,
enable_ip4_nat = false,
enable_ip6_egw = false,
tags = {},
} = config;
const azs = new DataAwsAvailabilityZones(this, "azs", {
state: "available",
});
// Create iterator for AZs
const azIterator = TerraformIterator.fromList(azs.names);
const caller = new DataAwsCallerIdentity(this, "current");
const time_static = new StaticResource(this, "timestamp");
const vpc_name = `${service_name}_${env}_vpc`;
const defaultTags = {
Name: vpc_name,
Service: service_name,
ENV: env,
Provisioner: "CDKTF",
"Provisioned By": caller.userId,
"Provisioned Date": time_static.id,
...tags,
};
new TimeProvider(this, "time", {});
this.vpc = new Vpc(this, "vpc", {
cidrBlock: cidr,
enableDnsHostnames: true,
enableDnsSupport: true,
assignGeneratedIpv6CidrBlock: true,
tags: defaultTags,
});
const numOfSubnetTypes = 1 +
(enable_private_subnets ? 1 : 0) +
(enable_database_subnets ? 1 : 0);
const newBits = Fn.max([Fn.ceil(Fn.log(Op.mul(numOfSubnetTypes, Fn.lengthOf(azs.names)), 2)), 3]);
// Create public subnets
const public_subnet = new Subnet(this, "public-subnet", {
forEach: azIterator,
vpcId: this.vpc.id,
cidrBlock: Fn.cidrsubnet(cidr, newBits, Op.add(Fn.index(azs.names, azIterator.key), 1)),
ipv6CidrBlock: Fn.cidrsubnet(this.vpc.ipv6CidrBlock, 8, Op.add(Fn.index(azs.names, azIterator.key), 1)),
assignIpv6AddressOnCreation: true,
availabilityZone: azIterator.value,
mapPublicIpOnLaunch: true,
tags: {
...defaultTags,
Name: `${vpc_name}-public-${Fn.element(Fn.split("-", azIterator.value), 2)}`,
},
});
this.public_subnet_it = TerraformIterator.fromResources(public_subnet);
// Create private subnets
if (enable_private_subnets) {
const private_subnet = new Subnet(this, "private-subnet", {
forEach: azIterator,
vpcId: this.vpc.id,
cidrBlock: Fn.cidrsubnet(cidr, newBits,
Op.add(Op.add(Fn.index(azs.names, azIterator.key), 1), Fn.lengthOf(azs.names))),
ipv6CidrBlock: Fn.cidrsubnet(this.vpc.ipv6CidrBlock, 8,
Op.add(Op.add(Fn.index(azs.names, azIterator.key), 1), Fn.lengthOf(azs.names))),
assignIpv6AddressOnCreation: true,
availabilityZone: azIterator.value,
tags: {
...defaultTags,
Name: `${vpc_name}-private-${Fn.element(Fn.split("-", azIterator.value), 2)}`,
},
});
this.private_subnet_it = TerraformIterator.fromResources(private_subnet);
}
// Create database subnets
if (enable_database_subnets) {
const database_subnet = new Subnet(this, "database-subnet", {
forEach: azIterator,
vpcId: this.vpc.id,
cidrBlock: Fn.cidrsubnet(cidr, newBits,
Op.add(Op.add(Fn.index(azs.names, azIterator.key), 1), Op.mul(2, Fn.lengthOf(azs.names)))),
ipv6CidrBlock: Fn.cidrsubnet(this.vpc.ipv6CidrBlock, 8,
Op.add(Op.add(Fn.index(azs.names, azIterator.key), 1), Op.mul(2, Fn.lengthOf(azs.names)))),
assignIpv6AddressOnCreation: true,
availabilityZone: azIterator.value,
tags: {
...defaultTags,
Name: `${vpc_name}-database-${Fn.element(Fn.split("-", azIterator.value), 2)}`,
},
});
this.database_subnet_it = TerraformIterator.fromResources(database_subnet);
}
const igw = new InternetGateway(this, "igw", {
vpcId: this.vpc.id,
tags: defaultTags,
});
// Create route tables and routes
const public_route_table = new RouteTable(this, "public-rt", {
vpcId: this.vpc.id,
tags: {
...defaultTags,
Name: `${vpc_name}-public-rt`,
},
});
new Route(this, "public-route", {
routeTableId: public_route_table.id,
destinationCidrBlock: "0.0.0.0/0",
gatewayId: igw.id,
});
new Route(this, "public-route-ipv6", {
routeTableId: public_route_table.id,
destinationIpv6CidrBlock: "::/0",
gatewayId: igw.id,
});
// Associate public subnets with route table
new RouteTableAssociation(this, "public-rta", {
forEach: this.public_subnet_it,
subnetId: this.public_subnet_it.getString("id"),
routeTableId: public_route_table.id,
});
if (enable_private_subnets) {
const private_route_table = new RouteTable(this, "private-rt", {
vpcId: this.vpc.id,
tags: {
...defaultTags,
Name: `${vpc_name}-private-rt`,
},
});
if (enable_ip4_nat) {
const eip = new Eip(this, "nat-eip", {
tags: defaultTags,
});
this.natGw = new NatGateway(this, "nat-gw", {
allocationId: eip.id,
subnetId: Fn.element(this.public_subnet_it.pluckProperty("id"), 0),
tags: defaultTags,
});
new Route(this, "private-nat-route", {
routeTableId: private_route_table.id,
destinationCidrBlock: "0.0.0.0/0",
natGatewayId: this.natGw.id,
});
}
if (enable_ip6_egw) {
this.egw = new EgressOnlyInternetGateway(this, "egw", {
vpcId: this.vpc.id,
tags: defaultTags,
});
new Route(this, "private-ipv6-route", {
routeTableId: private_route_table.id,
destinationIpv6CidrBlock: "::/0",
egressOnlyGatewayId: this.egw.id,
});
}
// Associate private subnets with route table
if (this.private_subnet_it) {
new RouteTableAssociation(this, "private-rta", {
forEach: this.private_subnet_it,
subnetId: this.private_subnet_it.getString("id"),
routeTableId: private_route_table.id,
});
}
}
if (enable_database_subnets) {
const database_route_table = new RouteTable(this, "database-rt", {
vpcId: this.vpc.id,
tags: {
...defaultTags,
Name: `${vpc_name}-database-rt`,
},
});
// Associate database subnets with route table
if (this.database_subnet_it) {
new RouteTableAssociation(this, "database-rta", {
forEach: this.database_subnet_it,
subnetId: this.database_subnet_it.getString("id"),
routeTableId: database_route_table.id,
});
}
}
// Outputs
this.vpc_id = this.vpc.id;
this.vpc_cidr = this.vpc.cidrBlock;
this.vpc_ipv6_cidr = this.vpc.ipv6CidrBlock;
this.public_subnet_ids = Token.asList(this.public_subnet_it.pluckProperty("id"));
new TerraformOutput(this, "vpc_id", {
value: this.vpc.id,
});
new TerraformOutput(this, "vpc_cidr", {
value: this.vpc.cidrBlock,
});
new TerraformOutput(this, "vpc_ipv6_cidr", {
value: this.vpc.ipv6CidrBlock,
});
new TerraformOutput(this, "public_subnet_ids", {
value: this.public_subnet_it.pluckProperty("id"),
});
if (enable_private_subnets && this.private_subnet_it) {
this.private_subnet_ids = Token.asList(this.private_subnet_it.pluckProperty("id"));
new TerraformOutput(this, "private_subnet_ids", {
value: this.private_subnet_it.pluckProperty("id"),
});
}
if (enable_database_subnets && this.database_subnet_it) {
this.database_subnet_ids = Token.asList(this.database_subnet_it.pluckProperty("id"));
new TerraformOutput(this, "database_subnet_ids", {
value: this.database_subnet_it.pluckProperty("id"),
});
}
}
}