/
service-pipeline.yml
326 lines (314 loc) · 10.4 KB
/
service-pipeline.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
AWSTemplateFormatVersion: "2010-09-09"
Description: "Sample Service Pipeline."
Parameters:
ProductName:
Type: String
Description: "Name of the product."
MinLength: 1
MaxLength: 10
AllowedPattern: "[A-Za-z0-9-]+"
ConstraintDescription: "Malformed input parameter. ProductName must only contain upper and lower case letters, numbers, and -."
Default: my
ProjectName:
Type: String
Description: "Name of the project."
MinLength: 1
MaxLength: 20
AllowedPattern: "[A-Za-z0-9-]+"
ConstraintDescription: "Malformed input parameter. ProjectName must only contain upper and lower case letters, numbers, and -."
Default: service-x
CodeCommitRepoName:
Type: String
Description: "The CodeCommit repo name"
Default: my-service-x
CodeCommitRepoBranch:
Type: String
Description: "The CodeCommit repo branch"
Default: master
Resources:
CodePipelineRole:
Type: AWS::IAM::Role
DependsOn: [S3ArtifactBucket, CloudFormationRole]
Properties:
RoleName: !Sub "${AWS::StackName}-codepipeline-role"
Path: /
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [codepipeline.amazonaws.com]
Action: sts:AssumeRole
Policies:
- PolicyName: !Sub "${AWS::StackName}-codepipeline-role-policy"
PolicyDocument:
Statement:
- Action: ["s3:*"]
Resource:
- !Sub "arn:aws:s3:::${S3ArtifactBucket}"
- !Sub "arn:aws:s3:::${S3ArtifactBucket}/*"
Effect: Allow
- Action: ["codecommit:*"]
Resource: !Sub "arn:aws:codecommit:${AWS::Region}:${AWS::AccountId}:${CodeCommitRepoName}"
Effect: Allow
- Action: ["codebuild:*"]
Resource: !Sub "arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/${AWS::StackName}*"
Effect: Allow
- Action: ["cloudformation:*"]
Resource: !Sub "arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:*"
Effect: Allow
- Action: ["iam:PassRole"]
Resource: !GetAtt CloudFormationRole.Arn
Effect: Allow
CloudFormationRole:
Type: AWS::IAM::Role
DependsOn: [S3ArtifactBucket]
Properties:
RoleName: !Sub "${AWS::StackName}-cloudformation-role"
Path: /
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [cloudformation.amazonaws.com]
Action: sts:AssumeRole
Policies:
- PolicyName: CloudFormationPolicy
PolicyDocument:
Statement:
- Action: ["cloudformation:*"]
Resource: !Sub "arn:aws:cloudformation:${AWS::Region}:aws:transform/*"
Effect: Allow
- Action: ["s3:*"]
Resource: !Sub "arn:aws:s3:::${S3ArtifactBucket}/*"
Effect: Allow
- Action: ["lambda:*"]
Resource: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:*"
Effect: Allow
- Action: ["iam:*"]
Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/*"
Effect: Allow
CodeBuildRole:
Type: AWS::IAM::Role
DependsOn: [S3ArtifactBucket]
Properties:
RoleName: !Sub "${AWS::StackName}-codebuild-role"
Path: /
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [codebuild.amazonaws.com]
Action: sts:AssumeRole
Policies:
- PolicyName: !Sub "${AWS::StackName}-codebuild-role-policy"
PolicyDocument:
Statement:
- Action: ["logs:*"]
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/${AWS::StackName}*"
Effect: Allow
- Action: ["s3:*"]
Resource: !Sub "arn:aws:s3:::${S3ArtifactBucket}/*"
Effect: Allow
S3ArtifactBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Delete
Properties:
BucketName: !Sub "${AWS::StackName}-${AWS::AccountId}-${AWS::Region}"
CodeBuild:
Type: AWS::CodeBuild::Project
DependsOn: [CodeBuildRole]
Properties:
Name: !Sub "${AWS::StackName}-codebuild"
Artifacts:
Type: CODEPIPELINE
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: "aws/codebuild/ubuntu-base:14.04"
Type: LINUX_CONTAINER
ServiceRole: !Ref CodeBuildRole
Source:
Type: CODEPIPELINE
BuildSpec: |
version: 0.2
phases:
build:
commands:
- echo "Building artifacts..."
artifacts:
files: ['*.js']
CodeBuildTest:
Type: AWS::CodeBuild::Project
DependsOn: [CodeBuildRole]
Properties:
Name: !Sub "${AWS::StackName}-test-codebuild"
Artifacts:
Type: CODEPIPELINE
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: "aws/codebuild/ubuntu-base:14.04"
Type: LINUX_CONTAINER
ServiceRole: !Ref CodeBuildRole
Source:
Type: CODEPIPELINE
BuildSpec: |
version: 0.2
phases:
build:
commands:
- echo "Integration testing..."
CodePipeline:
Type: AWS::CodePipeline::Pipeline
DependsOn: [CodePipelineRole, S3ArtifactBucket, CodeBuild, CodeBuildTest]
Properties:
Name: !Sub "${ProductName}-${ProjectName}-pipeline"
RoleArn: !GetAtt CodePipelineRole.Arn
RestartExecutionOnUpdate: true
ArtifactStore:
Type: S3
Location: !Ref S3ArtifactBucket
Stages:
- Name: Source
Actions:
- Name: !Sub "${CodeCommitRepoName}@${CodeCommitRepoBranch}"
InputArtifacts: []
OutputArtifacts:
- Name: SourceArtifact
ActionTypeId:
Category: Source
Owner: AWS
Provider: CodeCommit
Version: 1
Configuration:
RepositoryName: !Ref CodeCommitRepoName
BranchName: !Ref CodeCommitRepoBranch
PollForSourceChanges: true
RunOrder: 1
- Name: Building
Actions:
- Name: build
InputArtifacts:
- Name: SourceArtifact
OutputArtifacts:
- Name: BuildArtifact
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref CodeBuild
RunOrder: 1
- Name: StagingDEV
Actions:
- Name: stage
InputArtifacts:
- Name: SourceArtifact
- Name: BuildArtifact
OutputArtifacts: []
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: CloudFormation
Configuration:
StackName: !Sub "${ProductName}-${ProjectName}-dev"
ChangeSetName: !Sub "${ProductName}-${ProjectName}-dev-changeset"
RoleArn: !GetAtt CloudFormationRole.Arn
ActionMode: CREATE_UPDATE
Capabilities: CAPABILITY_NAMED_IAM
TemplatePath: !Sub "SourceArtifact::stack.yml"
OutputFileName: "dev-output.json"
ParameterOverrides: !Sub |
{
"Stage" : "dev",
"ArtifactS3Bucket" : "${S3ArtifactBucket}",
"LambdaS3Key" : { "Fn::GetArtifactAtt" : ["BuildArtifact", "ObjectKey"] }
}
RunOrder: 1
- Name: test
InputArtifacts:
- Name: SourceArtifact
ActionTypeId:
Category: Test
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref CodeBuildTest
RunOrder: 2
- Name: approve
InputArtifacts: []
OutputArtifacts: []
ActionTypeId:
Category: Approval
Owner: AWS
Version: 1
Provider: Manual
Configuration:
CustomData: "Approval to deploy to the QA stage."
RunOrder: 3
- Name: StagingQA
Actions:
- Name: stage
InputArtifacts:
- Name: SourceArtifact
- Name: BuildArtifact
OutputArtifacts: []
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: CloudFormation
Configuration:
StackName: !Sub "${ProductName}-${ProjectName}-qa"
ChangeSetName: !Sub "${ProductName}-${ProjectName}-qa-changeset"
RoleArn: !GetAtt CloudFormationRole.Arn
ActionMode: CREATE_UPDATE
Capabilities: CAPABILITY_NAMED_IAM
TemplatePath: !Sub "SourceArtifact::stack.yml"
OutputFileName: "qa-output.json"
ParameterOverrides: !Sub |
{
"Stage" : "qa",
"ArtifactS3Bucket" : "${S3ArtifactBucket}",
"LambdaS3Key" : { "Fn::GetArtifactAtt" : ["BuildArtifact", "ObjectKey"] }
}
RunOrder: 1
- Name: approve
InputArtifacts: []
OutputArtifacts: []
ActionTypeId:
Category: Approval
Owner: AWS
Version: 1
Provider: Manual
Configuration:
CustomData: "Approval to deploy to the PROD stage."
RunOrder: 2
- Name: StagingPROD
Actions:
- Name: stage
InputArtifacts:
- Name: SourceArtifact
- Name: BuildArtifact
OutputArtifacts: []
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: CloudFormation
Configuration:
StackName: !Sub "${ProductName}-${ProjectName}-prod"
ChangeSetName: !Sub "${ProductName}-${ProjectName}-prod-changeset"
RoleArn: !GetAtt CloudFormationRole.Arn
ActionMode: CREATE_UPDATE
Capabilities: CAPABILITY_NAMED_IAM
TemplatePath: !Sub "SourceArtifact::stack.yml"
OutputFileName: "prod-output.json"
ParameterOverrides: !Sub |
{
"Stage" : "prod",
"ArtifactS3Bucket" : "${S3ArtifactBucket}",
"LambdaS3Key" : { "Fn::GetArtifactAtt" : ["BuildArtifact", "ObjectKey"] }
}
RunOrder: 1