-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpc_template.yml
509 lines (479 loc) · 16.3 KB
/
vpc_template.yml
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
---
AWSTemplateFormatVersion: "2010-09-09"
Description: Nitro Enclaves Vault - VPC
Parameters:
pDomainName:
Type: String
Description: Domain name for vault API
pResourcePrefix:
Type: String
Description: VPC Resource Prefix
Default: nitro-vault
pCidrBlock:
Type: String
Description: VPC CIDR Block
Default: 10.0.0.0/16
pEnableRemoteAccess:
Type: String
Description: Enable Remote Access through SSM (optional)
Default: "false"
AllowedValues:
- "true"
- "false"
pEnableDnsFirewall:
Type: String
Description: Enable Route 53 Resolver DNS Firewall (optional)
Default: "true"
AllowedValues:
- "true"
- "false"
Metadata:
"AWS::CloudFormation::Interface":
ParameterGroups:
- Label:
default: VPC Configuration
Parameters:
- pResourcePrefix
- pCidrBlock
- pEnableDnsFirewall
- Label:
default: Network Configuration
Parameters:
- pDomainName
- Label:
default: Remote Access Configuration
Parameters:
- pEnableRemoteAccess
Conditions:
cEnableRemoteAccess: !Equals [!Ref pEnableRemoteAccess, "true"]
cEnableDnsFirewall: !Equals [!Ref pEnableDnsFirewall, "true"]
Resources:
rVpc:
Type: "AWS::EC2::VPC"
Metadata:
cfn_nag:
rules_to_suppress:
- id: W60
reason: "Ignoring vpc flow logs"
Properties:
CidrBlock: !Ref pCidrBlock
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: !Sub "${pResourcePrefix}-vpc"
rPrivateSubnet1: # Lambda Subnet
Type: "AWS::EC2::Subnet"
Properties:
AvailabilityZone: !Select [0, !GetAZs ""]
CidrBlock: !Select [0, !Cidr [!Ref pCidrBlock, 6, 8]] # /24
MapPublicIpOnLaunch: false
PrivateDnsNameOptionsOnLaunch:
EnableResourceNameDnsAAAARecord: false
EnableResourceNameDnsARecord: true
HostnameType: resource-name
Tags:
- Key: Name
Value: !Sub
- "${pResourcePrefix}-subnet-private1-${AvailabilityZone}"
- AvailabilityZone: !Select [0, !GetAZs ""]
VpcId: !Ref rVpc
rPrivateSubnet2: # Lambda Subnet
Type: "AWS::EC2::Subnet"
Properties:
AvailabilityZone: !Select [1, !GetAZs ""]
CidrBlock: !Select [1, !Cidr [!Ref pCidrBlock, 6, 8]] # /24
MapPublicIpOnLaunch: false
PrivateDnsNameOptionsOnLaunch:
EnableResourceNameDnsAAAARecord: false
EnableResourceNameDnsARecord: true
HostnameType: resource-name
Tags:
- Key: Name
Value: !Sub
- "${pResourcePrefix}-subnet-private2-${AvailabilityZone}"
- AvailabilityZone: !Select [1, !GetAZs ""]
VpcId: !Ref rVpc
rPrivateSubnet3: # EC2 Subnet
Type: "AWS::EC2::Subnet"
Properties:
AvailabilityZone: !Select [0, !GetAZs ""]
CidrBlock: !Select [4, !Cidr [!Ref pCidrBlock, 6, 8]] # /24
MapPublicIpOnLaunch: false
PrivateDnsNameOptionsOnLaunch:
EnableResourceNameDnsAAAARecord: false
EnableResourceNameDnsARecord: true
HostnameType: resource-name
Tags:
- Key: Name
Value: !Sub
- "${pResourcePrefix}-subnet-private3-${AvailabilityZone}"
- AvailabilityZone: !Select [0, !GetAZs ""]
VpcId: !Ref rVpc
rPrivateSubnet4: # EC2 Subnet
Type: "AWS::EC2::Subnet"
Properties:
AvailabilityZone: !Select [1, !GetAZs ""]
CidrBlock: !Select [5, !Cidr [!Ref pCidrBlock, 6, 8]] # /24
MapPublicIpOnLaunch: false
PrivateDnsNameOptionsOnLaunch:
EnableResourceNameDnsAAAARecord: false
EnableResourceNameDnsARecord: true
HostnameType: resource-name
Tags:
- Key: Name
Value: !Sub
- "${pResourcePrefix}-subnet-private4-${AvailabilityZone}"
- AvailabilityZone: !Select [1, !GetAZs ""]
VpcId: !Ref rVpc
rPrivateRouteTable1:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref rVpc
Tags:
- Key: Name
Value: !Sub "${pResourcePrefix}-rtb-private1-${rPrivateSubnet1.AvailabilityZone}"
rPrivateRouteTable2:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref rVpc
Tags:
- Key: Name
Value: !Sub "${pResourcePrefix}-rtb-private2-${rPrivateSubnet2.AvailabilityZone}"
rPrivateRouteTable3:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref rVpc
Tags:
- Key: Name
Value: !Sub "${pResourcePrefix}-rtb-private3-${rPrivateSubnet3.AvailabilityZone}"
rPrivateRouteTable4:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref rVpc
Tags:
- Key: Name
Value: !Sub "${pResourcePrefix}-rtb-private4-${rPrivateSubnet4.AvailabilityZone}"
rPrivateRouteTableAssociation1:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref rPrivateSubnet1
RouteTableId: !Ref rPrivateRouteTable1
rPrivateRouteTableAssociation2:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref rPrivateSubnet2
RouteTableId: !Ref rPrivateRouteTable2
rPrivateRouteTableAssociation3:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref rPrivateSubnet3
RouteTableId: !Ref rPrivateRouteTable3
rPrivateRouteTableAssociation4:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref rPrivateSubnet4
RouteTableId: !Ref rPrivateRouteTable4
rVpcEndpointSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: VPC Endpoint Security Group
SecurityGroupEgress:
- CidrIp: "127.0.0.1/32"
Description: Deny outbound traffic
IpProtocol: -1
SecurityGroupIngress:
- CidrIp: !GetAtt rVpc.CidrBlock
Description: Allow HTTPS traffic from the VPC
FromPort: 443
IpProtocol: tcp
ToPort: 443
VpcId: !Ref rVpc
rVpcEndpointS3:
Type: "AWS::EC2::VPCEndpoint"
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: AllowRequestsToAWSOwnedResources
Effect: Allow
Principal: "*"
Action: "s3:GetObject"
Resource:
- !Sub "arn:${AWS::Partition}:s3:::al2023-repos-${AWS::Region}-de612dc2/*"
- !Sub "arn:${AWS::Partition}:s3:::al2023-${AWS::Region}/*"
- !Sub "arn:${AWS::Partition}:s3:::aws-ssm-${AWS::Region}/*"
- !Sub "arn:${AWS::Partition}:s3:::amazon-ssm-${AWS::Region}/*"
- !Sub "arn:${AWS::Partition}:s3:::amazon-ssm-packages-${AWS::Region}/*"
- !Sub "arn:${AWS::Partition}:s3:::${AWS::Region}-birdwatcher-prod/*"
- !Sub "arn:${AWS::Partition}:s3:::aws-ssm-distributor-file-${AWS::Region}/*"
- !Sub "arn:${AWS::Partition}:s3:::aws-ssm-document-attachments-${AWS::Region}/*"
- !Sub "arn:${AWS::Partition}:s3:::patch-baseline-snapshot-${AWS::Region}/*"
- !Sub "arn:${AWS::Partition}:s3:::aws-patchmanager-macos-${AWS::Region}/*"
- !Sub "arn:${AWS::Partition}:s3:::amazoncloudwatch-agent-${AWS::Region}/*"
- !Sub "arn:${AWS::Partition}:s3:::amazoncloudwatch-agent/*"
- !Sub "arn:${AWS::Partition}:s3:::prod-${AWS::Region}-starport-layer-bucket/*"
- !Sub "arn:${AWS::Partition}:s3:::cloudformation-waitcondition-${AWS::Region}/*"
- !Sub "arn:${AWS::Partition}:s3:::aws-ec2-enclave-certificate-${AWS::Region}-prod/*"
- Sid: AllowRequestsByAccountIdentitiesToAccountResources
Effect: Allow
Principal: "*"
Action: "s3:GetObject"
Resource: "*"
Condition:
StringEquals:
"aws:PrincipalAccount": !Ref "AWS::AccountId"
"aws:ResourceAccount": !Ref "AWS::AccountId"
RouteTableIds:
- !Ref rPrivateRouteTable3 # EC2 Route Table
- !Ref rPrivateRouteTable4 # EC2 Route Table
ServiceName: !Sub "com.amazonaws.${AWS::Region}.s3"
VpcEndpointType: Gateway
VpcId: !Ref rVpc
rVpcEndpointDynamoDB:
Type: "AWS::EC2::VPCEndpoint"
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: AllowRequestsByAccountIdentitiesToAccountResources
Effect: Allow
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringEquals:
"aws:PrincipalAccount": !Ref "AWS::AccountId"
"aws:ResourceAccount": !Ref "AWS::AccountId"
RouteTableIds:
- !Ref rPrivateRouteTable1 # Lambda Route Table
- !Ref rPrivateRouteTable2 # Lambda Route Table
ServiceName: !Sub "com.amazonaws.${AWS::Region}.dynamodb"
VpcEndpointType: Gateway
VpcId: !Ref rVpc
rVpcEndpointKms:
Type: "AWS::EC2::VPCEndpoint"
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal: "*"
Action:
- "kms:Decrypt"
- "kms:GenerateDataKeyPairWithoutPlaintext"
Resource: "*"
- Sid: DenyRequestsByThirdPartyIdentities
Effect: Deny
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringNotEquals:
"aws:PrincipalAccount": !Ref "AWS::AccountId"
# cannot use aws:ResourceAccount here due to retrieving the ACM key
PrivateDnsEnabled: true
ServiceName: !Sub "com.amazonaws.${AWS::Region}.kms"
SecurityGroupIds:
- !Ref rVpcEndpointSecurityGroup
SubnetIds:
- !Ref rPrivateSubnet1 # Lambda Subnet AZ1
- !Ref rPrivateSubnet2 # Lambda Subnet AZ2
VpcEndpointType: Interface
VpcId: !Ref rVpc
rVpcEndpointSsm:
Type: "AWS::EC2::VPCEndpoint"
Condition: cEnableRemoteAccess
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal: "*"
Action: "*"
Resource: "*"
- Sid: DenyRequestsByThirdPartyIdentities
Effect: Deny
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringNotEquals:
"aws:PrincipalAccount": !Ref "AWS::AccountId"
PrivateDnsEnabled: true
ServiceName: !Sub "com.amazonaws.${AWS::Region}.ssm"
SecurityGroupIds:
- !Ref rVpcEndpointSecurityGroup
SubnetIds:
- !Ref rPrivateSubnet3 # EC2 Subnet AZ1
- !Ref rPrivateSubnet4 # EC2 Subnet AZ2
VpcEndpointType: Interface
VpcId: !Ref rVpc
rVpcEndpointSsmMessages:
Type: "AWS::EC2::VPCEndpoint"
Condition: cEnableRemoteAccess
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal: "*"
Action: "*"
Resource: "*"
- Sid: DenyRequestsByThirdPartyIdentities
Effect: Deny
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringNotEquals:
"aws:PrincipalAccount": !Ref "AWS::AccountId"
PrivateDnsEnabled: true
ServiceName: !Sub "com.amazonaws.${AWS::Region}.ssmmessages"
SecurityGroupIds:
- !Ref rVpcEndpointSecurityGroup
SubnetIds:
- !Ref rPrivateSubnet3 # EC2 Subnet AZ1
- !Ref rPrivateSubnet4 # EC2 Subnet AZ2
VpcEndpointType: Interface
VpcId: !Ref rVpc
rVpcEndpointEc2Messages:
Type: "AWS::EC2::VPCEndpoint"
Condition: cEnableRemoteAccess
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal: "*"
Action: "*"
Resource: "*"
- Sid: DenyRequestsByThirdPartyIdentities
Effect: Deny
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringNotEquals:
"aws:PrincipalAccount": !Ref "AWS::AccountId"
PrivateDnsEnabled: true
ServiceName: !Sub "com.amazonaws.${AWS::Region}.ec2messages"
SecurityGroupIds:
- !Ref rVpcEndpointSecurityGroup
SubnetIds:
- !Ref rPrivateSubnet3 # EC2 Subnet AZ1
- !Ref rPrivateSubnet4 # EC2 Subnet AZ2
VpcEndpointType: Interface
VpcId: !Ref rVpc
rVpcEndpointCloudFormation:
Type: "AWS::EC2::VPCEndpoint"
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: AllowRequestsByAccountIdentitiesToAccountResources
Effect: Allow
Principal: "*"
Action:
- "cloudformation:DescribeStackResource" # used by cfn-init
- "cloudformation:SignalResource" # used by cfn-signal
Resource: "*"
Condition:
StringEquals:
"aws:PrincipalAccount": !Ref "AWS::AccountId"
"aws:ResourceAccount": !Ref "AWS::AccountId"
PrivateDnsEnabled: true
ServiceName: !Sub "com.amazonaws.${AWS::Region}.cloudformation"
SecurityGroupIds:
- !Ref rVpcEndpointSecurityGroup
SubnetIds:
- !Ref rPrivateSubnet3 # EC2 Subnet AZ1
- !Ref rPrivateSubnet4 # EC2 Subnet AZ2
VpcEndpointType: Interface
VpcId: !Ref rVpc
rAllDomainsList:
Type: "AWS::Route53Resolver::FirewallDomainList"
Condition: cEnableDnsFirewall
Properties:
Domains:
- "*"
Name: !Sub "${pResourcePrefix}-AllDomains"
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: rAllDomainsList
- Key: AppManagerCFNStackKey
Value: !Ref "AWS::StackName"
rAllowDomainsList:
Type: "AWS::Route53Resolver::FirewallDomainList"
Condition: cEnableDnsFirewall
Properties:
Domains:
- !Ref pDomainName
- !Sub "*.${AWS::URLSuffix}"
Name: !Sub "${pResourcePrefix}-AllowDomains"
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: rAllowDomainsList
- Key: AppManagerCFNStackKey
Value: !Ref "AWS::StackName"
rWalledGardenRuleGroup:
Type: "AWS::Route53Resolver::FirewallRuleGroup"
Condition: cEnableDnsFirewall
Properties:
FirewallRules:
- Action: ALLOW
FirewallDomainListId: !Ref rAllowDomainsList
Priority: 1
- Action: BLOCK
BlockResponse: NODATA
FirewallDomainListId: !Ref rAllDomainsList
Priority: 2
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: rWalledGardenRuleGroup
- Key: AppManagerCFNStackKey
Value: !Ref "AWS::StackName"
rFirewallRuleGroupAssociation:
Type: "AWS::Route53Resolver::FirewallRuleGroupAssociation"
Condition: cEnableDnsFirewall
Properties:
FirewallRuleGroupId: !Ref rWalledGardenRuleGroup
MutationProtection: DISABLED # Prevents deletion when enabled
Priority: 101
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: rFirewallRuleGroupAssociation
- Key: AppManagerCFNStackKey
Value: !Ref "AWS::StackName"
VpcId: !Ref rVpc
Outputs:
oVpcId:
Description: VPC ID
Value: !Ref rVpc
oVpcCidrBlock:
Description: VPC CIDR block
Value: !GetAtt rVpc.CidrBlock
oApiSubnetIds:
Description: API Subnet IDs
Value: !Join [",", [!Ref rPrivateSubnet1, !Ref rPrivateSubnet2]]
oInstanceSubnetIds:
Description: EC2 Instance Subnet IDs
Value: !Join [",", [!Ref rPrivateSubnet3, !Ref rPrivateSubnet4]]