-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaws-01-vpc.yaml
More file actions
401 lines (379 loc) · 12.6 KB
/
aws-01-vpc.yaml
File metadata and controls
401 lines (379 loc) · 12.6 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Network Design Patterns
# "1 tier | 1 subnet | public"
# "1 tier | 2 subnet | public"
# "2 tier | 1 subnet public | 1 subnet private | 1 nat"
# "2 tier | 2 subnets public | 2 subnets private | 1 nat"
# "3 tier | 1 subnet public | 1 subnet private | 1 subnet database | 1 nat"
# "3 tier | 2 subnets public | 2 subnets private | 2 subnets database | 1 nat"
AWSTemplateFormatVersion: 2010-09-09
Description: Creates VPC, Subnets, Internet Gateway, Nat Gateways, RouteTables, Security Groups and more.
Metadata:
Authors:
Description: Apper DevOps (developers@apper.ph) - Apper Digital Inc.
License:
Description:
"Copyright 2020 Amazon.com, Inc. and its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0"
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Amazon VPC Parameters
Parameters:
- EnvironmentName
- NetworkDesignPatterns
- VpcCidr
- SubnetsCidrBlock
ParameterLabels:
EnvironmentName:
default: Environment Name
NetworkDesignPatterns:
default: Network Design Patterns
VpcCidr:
default: VPC CIDR Block
SubnetsCidrBlock:
default: Subnets CIDR Blocks
Parameters:
EnvironmentName:
Description: The name of environment for the current stack (e.g. dev, test, staging, beta, production).
Type: String
NetworkDesignPatterns:
Description: The design pattern of the current vpc network architecture based from industry standards and cost tradeoffs
Type: String
AllowedValues:
- "1 tier | 1 subnet | public"
- "1 tier | 2 subnet | public"
- "2 tier | 1 subnet public | 1 subnet private | 1 nat"
- "2 tier | 2 subnets public | 2 subnets private | 1 nat"
- "3 tier | 1 subnet public | 1 subnet private | 1 subnet database | 1 nat"
- "3 tier | 2 subnets public | 2 subnets private | 2 subnets database | 1 nat"
Default: "1 tier | 2 subnet | public"
VpcCidr:
AllowedPattern: "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(1[6-9]|2[0-8]))$"
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
Default: 10.0.0.0/16
Description: CIDR block for the VPC
Type: String
SubnetsCidrBlock:
Description: |
Comma-delimited list of CIDR blocks for subnets located in AZ's within the current AWS Regions (e.g. 10.0.1.0/24, 10.0.2.0/24, 10.0.3.0/24).
Note: The logical order is preserved and must be greater than or equal to the number of subnet selected in the network design pattern field.
Type: CommaDelimitedList
Default: "10.0.1.0/24, 10.0.2.0/24, 10.0.3.0/24, 10.0.4.0/24, 10.0.5.0/24, 10.0.6.0/24"
Conditions:
# parameter conditionals
1Tier1SubPublic: !Equals [!Ref NetworkDesignPatterns, "1 tier | 1 subnet | public"]
1Tier2SubPublic: !Equals [!Ref NetworkDesignPatterns, "1 tier | 2 subnet | public"]
2Tier1SubPublicPrivate1Nat: !Equals [!Ref NetworkDesignPatterns, "2 tier | 1 subnet public | 1 subnet private | 1 nat"]
2Tier2SubPublicPrivate1Nat: !Equals [!Ref NetworkDesignPatterns, "2 tier | 2 subnets public | 2 subnets private | 1 nat"]
3Tier1SubPublicPrivate1Nat: !Equals [!Ref NetworkDesignPatterns, "3 tier | 1 subnet public | 1 subnet private | 1 subnet database | 1 nat"]
3Tier2SubPublicPrivate1Nat: !Equals [!Ref NetworkDesignPatterns, "3 tier | 2 subnets public | 2 subnets private | 2 subnets database | 1 nat"]
# template conditionals
1Subnet: !Or
- !Condition 1Tier1SubPublic
- !Condition 1Tier2SubPublic
- !Condition 2Tier1SubPublicPrivate1Nat
- !Condition 2Tier2SubPublicPrivate1Nat
- !Condition 3Tier1SubPublicPrivate1Nat
- !Condition 3Tier2SubPublicPrivate1Nat
2Subnet: !Or
- !Condition 1Tier2SubPublic
- !Condition 2Tier2SubPublicPrivate1Nat
- !Condition 3Tier2SubPublicPrivate1Nat
1Nat: !Or
- !Condition 2Tier1SubPublicPrivate1Nat
- !Condition 2Tier2SubPublicPrivate1Nat
- !Condition 3Tier1SubPublicPrivate1Nat
- !Condition 3Tier2SubPublicPrivate1Nat
1Tier: !Or
- !Condition 1Tier1SubPublic
- !Condition 1Tier2SubPublic
2Tier: !Or
- !Condition 2Tier1SubPublicPrivate1Nat
- !Condition 2Tier2SubPublicPrivate1Nat
- !Condition 3Tier1SubPublicPrivate1Nat
- !Condition 3Tier2SubPublicPrivate1Nat
3Tier: !Or
- !Condition 3Tier1SubPublicPrivate1Nat
- !Condition 3Tier2SubPublicPrivate1Nat
# Very complicated logic but it works!
1Tier1Sub: !Or
- !Condition 1Subnet
- !Condition 2Subnet
1Tier2Sub: !Or
- !Condition 2Subnet
- !Condition 1Tier2SubPublic
2Tier1Sub: !And
- !Condition 1Subnet
- !Condition 2Tier
2Tier2Sub: !And
- !Condition 2Subnet
- !Condition 2Tier
3Tier1Sub: !And
- !Condition 1Subnet
- !Condition 3Tier
3Tier2Sub: !And
- !Condition 2Subnet
- !Condition 3Tier
1TierRt: !Or
- !Condition 1Tier
- !Condition 2Tier
- !Condition 3Tier
2TierRt: !Or
- !Condition 2Tier
- !Condition 3Tier
Resources:
Vpc:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
EnableDnsHostnames: true
EnableDnsSupport: true
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-vpc-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-igw-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
AttachInternetGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref Vpc
# VPC ROUTES
PublicRoute:
Type: AWS::EC2::Route
DependsOn: AttachInternetGateway
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
# SUBNETS
# PUBLIC SUBNETS
PublicAZASubnet:
Condition: 1Tier1Sub
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref Vpc
AvailabilityZone: !Select [0, Fn::GetAZs: !Ref "AWS::Region"]
CidrBlock: !Select [0, !Ref SubnetsCidrBlock]
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-public-sub1-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
PublicAZBSubnet:
Condition: 1Tier2Sub
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref Vpc
AvailabilityZone: !Select [1, Fn::GetAZs: !Ref "AWS::Region"]
CidrBlock: !Select [1, !Ref SubnetsCidrBlock]
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-public-sub2-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
# PRIVATE SUBNETS
PrivateAZASubnet:
Condition: 2Tier1Sub
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref Vpc
AvailabilityZone: !Select [0, Fn::GetAZs: !Ref "AWS::Region"]
CidrBlock: !Select [2, !Ref SubnetsCidrBlock]
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-private-sub1-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
PrivateAZBSubnet:
Condition: 2Tier2Sub
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref Vpc
AvailabilityZone: !Select [1, Fn::GetAZs: !Ref "AWS::Region"]
CidrBlock: !Select [3, !Ref SubnetsCidrBlock]
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-private-sub2-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
# DATABASE SUBNETS
DatabaseAZASubnet:
Condition: 3Tier1Sub
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref Vpc
AvailabilityZone: !Select [0, Fn::GetAZs: !Ref "AWS::Region"]
CidrBlock: !Select [4, !Ref SubnetsCidrBlock]
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-database-sub1-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
DatabaseAZBSubnet:
Condition: 3Tier2Sub
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref Vpc
AvailabilityZone: !Select [1, Fn::GetAZs: !Ref "AWS::Region"]
CidrBlock: !Select [5, !Ref SubnetsCidrBlock]
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-database-sub2-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
# ROUTE TABLE
PublicRouteTable:
Type: AWS::EC2::RouteTable
Condition: 1TierRt
Properties:
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-public-rt-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Condition: 2TierRt
Properties:
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-private-rt-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
DatabaseRouteTable:
Type: AWS::EC2::RouteTable
Condition: 3Tier
Properties:
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-database-rt-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
# ROUTE ASSOC
PublicRouteTableAssociation1:
Condition: 1Tier1Sub
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicAZASubnet
RouteTableId: !Ref PublicRouteTable
PublicRouteTableAssociation2:
Condition: 1Tier2Sub
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicAZBSubnet
RouteTableId: !Ref PublicRouteTable
PrivateRouteTableAssociation1:
Condition: 2Tier1Sub
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateAZASubnet
RouteTableId: !Ref PrivateRouteTable
PrivateRouteTableAssociation2:
Condition: 2Tier2Sub
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateAZBSubnet
RouteTableId: !Ref PrivateRouteTable
DatabaseRouteTableAssociation1:
Condition: 3Tier1Sub
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref DatabaseAZASubnet
RouteTableId: !Ref DatabaseRouteTable
DatabaseRouteTableAssociation2:
Condition: 3Tier2Sub
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref DatabaseAZBSubnet
RouteTableId: !Ref DatabaseRouteTable
# NAT GATEWAYS
NatEIP1:
Condition: 1Nat
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NatGateway1:
Condition: 1Nat
Type: AWS::EC2::NatGateway
DependsOn: AttachInternetGateway
Properties:
AllocationId: !GetAtt NatEIP1.AllocationId
SubnetId: !Ref PublicAZASubnet
Tags:
- Key: Name
Value: !Sub "${EnvironmentName}-ngw1-${AWS::StackName}"
- Key: Environment
Value: !Ref EnvironmentName
- Key: Type
Value: "Managed by AWS Cloudformation"
NatRoute1:
Condition: 1Nat
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway1
NatRoute2:
Condition: 3Tier
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref DatabaseRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway1
Outputs:
Vpc:
Value: !Ref Vpc
VpcCidr:
Value: !Ref VpcCidr
# SubnetsCidrBlock:
# Value: !Join
# - ","
# - !Ref SubnetsCidrBlock
# PublicAZASubnet:
# Condition: 1Subnet
# Value: !Ref PublicAZASubnet
# PublicAZBSubnet:
# Condition: 2Subnet
# Value: !Ref PublicAZBSubnet
# PrivateAZASubnet:
# Condition: 1Subnet
# Value: !Ref PrivateAZASubnet
# PrivateAZBSubnet:
# Condition: 2Subnet
# Value: !Ref PrivateAZBSubnet