-
Notifications
You must be signed in to change notification settings - Fork 28
/
cloudformation.yaml
546 lines (531 loc) · 17.3 KB
/
cloudformation.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
534
535
536
537
538
539
540
541
542
543
544
545
546
AWSTemplateFormatVersion: "2010-09-09"
Description: Change Set CodeDeploy Pipeline
Parameters:
ApplicationStackName:
Description: Name of the not yet created Application CFN Stack
Type: String
Default: compose-application
AllowedPattern: (^[a-z][a-z0-9-]+$)
ChangeSetName:
Description: Name of Cfn Change Set
Type: String
Default: compose-demo
AllowedPattern: (^[a-z][a-z0-9-]+$)
ExistingAwsVpc:
Description: Name of existing AWS Vpc
Type: AWS::EC2::VPC::Id
ExistingLoadbalancer:
Description: ARN of existing Application Loadbalancer
Type: String
ExistingEcsCluster:
Description: Name of existing Ecs Cluster
Type: String
Resources:
DemoAppEcr:
Type: AWS::ECR::Repository
Properties:
RepositoryName: !Join
- "-"
- - !Sub "${AWS::StackName}"
- "demo-app"
SourceBucket:
Type: "AWS::S3::Bucket"
Properties:
VersioningConfiguration:
Status: Enabled
PipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: codepipeline.amazonaws.com
Version: "2012-10-17"
PipelineRoleDefaultPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: PipelineRoleDefaultPolicy
Roles:
- Ref: PipelineRole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- s3:GetObject*
- s3:GetBucket*
- s3:List*
- s3:DeleteObject*
- s3:PutObject*
- s3:Abort*
Effect: Allow
Resource:
- Fn::GetAtt:
- SourceBucket
- Arn
- Fn::Join:
- ""
- - Fn::GetAtt:
- SourceBucket
- Arn
- /*
- Action:
- codebuild:StartBuild
- codebuild:BatchGetBuilds
Effect: Allow
Resource:
- Fn::GetAtt:
- ExtractBuild
- Arn
- Fn::GetAtt:
- ImageBuild
- Arn
- Action:
- cloudformation:CreateChangeSet
- cloudformation:DescribeStacks
- cloudformation:DescribeChangeSet
- cloudformation:ExecuteChangeSet
Effect: Allow
Resource:
- !Join
- ""
- - "arn:aws:cloudformation:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- ":stack/"
- Ref: ApplicationStackName
- "/*"
- Action:
- iam:PassRole
Effect: Allow
Resource:
- !GetAtt [ExtractBuildRole, Arn]
ImageBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Version: "2012-10-17"
ImageBuildRolePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: ImageBuildRoleDefaultPolicy
Roles:
- Ref: ImageBuildRole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource:
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":logs:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :log-group:/aws/codebuild/
- Ref: ImageBuild
- :*
- Action:
- s3:GetObject*
- s3:GetBucket*
- s3:List*
- s3:PutObject*
Effect: Allow
Resource:
- Fn::GetAtt:
- SourceBucket
- Arn
- Fn::Join:
- ""
- - Fn::GetAtt:
- SourceBucket
- Arn
- /*
- Action:
- ecr:GetAuthorizationToken
Effect: Allow
Resource:
- "*"
- Action:
- ecr:BatchCheckLayerAvailability
- ecr:CompleteLayerUpload
- ecr:InitiateLayerUpload
- ecr:PutImage
- ecr:UploadLayerPart
Effect: Allow
Resource:
- !GetAtt DemoAppEcr.Arn
ExtractBuildRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: cloudformation.amazonaws.com
Version: "2012-10-17"
ExtractBuildRoleDefaultPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: ExtractBuildRoleDefaultPolicy
Roles:
- Ref: ExtractBuildRole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource:
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":logs:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :log-group:/aws/codebuild/
- Ref: ExtractBuild
- :*
- Action:
- s3:GetObject*
- s3:GetBucket*
- s3:List*
- s3:PutObject*
Effect: Allow
Resource:
- Fn::GetAtt:
- SourceBucket
- Arn
- Fn::Join:
- ""
- - Fn::GetAtt:
- SourceBucket
- Arn
- /*
ComposeRolePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: ComposeRolePolicy
Roles:
- Ref: ExtractBuildRole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- cloudformation:*
- ecs:ListAccountSettings
- ecs:CreateCluster
- ecs:CreateService
- ecs:DeleteCluster
- ecs:DeleteService
- ecs:DeregisterTaskDefinition
- ecs:DescribeClusters
- ecs:DescribeServices
- ecs:DescribeTasks
- ecs:ListTasks
- ecs:RegisterTaskDefinition
- ecs:UpdateService
- ec2:AuthorizeSecurityGroupIngress
- ec2:DescribeVpcs
- ec2:DescribeVpcAttribute
- ec2:DescribeSubnets
- ec2:DescribeRouteTables
- ec2:CreateSecurityGroup
- ec2:CreateTags
- ec2:DescribeSecurityGroups
- ec2:DeleteSecurityGroup
- ec2:RevokeSecurityGroupIngress
- elasticfilesystem:CreateAccessPoint
- elasticfilesystem:CreateFileSystem
- elasticfilesystem:CreateMountTarget
- elasticfilesystem:DeleteAccessPoint
- elasticfilesystem:DeleteFileSystem
- elasticfilesystem:DeleteMountTarget
- elasticfilesystem:DescribeAccessPoints
- elasticfilesystem:DescribeBackupPolicy
- elasticfilesystem:DescribeFileSystemPolicy
- elasticfilesystem:DescribeFileSystems
- elasticfilesystem:DescribeLifecycleConfiguration
- elasticfilesystem:DescribeMountTargets
- elasticfilesystem:ModifyMountTarget
- elasticfilesystem:ModifyMountTargetSecurityGroups
- iam:AttachRolePolicy
- iam:CreateRole
- iam:DeleteRole
- iam:DeleteRolePolicy
- iam:DetachRolePolicy
- iam:PassRole
- iam:PutRolePolicy
- elasticloadbalancing:*
- application-autoscaling:*
- servicediscovery:*
- logs:CreateLogGroup
- logs:DescribeLogGroups
- logs:FilterLogEvents
- logs:DeleteLogGroup
- route53:CreateHostedZone
- route53:DeleteHostedZone
- route53:GetHealthCheck
- route53:GetHostedZone
- route53:ListHostedZonesByName
Effect: Allow
Resource:
- "*"
# CodeBuild to Build the Container Image
ImageBuild:
Type: AWS::CodeBuild::Project
Properties:
Name: !Join
- "-"
- - !Sub ${AWS::StackName}
- "ImageBuild"
Artifacts:
Type: CODEPIPELINE
EncryptionDisabled: false
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:5.0
PrivilegedMode: true
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: AWS_ACCOUNT_ID
Type: PLAINTEXT
Value: !Ref AWS::AccountId
ServiceRole: !Ref ImageBuildRole
Source:
Type: CODEPIPELINE
BuildSpec: |
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
build:
commands:
- echo Building the Docker image...
- cd frontend/
- docker build -t $IMAGE_URI:$IMAGE_TAG .
post_build:
commands:
- echo Pushing the Docker image...
- docker push $IMAGE_URI:$IMAGE_TAG
# Code Build to Extract Cfn
ExtractBuild:
Type: AWS::CodeBuild::Project
Properties:
Name: !Join
- "-"
- - !Sub ${AWS::StackName}
- "ExtractBuild"
Artifacts:
Type: CODEPIPELINE
EncryptionDisabled: false
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/amazonlinux2-x86_64-standard:3.0
PrivilegedMode: true
Type: LINUX_CONTAINER
ServiceRole: !Ref ExtractBuildRole
Source:
Type: CODEPIPELINE
BuildSpec: |
version: 0.2
phases:
install:
commands:
- mv /usr/local/bin/docker /usr/bin/docker
- curl -L https://raw.githubusercontent.com/docker/compose-cli/main/scripts/install/install_linux.sh | sh
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
- echo Creating Docker Compose Context
- curl "http://169.254.170.2${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}" > creds.json
- export AWS_ACCESS_KEY_ID=$(cat creds.json | jq -r .AccessKeyId)
- export AWS_SECRET_ACCESS_KEY=$(cat creds.json | jq -r .SecretAccessKey)
- export AWS_SESSION_TOKEN=$(cat creds.json | jq -r .Token)
- docker context create ecs demoecs --from-env
- docker context use demoecs
build:
commands:
- echo Convert Compose File
- docker --debug compose convert > cloudformation.yml
artifacts:
files:
- cloudformation.yml
# Code Pipeline
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
ArtifactStore:
Type: S3
Location: !Ref "SourceBucket"
RoleArn:
Fn::GetAtt:
- PipelineRole
- Arn
Stages:
- Name: S3Source
Actions:
- Name: TemplateSource
ActionTypeId:
Category: Source
Owner: AWS
Provider: S3
Version: "1"
Configuration:
S3Bucket: !Ref "SourceBucket"
S3ObjectKey: "compose-bundle.zip"
OutputArtifacts:
- Name: Source
- Name: Build
Actions:
- Name: BuildContainerImage
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: "1"
Configuration:
ProjectName: !Ref ImageBuild
EnvironmentVariables: !Sub |
[
{
"name": "AWS_ACCOUNT_ID",
"value": "${AWS::AccountId}",
"type": "PLAINTEXT"
},
{
"name": "IMAGE_URI",
"value": "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${DemoAppEcr}",
"type": "PLAINTEXT"
},
{
"name": "IMAGE_TAG",
"value": "#{codepipeline.PipelineExecutionId}",
"type": "PLAINTEXT"
}
]
InputArtifacts:
- Name: Source
OutputArtifacts:
- Name: ImageBuild
- Name: Compose2Cloudformation
Actions:
- Name: ExtractCFN
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: "1"
Configuration:
ProjectName: !Ref ExtractBuild
EnvironmentVariables: !Sub |
[
{
"name": "AWS_ACCOUNT_ID",
"value": "${AWS::AccountId}",
"type": "PLAINTEXT"
},
{
"name": "IMAGE_URI",
"value": "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${DemoAppEcr}",
"type": "PLAINTEXT"
},
{
"name": "IMAGE_TAG",
"value": "#{codepipeline.PipelineExecutionId}",
"type": "PLAINTEXT"
},
{
"name": "AWS_ECS_CLUSTER",
"value": "${ExistingEcsCluster}",
"type": "PLAINTEXT"
},
{
"name": "AWS_VPC",
"value": "${ExistingAwsVpc}",
"type": "PLAINTEXT"
},
{
"name": "AWS_ELB",
"value": "${ExistingLoadbalancer}",
"type": "PLAINTEXT"
}
]
InputArtifacts:
- Name: Source
OutputArtifacts:
- Name: ExtractedCfn
- Name: DeployStage
Actions:
- Name: CreateChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: "1"
InputArtifacts:
- Name: ExtractedCfn
Configuration:
ActionMode: CHANGE_SET_REPLACE
RoleArn: !GetAtt [ExtractBuildRole, Arn]
StackName: !Ref ApplicationStackName
ChangeSetName: !Ref ChangeSetName
TemplatePath: "ExtractedCfn::cloudformation.yml"
Capabilities: CAPABILITY_IAM
RunOrder: 1
- Name: ApproveChangeSet
ActionTypeId:
Category: Approval
Owner: AWS
Provider: Manual
Version: "1"
RunOrder: 2
- Name: ExecuteChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: "1"
Configuration:
ActionMode: CHANGE_SET_EXECUTE
StackName: !Ref ApplicationStackName
ChangeSetName: !Ref ChangeSetName
RoleArn: !GetAtt [ExtractBuildRole, Arn]
RunOrder: 3
Outputs:
DemoAppEcr:
Description: ECR Repository to store the Demo App Image
Value: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${DemoAppEcr}"
DemoAppEcrName:
Description: ECR Repository to store the Demo App Image
Value: !Ref DemoAppEcr
S3BucketName:
Description: S3 Bucket to store Application Source Code
Value: !Ref SourceBucket