/
Create_CodePipeline.yaml
269 lines (248 loc) · 8.39 KB
/
Create_CodePipeline.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
AWSTemplateFormatVersion: 2010-09-09
Description: Create a 3-phase CodePipeline with source, build and deploy stages
# The first stage involves getting the application source and deployment configuration from GitHub.
# The second stage involves building the docker image from the application source, and pushing it to the Docker Hub image repository.
# The final stage involves updating the ECS task definition and creating replacement tasks using blue/green deployment.
Parameters:
CodeBuildProjectStackTemplateURL:
Description: CodeBuild project stack template URL
Type: String
CodeBuildProjectName:
Description: CodeBuild project name
Type: String
GitHubApplicationRepoURL:
Description: GitHub repository URL of the application
Type: String
CodeDeployApplicationName:
Description: CodeDeploy application name
Type: String
CodeDeployDeploymentGroupName:
Description: CodeDeploy deployment group Name
Type: String
CodePipelineName:
Description: CodePipeline name
Type: String
CodePipelineArtifactStoreBucketName:
Description: CodePipeline artifact store bucket name
Type: String
GitHubApplicationOwner:
Description: GitHub owner of the application repository
Type: String
GitHubApplicationRepo:
Description: GitHub repository of the application
Type: String
GitHubApplicationBranch:
Description: GitHub branch of the application repository
Type: String
GitHubCodeDeployConfigOwner:
Description: GitHub owner of the CodeDeploy configuration repository
Type: String
GitHubCodeDeployConfigRepo:
Description: GitHub repository of the CodeDeploy configuration
Type: String
GitHubCodeDeployConfigBranch:
Description: GitHub branch of the CodeDeplpy configuration repository
Type: String
RegionName:
Description: Region name
Type: String
Default: us-east-1
Resources:
CodeBuildProjectStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: !Ref CodeBuildProjectStackTemplateURL
Parameters:
CodeBuildProjectName: !Ref CodeBuildProjectName
GitHubApplicationRepoURL: !Ref GitHubApplicationRepoURL
CodePipelineArtifactStoreBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref CodePipelineArtifactStoreBucketName
CodePipelineArtifactStoreBucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: !Ref CodePipelineArtifactStoreBucket
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: DenyUnEncryptedObjectUploads
Effect: Deny
Principal: '*'
Action: 's3:PutObject'
Resource: !Join
- ''
- - !GetAtt
- CodePipelineArtifactStoreBucket
- Arn
- /*
Condition:
StringNotEquals:
's3:x-amz-server-side-encryption': 'aws:kms'
- Sid: DenyInsecureConnections
Effect: Deny
Principal: '*'
Action: 's3:*'
Resource: !Join
- ''
- - !GetAtt
- CodePipelineArtifactStoreBucket
- Arn
- /*
Condition:
Bool:
'aws:SecureTransport': false
CodePipelineServiceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- codepipeline.amazonaws.com
Action: 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: AWS-CodePipeline-Service-3
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'codecommit:CancelUploadArchive'
- 'codecommit:GetBranch'
- 'codecommit:GetCommit'
- 'codecommit:GetUploadArchiveStatus'
- 'codecommit:UploadArchive'
Resource: '*'
- Effect: Allow
Action:
- 'codedeploy:CreateDeployment'
- 'codedeploy:GetApplication'
- 'codedeploy:GetApplicationRevision'
- 'codedeploy:GetDeployment'
- 'codedeploy:GetDeploymentConfig'
- 'codedeploy:RegisterApplicationRevision'
Resource: '*'
- Effect: Allow
Action:
- 'codebuild:BatchGetBuilds'
- 'codebuild:StartBuild'
Resource: '*'
- Effect: Allow
Action:
- 'devicefarm:ListProjects'
- 'devicefarm:ListDevicePools'
- 'devicefarm:GetRun'
- 'devicefarm:GetUpload'
- 'devicefarm:CreateUpload'
- 'devicefarm:ScheduleRun'
Resource: '*'
- Effect: Allow
Action:
- 'lambda:InvokeFunction'
- 'lambda:ListFunctions'
Resource: '*'
- Effect: Allow
Action:
- 'iam:PassRole'
Resource: '*'
- Effect: Allow
Action:
- 'elasticbeanstalk:*'
- 'ec2:*'
- 'elasticloadbalancing:*'
- 'autoscaling:*'
- 'cloudwatch:*'
- 's3:*'
- 'sns:*'
- 'cloudformation:*'
- 'rds:*'
- 'sqs:*'
- 'ecs:*'
Resource: '*'
CodePipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
Name: !Ref CodePipelineName
ArtifactStore:
Type: S3
Location: !Ref CodePipelineArtifactStoreBucketName
RestartExecutionOnUpdate: true
RoleArn: !GetAtt CodePipelineServiceRole.Arn
Stages:
- Name: Source
Actions:
- Name: Source
InputArtifacts: []
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: 1
Provider: GitHub
OutputArtifacts:
- Name: SourceArtifact
Configuration:
Owner: !Ref GitHubApplicationOwner
Repo: !Ref GitHubApplicationRepo
Branch: !Ref GitHubApplicationBranch
PollForSourceChanges: false
OAuthToken: '{{resolve:secretsmanager:github:SecretString:token}}'
RunOrder: 1
- Name: GetCodeDeployConfig
InputArtifacts: []
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: 1
Provider: GitHub
OutputArtifacts:
- Name: CodeDeployConfigArtifact
Configuration:
Owner: !Ref GitHubCodeDeployConfigOwner
Repo: !Ref GitHubCodeDeployConfigRepo
Branch: !Ref GitHubCodeDeployConfigBranch
PollForSourceChanges: false
OAuthToken: '{{resolve:secretsmanager:github:SecretString:token}}'
RunOrder: 1
- Name: Build
Actions:
- Name: Build
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: '1'
InputArtifacts:
- Name: SourceArtifact
OutputArtifacts:
- Name: BuildArtifact
Configuration:
ProjectName: !Ref CodeBuildProjectName
RunOrder: 2
- Name: Deploy
Actions:
- Name: Deploy
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CodeDeployToECS
Version: '1'
InputArtifacts:
- Name: BuildArtifact
- Name: CodeDeployConfigArtifact
OutputArtifacts: []
Configuration:
ApplicationName: !Ref CodeDeployApplicationName
DeploymentGroupName: !Ref CodeDeployDeploymentGroupName
AppSpecTemplateArtifact: CodeDeployConfigArtifact
AppSpecTemplatePath: appspec.yaml
TaskDefinitionTemplateArtifact: CodeDeployConfigArtifact
TaskDefinitionTemplatePath: taskdef.json
Image1ArtifactName: BuildArtifact
Image1ContainerName: IMAGE
Region: !Ref RegionName
Namespace: DeployVariables
RunOrder: 3