-
Notifications
You must be signed in to change notification settings - Fork 419
/
petstore-api-pipeline.yaml
399 lines (383 loc) · 11.3 KB
/
petstore-api-pipeline.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
AWSTemplateFormatVersion: "2010-09-09"
Description: Code pipeline for automated api functional testing
Parameters:
GitHubRepositoryName:
Type: String
Description: GitHub repository name.
MinLength: 1
MaxLength: 100
GitHubUser:
Type: String
GitHubBranch:
Type: String
GitHubToken:
Type: String
Description: repository token.
NoEcho: true
BucketRoot:
Type: String
Description: bucket containing project artifacts e.g. postman config files, etc.
MinLength: 1
MaxLength: 100
Resources:
CodePipelineTrustRole:
Type: "AWS::IAM::Role"
Properties:
RoleName:
Fn::Sub: ${AWS::StackName}-CodePipelineRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "codepipeline.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: /
#TODO: scope resources
Policies:
-
PolicyName:
Fn::Sub: ${AWS::StackName}-CodePipelineRolePolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "s3:DeleteObject"
- "s3:GetObject"
- "s3:GetObjectVersion"
- "s3:ListBucket"
- "s3:PutObject"
- "s3:GetBucketPolicy"
Resource: '*'
-
Effect: "Allow"
Action:
- "codebuild:StartBuild"
- "codebuild:BatchGetBuilds"
Resource: '*'
-
Effect: "Allow"
Action:
- "cloudformation:DescribeStacks"
- "cloudformation:DescribeChangeSet"
- "cloudformation:CreateStack"
- "cloudformation:DeleteStack"
- "cloudformation:UpdateStack"
- "cloudformation:CreateChangeSet"
- "cloudformation:DeleteChangeSet"
- "cloudformation:ExecuteChangeSet"
- "cloudformation:SetStackPolicy"
- "cloudformation:ValidateTemplate"
- "iam:PassRole"
Resource: '*'
CodeBuildServiceRole:
Type: AWS::IAM::Role
Properties:
RoleName:
Fn::Sub: ${AWS::StackName}-CodeBuildServiceRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "codebuild.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: /service-role/
Policies:
-
PolicyName:
Fn::Sub: ${AWS::StackName}-CodeBuildServiceRole-Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "codebuild:CreateReportGroup"
- "codebuild:CreateReport"
- "codebuild:UpdateReport"
- "codebuild:BatchPutTestCases"
Resource:
- "*"
- Effect: "Allow"
Action:
- "cloudformation:DescribeStacks"
Resource:
- "*"
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource:
- Fn::Sub: arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*
-
Effect: "Allow"
Action:
- "s3:PutObject"
- "s3:GetObject"
- "s3:GetObjectVersion"
- "s3:ListBucket"
Resource:
- Fn::Sub: arn:aws:s3:::${AWS::StackName}-codepipeline-artifact-bucket-${AWS::AccountId}/*
- Fn::Sub: arn:aws:s3:::${AWS::StackName}-codepipeline-artifact-bucket-${AWS::AccountId}
- Fn::Sub: arn:aws:s3:::${BucketRoot}/*
- Fn::Sub: arn:aws:s3:::${BucketRoot}
-
Effect: "Allow"
Action:
- "iam:PassRole"
Resource:
- "*"
CloudFormationTrustRole:
Type: 'AWS::IAM::Role'
Description: Creating service role in IAM for AWS CloudFormation
Properties:
Path: /
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/AdministratorAccess'
RoleName:
Fn::Sub: ${AWS::StackName}-CloudFormationTrustRole
AssumeRolePolicyDocument:
Statement:
- Action: 'sts:AssumeRole'
Effect: Allow
Principal:
Service:
- cloudformation.amazonaws.com
CodePipelineArtifactBucket:
DeletionPolicy: Delete
Type: 'AWS::S3::Bucket'
Description: S3 bucket for pipeline artifacts
Properties:
AccessControl: Private
BucketName:
Fn::Sub: ${AWS::StackName}-codepipeline-artifact-bucket-${AWS::AccountId}
VersioningConfiguration:
Status: Enabled
Tags:
- Key: Name
Value:
Fn::Sub: ${AWS::StackName}-codepipeline-artifact-bucket
#code build - api deployment using SAM (see buildspec.yml)
PetStoreAPISAMCodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name: petstore-api-stack-project
Description: CodeBuild Project using AWS SAM to deploy PetStore API
ServiceRole:
Fn::GetAtt: [ CodeBuildServiceRole, Arn ]
Artifacts:
Type: S3
Location:
Ref: CodePipelineArtifactBucket
Name:
Fn::Sub: ${AWS::StackName}-petstore-api-stack-codebuild-project
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/nodejs:8.11.0
Source:
Location: !Join
- ''
- - 'https://github.com/'
- !Ref GitHubUser
- '/'
- !Ref GitHubRepositoryName
Type: GITHUB
BuildSpec: 01api/petstore-api-buildspec.yml
TimeoutInMinutes: 10
Tags:
- Key: Name
Value:
Fn::Sub: ${AWS::StackName}-petstore-api-stack-project
#code build - api testing using Newman to execute Postman test def (see buildspec.yml)
PostmanNewmanCodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name:
Fn::Sub: ${AWS::StackName}-newman-project
Description: CodeBuild Project using newman cli to execute postman tests
ServiceRole:
Fn::GetAtt: [ CodeBuildServiceRole, Arn ]
Artifacts:
Type: S3
Location:
Ref: CodePipelineArtifactBucket
Name:
Fn::Sub: ${AWS::StackName}-newman-project
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/amazonlinux2-x86_64-standard:2.0
EnvironmentVariables:
- Name: S3_BUCKET
Type: PLAINTEXT
Value:
Fn::Sub: ${BucketRoot}
Source:
Location: !Join
- ''
- - 'https://github.com/'
- !Ref GitHubUser
- '/'
- !Ref GitHubRepositoryName
Type: GITHUB
BuildSpec: 03codebuild/postman-newman-buildspec.yml
TimeoutInMinutes: 10
Tags:
- Key: Name
Value:
Fn::Sub: ${AWS::StackName}-newman-project
PetStoreAPIPipeline:
Type: "AWS::CodePipeline::Pipeline"
DependsOn:
- PetStoreAPISAMCodeBuildProject
Properties:
Name: petstore-api-pipeline
RoleArn:
Fn::GetAtt: [ CodePipelineTrustRole, Arn ]
ArtifactStore:
Type: S3
Location: !Ref CodePipelineArtifactBucket
Stages:
- Name: Source
Actions:
- Name: CheckoutPetStoreAPISource
ActionTypeId:
Owner: ThirdParty
Category: Source
Version: 1
Provider: GitHub
Configuration:
Owner: !Ref GitHubUser
PollForSourceChanges: false
Repo: !Ref GitHubRepositoryName
Branch: !Ref GitHubBranch
OAuthToken: !Ref GitHubToken
InputArtifacts: []
OutputArtifacts:
- Name: 'PetStoreAPI-SourceArtifact'
RunOrder: 1
- Name: Build
Actions:
- Name: BuildPetStoreAPI
ActionTypeId:
Owner: AWS
Category: Build
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref PetStoreAPISAMCodeBuildProject
InputArtifacts:
- Name: 'PetStoreAPI-SourceArtifact'
OutputArtifacts:
- Name: 'PetStoreAPI-BuildArtifact'
RunOrder: 1
- Name: Deploy
Actions:
- Name: GenerateChangeSet
ActionTypeId:
Owner: AWS
Category: Deploy
Version: 1
Provider: CloudFormation
Configuration:
TemplatePath: !Join
- ''
- - 'PetStoreAPI-BuildArtifact'
- '::01api/petstore-api-output.yaml'
ActionMode: CHANGE_SET_REPLACE
Capabilities: CAPABILITY_IAM
ChangeSetName: pipeline-changeset
RoleArn: !GetAtt
- CloudFormationTrustRole
- Arn
StackName: petstore-api-stack
InputArtifacts:
- Name: 'PetStoreAPI-BuildArtifact'
OutputArtifacts: []
RunOrder: 1
- Name: ExecuteChangeSet
ActionTypeId:
Owner: AWS
Category: Deploy
Version: 1
Provider: CloudFormation
Configuration:
ActionMode: CHANGE_SET_EXECUTE
ChangeSetName: pipeline-changeset
StackName: petstore-api-stack
InputArtifacts: []
OutputArtifacts: []
RunOrder: 2
# https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html
- Name: Test
Actions:
- Name: TestPetStoreAPI
ActionTypeId:
Owner: AWS
Category: Build
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref PostmanNewmanCodeBuildProject
InputArtifacts:
- Name: 'PetStoreAPI-SourceArtifact'
OutputArtifacts:
- Name: 'PetStoreAPI-TestArtifact'
RunOrder: 1
PetStorePostmanPipelineRunnerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 'sts:AssumeRole'
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Path: /
Policies:
-
PolicyName: "root"
PolicyDocument:
Version: '2012-10-17' # Policy Document
Statement:
-
Effect: Allow
Action:
- s3:PutObject
- s3:PutObjectAcl
- s3:GetObject
- s3:GetObjectVersion
Resource:
- Fn::Sub: arn:aws:s3:::${BucketRoot}/*
-
Effect: Allow
Action:
- codepipeline:PutJobSuccessResult
- codepipeline:PutJobSuccessFailure
Resource: "*"
-
Effect: Allow
Action:
- cloudformation:ListExports
Resource: "*"
-
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: 'arn:aws:logs:*:*:*'