-
Notifications
You must be signed in to change notification settings - Fork 139
/
01-newvpc.yaml
526 lines (485 loc) · 15.4 KB
/
01-newvpc.yaml
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
---
AWSTemplateFormatVersion: 2010-09-09
Description: Moodle network (VPC)
Parameters:
AvailabilityZones:
Description: 'List of Availability Zones to use for the subnets in the VPC. Note: The logical order is preserved.'
Type: List<AWS::EC2::AvailabilityZone::Name>
NumberOfAZs:
AllowedValues:
- 1
- 2
- 3
Default: 2
Description: Number of Availability Zones to use in the VPC. This must match your selections in the list of Availability Zones parameter.
Type: Number
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
DataSubnet0Cidr:
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.100.0/24
Description: CIDR block for data subnet 0 located in Availability Zone 0
Type: String
DataSubnet1Cidr:
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.101.0/24
Description: CIDR block for data subnet 1 located in Availability Zone 1
Type: String
DataSubnet2Cidr:
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.102.0/24
Description: CIDR block for data subnet 2 located in Availability Zone 2
Type: String
PublicSubnet0Cidr:
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.200.0/24
Description: CIDR block for Public subnet 0 located in Availability Zone 0
Type: String
PublicSubnet1Cidr:
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.201.0/24
Description: CIDR block for Public subnet 1 located in Availability Zone 1
Type: String
PublicSubnet2Cidr:
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.202.0/24
Description: CIDR block for Public subnet 2 located in Availability Zone 2
Type: String
AppSubnet0Cidr:
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/22
Description: CIDR block for App Subnet 0 located in Availability Zone 0
Type: String
AppSubnet1Cidr:
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.4.0/22
Description: CIDR block for App Subnet 1 located in Availability Zone 1
Type: String
AppSubnet2Cidr:
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.8.0/22
Description: CIDR block for App Subnet 2 located in Availability Zone 2
Type: String
Conditions:
NumberOfAZs1: !Equals [ '1', !Ref NumberOfAZs ]
NumberOfAZs2: !Equals [ '2', !Ref NumberOfAZs ]
NumberOfAZs3: !Equals [ '3', !Ref NumberOfAZs ]
AZ0: !Or
- !Condition NumberOfAZs1
- !Condition NumberOfAZs2
- !Condition NumberOfAZs3
AZ1: !Or
- !Condition NumberOfAZs2
- !Condition NumberOfAZs3
AZ2: !Condition NumberOfAZs3
Resources:
AppSubnet0:
Condition: AZ0
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [ 0, !Ref AvailabilityZones ]
CidrBlock: !Ref AppSubnet0Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [ '', [ 'AppSubnet0 / ', !Ref 'AWS::StackName' ] ]
- Key: SubnetType
Value: Private
VpcId: !Ref Vpc
AppSubnet1:
Condition: AZ1
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [ 1, !Ref AvailabilityZones ]
CidrBlock: !Ref AppSubnet1Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [ '', [ 'AppSubnet1 / ', !Ref 'AWS::StackName' ] ]
- Key: SubnetType
Value: Private
VpcId: !Ref Vpc
AppSubnet2:
Condition: AZ2
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [ 2, !Ref AvailabilityZones ]
CidrBlock: !Ref AppSubnet2Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [ '', [ 'AppSubnet2 / ', !Ref 'AWS::StackName' ] ]
- Key: SubnetType
Value: Private
VpcId: !Ref Vpc
AppSubnetRouteTableAssociation0:
Condition: AZ0
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref NatRouteTable0
SubnetId: !Ref AppSubnet0
AppSubnetRouteTableAssociation1:
Condition: AZ1
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref NatRouteTable1
SubnetId: !Ref AppSubnet1
AppSubnetRouteTableAssociation2:
Condition: AZ2
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref NatRouteTable2
SubnetId: !Ref AppSubnet2
DataSubnet0:
Condition: AZ0
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [ 0, !Ref AvailabilityZones ]
CidrBlock: !Ref DataSubnet0Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [ '', [ 'DataSubnet0 / ', !Ref 'AWS::StackName' ] ]
- Key: SubnetType
Value: Private
VpcId: !Ref Vpc
DataSubnet1:
Condition: AZ1
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [ 1, !Ref AvailabilityZones ]
CidrBlock: !Ref DataSubnet1Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [ '', [ 'DataSubnet1 / ', !Ref 'AWS::StackName' ] ]
- Key: SubnetType
Value: Private
VpcId: !Ref Vpc
DataSubnet2:
Condition: AZ2
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [ 2, !Ref AvailabilityZones ]
CidrBlock: !Ref DataSubnet2Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [ '', [ 'DataSubnet2 / ', !Ref 'AWS::StackName' ] ]
- Key: SubnetType
Value: Private
VpcId: !Ref Vpc
DataSubnetRouteTableAssociation0:
Condition: AZ0
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref NatRouteTable0
SubnetId: !Ref DataSubnet0
DataSubnetRouteTableAssociation1:
Condition: AZ1
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref NatRouteTable1
SubnetId: !Ref DataSubnet1
DataSubnetRouteTableAssociation2:
Condition: AZ2
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref NatRouteTable2
SubnetId: !Ref DataSubnet2
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Join [ '', [ 'InternetGateway / ', !Ref 'AWS::StackName' ] ]
AttachInternetGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref Vpc
NatEIP0:
Condition: AZ0
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NatGateway0:
Condition: AZ0
Type: AWS::EC2::NatGateway
DependsOn: AttachInternetGateway
Properties:
AllocationId: !GetAtt NatEIP0.AllocationId
SubnetId: !Ref PublicSubnet0
NatRoute0:
Condition: AZ0
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref NatRouteTable0
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway0
NatRouteTable0:
Condition: AZ0
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: !Join [ '', ['NatRouteTable0 / ', !Ref 'AWS::StackName' ] ]
- Key: Network
Value: Public
VpcId: !Ref Vpc
NatEIP1:
Condition: AZ1
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NatGateway1:
Condition: AZ1
Type: AWS::EC2::NatGateway
DependsOn: AttachInternetGateway
Properties:
AllocationId: !GetAtt NatEIP1.AllocationId
SubnetId: !Ref PublicSubnet1
NatRoute1:
Condition: AZ1
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref NatRouteTable1
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway1
NatRouteTable1:
Condition: AZ1
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: !Join [ '', [ 'NatRouteTable1 / ', !Ref 'AWS::StackName' ] ]
- Key: Network
Value: Public
VpcId: !Ref Vpc
NatEIP2:
Condition: AZ2
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NatGateway2:
Condition: AZ2
Type: AWS::EC2::NatGateway
DependsOn: AttachInternetGateway
Properties:
AllocationId: !GetAtt NatEIP2.AllocationId
SubnetId: !Ref PublicSubnet2
NatRoute2:
Condition: AZ2
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref NatRouteTable2
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway2
NatRouteTable2:
Condition: AZ2
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: !Join [ '', [ 'NatRouteTable2 / ', !Ref 'AWS::StackName' ] ]
- Key: Network
Value: Public
VpcId: !Ref Vpc
PublicRoute:
Type: AWS::EC2::Route
DependsOn: AttachInternetGateway
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: !Join [ '', [ 'PublicRouteTable / ', !Ref 'AWS::StackName' ] ]
- Key: Network
Value: Public
VpcId: !Ref Vpc
PublicRouteTableAssociation0:
Condition: AZ0
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet0
RouteTableId: !Ref PublicRouteTable
PublicRouteTableAssociation1:
Condition: AZ1
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet1
RouteTableId: !Ref PublicRouteTable
PublicRouteTableAssociation2:
Condition: AZ2
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet2
RouteTableId: !Ref PublicRouteTable
PublicSubnet0:
Condition: AZ0
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [ 0, !Ref AvailabilityZones ]
CidrBlock: !Ref PublicSubnet0Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [ '', [ 'PublicSubnet0 / ', !Ref 'AWS::StackName' ] ]
- Key: SubnetType
Value: Public
VpcId: !Ref Vpc
PublicSubnet1:
Condition: AZ1
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [ 1, !Ref AvailabilityZones ]
CidrBlock: !Ref PublicSubnet1Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [ '', [ 'PublicSubnet1 / ', !Ref 'AWS::StackName' ] ]
- Key: SubnetType
Value: Public
VpcId: !Ref Vpc
PublicSubnet2:
Condition: AZ2
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [ 2, !Ref AvailabilityZones ]
CidrBlock: !Ref PublicSubnet2Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [ '', [ 'PublicSubnet2 / ', !Ref 'AWS::StackName' ] ]
- Key: SubnetType
Value: Public
VpcId: !Ref Vpc
Vpc:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
EnableDnsHostnames: true
EnableDnsSupport: true
Tags:
- Key: Name
Value: !Join [ '', [ 'Vpc / ', !Ref 'AWS::StackName' ] ]
VpcFlowLog:
Type: AWS::EC2::FlowLog
Properties:
DeliverLogsPermissionArn: !GetAtt VpcFlowLogsRole.Arn
LogGroupName: !Join [ '', [ !Ref 'AWS::StackName', '-FlowLog' ] ]
ResourceId: !Ref Vpc
ResourceType: VPC
TrafficType: ALL
VpcFlowLogsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- vpc-flow-logs.amazonaws.com
Path: '/'
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- logs:CreateLogStream
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:PutLogEvents
Effect: Allow
Resource: !GetAtt VpcFlowLogsGroup.Arn
VpcFlowLogsGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
Properties:
RetentionInDays: 7
Outputs:
Vpc:
Value: !Ref Vpc
VpcCidr:
Value: !Ref VpcCidr
PublicSubnet0:
Condition: AZ0
Value: !Ref PublicSubnet0
PublicSubnet1:
Condition: AZ1
Value: !Ref PublicSubnet1
PublicSubnet2:
Condition: AZ2
Value: !Ref PublicSubnet2
AppSubnet0:
Condition: AZ0
Value: !Ref AppSubnet0
AppSubnet1:
Condition: AZ1
Value: !Ref AppSubnet1
AppSubnet2:
Condition: AZ2
Value: !Ref AppSubnet2
DataSubnet0:
Condition: AZ0
Value: !Ref DataSubnet0
DataSubnet1:
Condition: AZ1
Value: !Ref DataSubnet1
DataSubnet2:
Condition: AZ2
Value: !Ref DataSubnet2
DataSubnet:
Value:
!If
[ NumberOfAZs1,
!Ref DataSubnet0,
!If
[ NumberOfAZs2,
!Join [ ',', [ !Ref DataSubnet0, !Ref DataSubnet1 ] ],
!Join [ ',', [ !Ref DataSubnet0, !Ref DataSubnet1, !Ref DataSubnet2 ] ]
]
]
AppSubnet:
Value:
!If
[ NumberOfAZs1,
!Ref AppSubnet0,
!If
[ NumberOfAZs2,
!Join [ ',', [ !Ref AppSubnet0, !Ref AppSubnet1 ] ],
!Join [ ',', [ !Ref AppSubnet0, !Ref AppSubnet1, !Ref AppSubnet2 ] ]
]
]
PublicSubnet:
Value:
!If
[ NumberOfAZs1,
!Ref PublicSubnet0,
!If
[ NumberOfAZs2,
!Join [ ',', [ !Ref PublicSubnet0, !Ref PublicSubnet1 ] ],
!Join [ ',', [ !Ref PublicSubnet0, !Ref PublicSubnet1, !Ref PublicSubnet2 ] ]
]
]