-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.yml
494 lines (481 loc) · 16.2 KB
/
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
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
AWSTemplateFormatVersion: 2010-09-09
Description: Continuous Deployment setup for the interLibraryLoanMetadata stack/function
# Name:
# Recommended: [DeployStackName]-pipeline
# eg: interLibraryLoanMetadata-api-pipeline
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Git Settings
Parameters:
- GitRepo
- GitBranch
- Label:
default: Stack Settings
Parameters:
- StackTemplatePath
- StackTemplateConfigurationPath
- DeployStackName
- Label:
default: Pipeline approval
Parameters:
- PipelineApprovalAction
- PipelineApprovalEmail
- Label:
default: Alert Configuration
Parameters:
- AlertTopicArn
ParameterLabels:
GitRepo:
default: Git Repo
GitBranch:
default: Git Branch
StackTemplatePath:
default: Stack template path
StackTemplateConfigurationPath:
default: Stack template configuration path
DeployStackName:
default: Stack name
PipelineApprovalAction:
default: Approval action
PipelineApprovalEmail:
default: Email address
AlertTopicArn:
default: SNS Alert Topic Arn
Parameters:
StackTemplatePath:
Type: String
Default: template.yaml
Description: CloudFormation stack template filename in the Git repo
StackTemplateConfigurationPath:
Type: String
Default: template-configuration.json
Description: CloudFormation stack template configuration filename in the Git repo
DeployStackName:
Type: String
Default: interLibraryLoanMetadata-api
Description: The name to use for the CloudFormation stack managed by this pipeline.
GitHubOAuthToken:
Description: OAuth token used by AWS CodePipeline to connect to GitHub
NoEcho: true
Type: String
Default: '{{resolve:secretsmanager:githubtoken:SecretString}}'
GitHubOwner:
Description: GitHub username owning the repo
Type: String
Default: BIBSYSDEV
GitRepo:
Type: String
Default: InterLibraryLoanMetadata-api
Description: Git repository name
GitBranch:
Type: String
Default: develop
Description: Git repository branch
PipelineApprovalAction:
Description: Do you want to add a manual approval action to the Release stage of this Pipeline?
AllowedValues:
- 'Yes'
- 'No'
Default: 'No'
Type: String
PipelineApprovalEmail:
Type: String
Default: ''
Description: The target email address for pipeline approval messages. Required when PipelineApprovalAction is Yes
AllowedPattern: '(^$|^[_A-Za-z0-9-\+\.]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$)'
ConstraintDescription: Valid email address or empty
AlertTopicArn:
# Type: 'AWS::SSM::Parameter::Value<String>'
# Default: '/alert/topicArn'
Type: String
Default: ''
Conditions:
IsPipelineApprovalActionCondition: !Equals [!Ref PipelineApprovalAction, 'Yes']
HasApprovalEmailAddress: !Not [!Equals [!Ref PipelineApprovalEmail, '']]
HasAlertTopic: !Not [!Equals [!Ref AlertTopicArn, '']]
Resources:
ArtifactsBucket:
Type: 'AWS::S3::Bucket'
Metadata:
cfn_nag:
rules_to_suppress:
- id: W35
reason: "Skip access logging for artifacts bucket"
- id: W41
reason: "Skip encryption for artifacts bucket"
- id: W51
reason: "Skip bucket policy for artifacts bucket"
DependsOn: CloudFormationRole # make sure that CloudFormationRole is deleted last
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
AccessControl: Private
VersioningConfiguration:
Status: Enabled
LifecycleConfiguration:
Rules:
- Id: DeleteContentAfter8Days
Status: Enabled
ExpirationInDays: 8
PipelineApprovalTopic:
Type: AWS::SNS::Topic
Metadata:
cfn_nag:
rules_to_suppress:
- id: W47
reason: KMS key not relevant
Condition: IsPipelineApprovalActionCondition
Properties:
Subscription: # When adding Subscription, SNS formats the messages before sending
- !If [HasApprovalEmailAddress, {Endpoint: !Ref PipelineApprovalEmail, Protocol: email}, !Ref 'AWS::NoValue']
# - Endpoint: !Ref PipelineApprovalEmail
# Protocol: email
PipelineRole:
# DependsOn: CloudFormationRole # make sure that CloudFormationRole is deleted last
Type: 'AWS::IAM::Role'
Metadata:
cfn_nag:
rules_to_suppress:
- id: W76
reason: Pipeline needs these permissions
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'codepipeline.amazonaws.com'
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: CodePipelineAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- iam:PassRole
Resource: !GetAtt CloudFormationRole.Arn
Condition:
StringEqualsIfExists:
iam:PassedToService:
- cloudformation.amazonaws.com
- Effect: Allow
Action:
- s3:ListBucket
- s3:GetBucketVersioning
Resource:
- !Sub ${ArtifactsBucket.Arn}
- Effect: Allow
Action:
- s3:PutObject
- s3:PutObjectAcl
- s3:CreateMultipartUpload
- s3:ListMultipartUploadParts
- s3:AbortMultipartUpload
- s3:DeleteObject
- s3:GetObject
- s3:GetObjectVersion
Resource:
- !Sub '${ArtifactsBucket.Arn}/*'
- Effect: Allow
Action:
- cloudformation:DescribeStacks
- cloudformation:CreateStack
- cloudformation:UpdateStack
- cloudformation:DescribeChangeSet
- cloudformation:CreateChangeSet
- cloudformation:ExecuteChangeSet
- cloudformation:DeleteChangeSet
Resource:
- !Sub 'arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AWS::StackName}/*'
- !Sub 'arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${DeployStackName}/*'
- Effect: Allow
Action:
- codebuild:BatchGetBuilds
- codebuild:StartBuild
Resource:
- !GetAtt CodeBuildProject.Arn
- !If
- IsPipelineApprovalActionCondition
- Effect: "Allow"
Action:
- "sns:Publish"
Resource: !Ref PipelineApprovalTopic
- !Ref AWS::NoValue
CloudFormationRole:
Type: 'AWS::IAM::Role'
Metadata:
cfn_nag:
rules_to_suppress:
- id: W11
reason: CloudFormation needs permissions to create/update/delete resources in a stack (allow * resource in policy)
- id: F38
reason: CloudFormation needs permissions to create/update/delete resources in a stack (allow * resource with PassRole action on permissions policy)
- id: F3
reason: CloudFormation needs permissions to create/update/delete resources in a stack (allow * action on permissions policy)
- id: W76
reason: CloudFormation needs permissions to create/update/delete resources in a stack
Properties:
Description: 'This role is used both for the CodePipeline CFN stack, and the application CFN stack'
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'cloudformation.amazonaws.com'
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: CloudFormationAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- apigateway:*
- application-autoscaling:*
- autoscaling:*
- cloudformation:*
- cloudwatch:*
- codebuild:*
- codedeploy:*
- codepipeline:*
- ec2:*
- ecs:*
- elasticfilesystem:*
- elasticloadbalancing:*
- events:*
- iam:*
- lambda:*
- logs:*
- kms:*
- rds:*
- s3:*
- secretsmanager:*
- sns:*
- ssm:*
- resource-groups:*
- route53:* # GetHostedZone
Resource: '*'
CodeBuildRole:
DependsOn: CloudFormationRole # make sure that CloudFormationRole is deleted last
Metadata:
cfn_nag:
rules_to_suppress:
- id: W11
reason: "Allow Resource 'bucket/*' for s3 and Resource '*' for CloudWatch Logs."
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'codebuild.amazonaws.com'
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: ServiceRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: CloudWatchLogsPolicy
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'
- Sid: S3GetPutObjects
Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectVersion
- s3:PutObject
# - s3:PutObjectAcl
- s3:CreateMultipartUpload
- s3:ListMultipartUploadParts
- s3:AbortMultipartUpload
Resource: !Sub
- 'arn:aws:s3:::${BucketName}/*'
- BucketName: !Ref ArtifactsBucket
CodeBuildProject:
DependsOn: CloudFormationRole # make sure that CloudFormationRole is deleted last
Type: 'AWS::CodeBuild::Project'
Metadata:
cfn_nag:
rules_to_suppress:
- id: W32
reason: "Skip encryption for CodeBuild project"
Properties:
Artifacts:
Type: NO_ARTIFACTS
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:7.0
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: S3_BUCKET
Value: !Ref ArtifactsBucket
- Name: CFN_TEMPLATE
Value: !Ref StackTemplatePath
Name: !Sub '${AWS::StackName}-build'
ServiceRole: !GetAtt 'CodeBuildRole.Arn'
Source:
Type: GITHUB
Location: https://github.com/BIBSYSDEV/interLibraryLoanMetadata-api.git
Triggers:
Webhook: true
TimeoutInMinutes: 30
Cache:
Type: S3
Location: !Sub '${ArtifactsBucket}/codebuildcache'
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
Name: !Sub '${AWS::StackName}-Pipeline'
RoleArn: !GetAtt PipelineRole.Arn
RestartExecutionOnUpdate: true
# DisableInboundStageTransitions:
# - Reason: Approve Step
# StageName: Release
ArtifactStore:
Type: S3
Location: !Ref ArtifactsBucket
Tags:
- Key: 'automation:approval-timeout'
Value: '1'
Stages:
- Name: Source
Actions:
- Name: Source
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: '1'
Provider: GitHub
Configuration:
Owner: !Ref GitHubOwner
OAuthToken: !Ref GitHubOAuthToken
Repo: !Ref GitRepo
Branch: !Ref GitBranch
PollForSourceChanges: false
OutputArtifacts: [Name: SourceArtifacts]
Namespace: SourceVariables
RunOrder: 1
- Name: Build
Actions:
- Name: TestAndBuildApp
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: '1'
Configuration:
ProjectName: !Ref CodeBuildProject
InputArtifacts:
- Name: SourceArtifacts
OutputArtifacts:
- Name: BuildArtifacts
RunOrder: 1
- Name: UpdatePipeline
Actions:
- Name: DeployPipeline
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
Configuration:
ActionMode: REPLACE_ON_FAILURE
RoleArn: !GetAtt 'CloudFormationRole.Arn'
StackName: !Ref 'AWS::StackName'
Capabilities: CAPABILITY_IAM
TemplatePath: 'BuildArtifacts::pipeline.yml'
ParameterOverrides: !Sub |
{
"GitRepo": "${GitRepo}",
"GitBranch": "${GitBranch}",
"StackTemplatePath": "${StackTemplatePath}",
"StackTemplateConfigurationPath": "${StackTemplateConfigurationPath}",
"DeployStackName": "${DeployStackName}",
"PipelineApprovalAction": "${PipelineApprovalAction}",
"PipelineApprovalEmail": "${PipelineApprovalEmail}",
"AlertTopicArn": "${AlertTopicArn}"
}
InputArtifacts:
- Name: BuildArtifacts
RunOrder: 1
- Name: Release
Actions:
- Name: CreateChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Version: '1'
Provider: CloudFormation
InputArtifacts:
- Name: BuildArtifacts
Configuration:
ActionMode: CHANGE_SET_REPLACE
ChangeSetName: !Sub '${AWS::StackName}-Pipeline-changeset'
RoleArn: !GetAtt CloudFormationRole.Arn
StackName: !Ref DeployStackName
Capabilities: CAPABILITY_AUTO_EXPAND,CAPABILITY_NAMED_IAM,CAPABILITY_IAM
TemplatePath: 'BuildArtifacts::packaged.yml'
TemplateConfiguration: !Sub 'BuildArtifacts::${StackTemplateConfigurationPath}'
RunOrder: 1
- !If
- IsPipelineApprovalActionCondition
- Name: ApprovalForRelease
ActionTypeId:
Category: Approval
Owner: AWS
Version: '1'
Provider: Manual
Configuration:
NotificationArn: !Ref PipelineApprovalTopic
CustomData: !Sub 'A new change set was created for the ${DeployStackName} stack in account ${AWS::AccountId}. Commit message: "#{SourceVariables.CommitMessage}". Do you want to deploy the changes?'
ExternalEntityLink: 'https://aws.unit.no'
RunOrder: 2
- !Ref AWS::NoValue
- Name: ExecuteChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Version: '1'
Provider: CloudFormation
Configuration:
ActionMode: CHANGE_SET_EXECUTE
ChangeSetName: !Sub '${AWS::StackName}-Pipeline-changeset'
StackName: !Ref DeployStackName
OutputFileName: ChangeSetExecuteOutput.json
RunOrder: 3
PipelineFailedNotification:
Type: 'AWS::Events::Rule'
Condition: HasAlertTopic
Properties:
EventPattern:
source:
- 'aws.codepipeline'
detail-type:
- 'CodePipeline Pipeline Execution State Change'
resources:
- !Ref Pipeline
detail:
state:
- FAILED
State: ENABLED
Targets:
- Arn: !Ref AlertTopicArn
Id: rule
Outputs:
ArtifactsBucket:
Description: The artifacts bucket
Value: !Ref ArtifactsBucket