forked from HicResearch/TREEHOOSE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeploymentInstance-Cfn.yaml
533 lines (512 loc) · 18.1 KB
/
DeploymentInstance-Cfn.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
527
528
529
530
531
532
533
# (c) 2022 Amazon Web Services, Inc. or its affiliates. All Rights Reserved.
# This AWS Content is provided subject to the terms of the AWS Customer
# Agreement available at https://aws.amazon.com/agreement or other written
# agreement between Customer and Amazon Web Services, Inc.
###############################################################################
AWSTemplateFormatVersion: "2010-09-09"
Description: Provision an EC2 instance to be used for TRE deployment.
Parameters:
VPCNetworkRange:
Description: Specify the VPC where the EC2 instance should reside in
Type: String
Default: "10.0.0.0/28"
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])(\/([0-9]|[1-2][0-9]|3[0-2]))$'
SubnetNetworkRange:
Description: Specify the Subnet where the EC2 instance should reside in
Type: String
Default: "10.0.0.0/28"
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])(\/([0-9]|[1-2][0-9]|3[0-2]))$'
VPCFlowLogsRetentionInDays:
Description: Specify the number of days you want to retain VPC flow log events"
Type: Number
Default: 14
AllowedValues:
[
1,
3,
5,
7,
14,
30,
60,
90,
120,
150,
180,
365,
400,
545,
731,
1827,
3653,
]
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VPCNetworkRange
Tags:
- Key: "Name"
Value: "TREDeploymentInstance-VPC"
VPCFlowLogRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: vpc-flow-logs.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: "flowlogs-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
- logs:DescribeLogGroups
- logs:DescribeLogStreams
Resource: !GetAtt VPCFlowLogsGroup.Arn
VPCFlowLogsGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "VPCFlowLogsGroup-${AWS::AccountId}"
RetentionInDays: !Ref VPCFlowLogsRetentionInDays
KmsKeyId: !ImportValue LandingZoneKeyARN
VPCFlowLogs:
Type: AWS::EC2::FlowLog
Properties:
DeliverLogsPermissionArn: !GetAtt VPCFlowLogRole.Arn
LogGroupName: !Ref VPCFlowLogsGroup
ResourceId: !Ref VPC
ResourceType: VPC
TrafficType: ALL
Subnet:
Metadata:
cfn_nag:
rules_to_suppress:
- id: W33
reason: "Public IP address is required"
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: !Ref SubnetNetworkRange
MapPublicIpOnLaunch: true
Tags:
- Key: "Name"
Value: "TREDeploymentInstance-Subnet"
InternetGateway:
Type: AWS::EC2::InternetGateway
InternetGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
SubnetRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
InternetGatewayRoute:
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: "0.0.0.0/0"
GatewayId: !Ref InternetGateway
RouteTableId: !Ref SubnetRouteTable
SubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref Subnet
RouteTableId: !Ref SubnetRouteTable
Instance:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
config:
commands:
dependencies:
command: !Sub "su ec2-user -c ' \
echo \"Initiating yum packages installation\"; \
sudo yum update -y; \
sudo yum install -y yum-utils git jq; \
echo \"Completed yum packages installation\"; \
echo \"---\"; \
echo \"Initiating nodejs and serverless installs\"; \
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash; \
export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin; \
. ~/.nvm/nvm.sh; \
source ~/.bashrc; \
nvm install v14.17.0; \
npm install -g [email protected] [email protected]; \
echo \"Completed nodejs and serverless installs\"; \
echo \"---\"; \
echo \"Downloading additional packages\"; \
mkdir ~/tmp && cd ~/tmp; \
curl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"; \
wget https://go.dev/dl/go1.18.linux-amd64.tar.gz; \
echo \"Finished downloading additional packages\"; \
echo \"---\"; \
echo \"Extracting additional packages\"; \
sudo unzip awscliv2.zip; \
sudo rm -rf /usr/local/go; \
sudo tar -C /usr/local -xzf go1.18.linux-amd64.tar.gz; \
echo \"Finished extracting additional packages\"; \
echo \"---\"; \
echo \"Installing additional packages\"; \
./aws/install; \
echo \"export PATH=/usr/local/go/bin:$PATH\" >>~/.bash_profile; \
echo \"Finished installing additional packages\"; \
echo \"---\"; \
echo \"Create default AWS profile\"; \
mkdir ~/.aws; \
echo \"[default]\" > ~/.aws/config; \
echo \"region = ${AWS::Region}\" >> ~/.aws/config; \
echo \"[default]\" > ~/.aws/credentials; \
echo \"region = ${AWS::Region}\" >> ~/.aws/credentials; \
echo \"Create default VPC\"; \
existing_vpc=`aws ec2 describe-vpcs --filters Name=isDefault,Values=true --query \"Vpcs[].VpcId\" --profile default | jq .[0]`; \
if [ \"$existing_vpc_id\" == \"null\" ]; then aws ec2 create-default-vpc; fi;'"
Properties:
Tags:
- Key: "Name"
Value: "TREDeploymentInstance"
InstanceType: t2.medium
IamInstanceProfile: !Ref InstanceProfile
SubnetId: !Ref Subnet
# amazon/amzn2-ami-kernel-5.10-hvm-2.0.20220316.0-x86_64-gp2
ImageId: ami-03e88be9ecff64781
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeType: gp3
VolumeSize: 40
DeleteOnTermination: true
Encrypted: true
UserData: !Base64
'Fn::Join':
- ''
- - |
#!/bin/bash -x
- |
- yum install -y aws-cfn-bootstrap
- |+
- |
- '/opt/aws/bin/cfn-init -v '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource Instance '
- ' --region '
- !Ref 'AWS::Region'
- |+
- |
- '/opt/aws/bin/cfn-signal -e $? '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource Instance '
- ' --region '
- !Ref 'AWS::Region'
- |+
CreationPolicy:
ResourceSignal:
Count: 1
Timeout: PT5M
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref InstanceIAMRole
InstanceIAMRole:
Type: AWS::IAM::Role
Metadata:
cfn_nag:
rules_to_suppress:
- id: W11
reason: "Asterisks in the policy are required"
- id: F38
reason: "Asterisks in the policy are required"
Properties:
Tags:
- Key: "Name"
Value: "TREDeploymentInstance-IAMRole"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonAppStreamFullAccess
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
Policies:
- PolicyName: CloudFormationAccess
PolicyDocument:
Statement:
- Action:
- cloudformation:CreateStack
- cloudformation:DescribeStacks
- cloudformation:UpdateStack
- cloudformation:DeleteStack
- cloudformation:UpdateTerminationProtection
- cloudformation:ListStackResources
- cloudformation:GetTemplate
- cloudformation:ValidateTemplate
- cloudformation:DescribeStackEvents
Effect: Allow
Resource: "*"
- PolicyName: CdkDeploy
PolicyDocument:
Statement:
# In addition to CloudFormationAccess
- Action:
- cloudformation:CreateChangeSet
- cloudformation:DescribeChangeSet
- cloudformation:ExecuteChangeSet
- ecr:CreateRepository
- ecr:SetRepositoryPolicy
- ecr:DescribeRepositories
Effect: Allow
Resource: "*"
- PolicyName: LogsAccess
PolicyDocument:
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
- PolicyName: IAMAccess
PolicyDocument:
Statement:
- Effect: Allow
Action:
- iam:GetRolePolicy
- iam:CreateRole
- iam:DeleteRole
- iam:CreatePolicy
- iam:GetPolicy
- iam:DeletePolicy
- iam:PutRolePolicy
- iam:DeleteRolePolicy
- iam:AttachRolePolicy
- iam:DetachRolePolicy
- iam:UpdateAssumeRolePolicy
- iam:ListPolicyVersions
- iam:PassRole
- iam:GetRole
- iam:GetInstanceProfile
- iam:CreateInstanceProfile
- iam:DeleteInstanceProfile
- iam:AddRoleToInstanceProfile
- iam:RemoveRoleFromInstanceProfile
- iam:CreateServiceLinkedRole
- iam:TagRole
Resource:
- "*"
- PolicyName: CloudFrontAccess
PolicyDocument:
Statement:
- Effect: Allow
Action:
- cloudfront:CreateCloudFrontOriginAccessIdentity
- cloudfront:CreateDistribution
- cloudfront:TagResource
- cloudfront:GetDistribution
- cloudfront:CreateInvalidation
Resource: "*"
- PolicyName: APIGateway
PolicyDocument:
Statement:
- Effect: Allow
Action:
- apigateway:GET
- apigateway:POST
- apigateway:PUT
- apigateway:PATCH
- apigateway:DELETE
Resource: "*"
- PolicyName: DynamodbPermissions
PolicyDocument:
Statement:
- Effect: Allow
Action:
- dynamodb:DescribeTable
- dynamodb:CreateTable
- dynamodb:DescribeTimeToLive
- dynamodb:UpdateTimeToLive
- dynamodb:DeleteTable
- dynamodb:TagResource
Resource: "*"
- PolicyName: CloudwatchLogsPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- logs:DeleteLogGroup
- lambda:GetFunction
- lambda:CreateFunction
- lambda:GetFunctionConfiguration
Resource: "*"
- PolicyName: S3Access
PolicyDocument:
Statement:
- Action:
- s3:GetObject
- s3:PutObject
- s3:DeleteObject
- s3:ListBucket
- s3:CreateBucket
- S3:DeleteBucket
- s3:GetEncryptionConfiguration
- s3:PutEncryptionConfiguration
- s3:PutBucketPublicAccessBlock
- s3:PutBucketLogging
- s3:GetBucketLocation
- s3:PutBucketAcl
- s3:PutObjectAcl
- s3:HeadObject
- s3:PutBucketWebsite
- s3:PutBucketPolicy
- s3:GetBucketAcl
- s3:DeleteBucketPolicy
- s3:PutBucketCORS
- s3:PutBucketVersioning
- s3:PutBucketOwnershipControls
Effect: Allow
Resource: "*"
- PolicyName: CloudwatchPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- events:PutRule
- events:DescribeRule
- events:PutTargets
- events:RemoveTargets
- events:DeleteRule
Resource: "*"
- PolicyName: LambdaPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- lambda:AddPermission
- lambda:InvokeFunction
- lambda:RemovePermission
- lambda:DeleteFunction
- lambda:UpdateFunctionCode
# Needed for subsequent updates to deployment
- lambda:UpdateFunctionConfiguration
- lambda:ListTags
Resource: "*"
- PolicyName: SMWorkflowPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- states:CreateStateMachine
- states:TagResource
- states:DeleteStateMachine
- states:DescribeStateMachine
Resource: "*"
- PolicyName: SSMPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- ssm:PutParameter
- ssm:AddTagsToResource
- ssm:DeleteParameter
- ssm:GetParameters
Resource: "*"
- PolicyName: SNSPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- sns:GetTopicAttributes
- sns:CreateTopic
- sns:TagResource
- sns:DeleteTopic
Resource: "*"
- PolicyName: KMSPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- kms:TagResource
- kms:CreateKey
- kms:Create*
- kms:Describe*
- kms:Enable*
- kms:List*
- kms:Put*
- kms:Update*
- kms:Revoke*
- kms:Disable*
- kms:Get*
- kms:Delete*
- kms:ScheduleKeyDeletion
- kms:CancelKeyDeletion
- kms:Encrypt
- kms:Decrypt
- kms:ReEncrypt
- kms:GenerateDataKey*
- kms:DescribeKey
Resource: "*"
- PolicyName: EC2Policy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- ec2:CopyImage
- ec2:CreateImage
- ec2:CreateSnapshot
- ec2:CreateTags
- ec2:CreateVolume
- ec2:DeleteSnapshot
- ec2:DeregisterImage
- ec2:DescribeImageAttribute
- ec2:DescribeImages
- ec2:DescribeInstances
- ec2:DescribeInstanceStatus
- ec2:DescribeSnapshots
- ec2:DescribeTags
- ec2:ModifyImageAttribute
- ec2:ModifyInstanceAttribute
- ec2:ModifySnapshotAttribute
- ec2:RegisterImage
- ec2:RunInstances
- ec2:StopInstances
- ec2:TerminateInstances
- ec2:CreateKeypair
- ec2:CreateSecurityGroup
- ec2:AuthorizeSecurityGroupIngress
- ec2:DescribeSecurityGroups
- ec2:DeleteSecurityGroup
- ec2:DeleteKeyPair
- ec2:DeleteVolume
- ec2:AttachVolume
- ec2:DetachVolume
- ec2:RevokeSecurityGroup*
- ec2:AuthorizeSecurityGroup*
- ec2:DescribeSubnets
- ec2:DescribeVolumes
- ec2:CreateDefaultVpc
- ec2:DescribeRegions
Resource: "*"
- PolicyName: CDKAccessPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action: sts:AssumeRole
Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/cdk-*"
Outputs:
InstanceId:
Description: TRE Deployment Instance ID
Value: !Ref Instance