-
Notifications
You must be signed in to change notification settings - Fork 0
/
codepipeline.yaml
444 lines (407 loc) · 13.6 KB
/
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
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
# Warning: This is a very basic cloudformation script
# No validation for any parameter values
# Not tested extensively, only have done minimum testing
# IAM policies created by the templates are more permissive than required
AWSTemplateFormatVersion: "2010-09-09"
Description: Cloudformation for deploying a codepipeline that can sync files from a S3 bucket to EFS
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Network configuration
Parameters:
- AwsVpc
- Subnets
- SecurityGroupId
- Label:
default: DAG update pipeline
Parameters:
- FileSourceZip
- TargetEFSID
ParameterLabels:
AwsVpc:
default: VPC where EFS is located
Subnets:
default: Subnets in the VPC where the EFS has access points
SecurityGroupId:
default: Existing security group id to be used
FileSourceZip:
default: Zip file containing extfiles
TargetEFSID:
default: ID of the EFS to which file copy needs to happen
Parameters:
AwsVpc:
Description: Name of existing AWS Vpc
Type: AWS::EC2::VPC::Id
Subnets:
Description: Subnets where the service to startup
Type: List<AWS::EC2::Subnet::Id>
SecurityGroupId:
Description: Existing security groups to assign to the airflow cluster nodes
Type: String
FileSourceBucket:
Description: S3 bucket that hosts the files to be copied
Type: String
FileSourceZip:
Description: source zip in S3
Type: String
TargetEFSID:
Description: Target EFS id
Type: String
Resources:
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-CodeBuild-Role
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [codebuild.amazonaws.com]
Version: '2012-10-17'
CodeBuildPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: Allows CodeBuild to perform builds and deploys
ManagedPolicyName: !Sub ${AWS::StackName}-CodeBuild-Policy
Roles:
- !Ref CodeBuildRole
PolicyDocument:
Version: 2012-10-17
Statement:
- # Allow codebuild to write to cloudwatch
Sid: CloudWatchLogsPermissionsForAllCodeBuildProjects
Effect: Allow
Action:
- logs:*
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*:*
- # Allow codebuild to list and get objects from any bucket
Sid: S3BucketPolicy
Effect: Allow
Action:
- s3:*
Resource:
- !Sub arn:aws:s3:::${FileSourceBucket}
- !Sub arn:aws:s3:::${FileSourceBucket}/*
- !GetAtt PipelineBucket.Arn
- !Sub ${PipelineBucket.Arn}/*
- # Allow codebuild to write reports
Sid: CodeBuildReportingPolicy
Effect: Allow
Action:
- codebuild:CreateReportGroup
- codebuild:CreateReport
- codebuild:BatchPutTestCases
- codebuild:UpdateReport
- codebuild:UpdateProject
Resource:
- !Sub "arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:*/*"
- # Allow codebuild to list and get objects from any bucket
Sid: EC2Access
Effect: Allow
Action:
- ec2:Describe*
- ec2:Create*
- ec2:*SecurityGroup*
- ec2:DeleteNetworkInterface
Resource: '*'
- # Allow codebuild to list and get objects from any bucket
Sid: EFSAccess
Effect: Allow
Action:
- elasticfilesystem:ClientMount
- elasticfilesystem:ClientRootAccess
- elasticfilesystem:ClientWrite
- elasticfilesystem:ClientMount
Resource: '*'
- # Allow codebuild to perform encryption and decryption
Sid: KMSPolicy
Effect: Allow
Action:
- kms:DescribeKey
- kms:GetKeyPolicy
- kms:List*
- kms:Encrypt
- kms:Decrypt
- kms:ReEncrypt*
- kms:Generate*
Resource:
- !GetAtt PipelineKMSKey.Arn
# CodeBuild for to update EFS
CodeBuildProjectDev:
Type: AWS::CodeBuild::Project
DependsOn:
- CodeBuildPolicy
Properties:
Name: !Sub CodePipeline-EFS-Update-${AWS::StackName}
VpcConfig:
SecurityGroupIds:
- !Ref SecurityGroupId
Subnets: !Ref Subnets
VpcId: !Ref AwsVpc
FileSystemLocations:
- Identifier: ExternalFiles
Location: !Sub ${TargetEFSID}.efs.${AWS::Region}.amazonaws.com:/
MountPoint: /mnt/efs
Type: EFS
Artifacts:
Type: NO_ARTIFACTS
Source:
Type: NO_SOURCE
BuildSpec: |
version: 0.2
phases:
pre_build:
commands:
- echo Nothing to setup here
build:
commands:
- echo "Starting file copy"
# Add the commands to copy files here
# eg:
# - rsync -avu --delete . /mnt/efs/external_files
post_build:
commands:
- echo Build done
ServiceRole: !GetAtt CodeBuildRole.Arn
Environment:
Type: LINUX_CONTAINER
Image: aws/codebuild/standard:5.0
ComputeType: BUILD_GENERAL1_SMALL
PrivilegedMode: True
EnvironmentVariables:
-
Name: SOURCE_S3
Type: PLAINTEXT
Value: !Ref FileSourceBucket
Tags:
- Key: DeployStage
Value: Build
- Key: ProjectName
Value: !Sub CodePipeline-EFS-Update-${AWS::StackName}
PipelineKMSKey:
Type: AWS::KMS::Key
Properties:
Description: Key used by cloudformation codepipeline
Enabled: true
KeyPolicy:
Version: '2012-10-17'
Id: key-consolepolicy-3
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action: kms:*
Resource: '*'
- Sid: Allow use of the key
Effect: Allow
Principal:
AWS:
- !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action:
- kms:Encrypt
- kms:Decrypt
- kms:ReEncrypt*
- kms:GenerateDataKey*
- kms:DescribeKey
Resource: '*'
- Sid: Allow attachment of persistent resources
Effect: Allow
Principal:
AWS:
- !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action:
- kms:CreateGrant
- kms:ListGrants
- kms:RevokeGrant
- kms:Encrypt
- kms:Decrypt
Resource: '*'
Condition:
Bool:
kms:GrantIsForAWSResource: 'true'
# S3 bucket used by the pipeline
PipelineBucket:
Type: AWS::S3::Bucket
DependsOn: PipelineKMSKey
Properties:
VersioningConfiguration:
Status: Enabled
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
KMSMasterKeyID: !Ref PipelineKMSKey
SSEAlgorithm: aws:kms
# S3 Bucket policy for the artifact storage bucket
PipelineBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref PipelineBucket
PolicyDocument:
Version: '2012-10-17'
Statement:
Action:
- '*'
Effect: Allow
Resource:
- !Join [ "", [ "arn:aws:s3:::", Ref: PipelineBucket ] ]
- !Join [ "", [ "arn:aws:s3:::", Ref: PipelineBucket, "/*" ] ]
Principal:
AWS:
- !Sub 'arn:aws:iam::${AWS::AccountId}:root'
# Cloudformation Service role used by the pipeline to deploy cloudformation scripts
#IAM role assumed by the pipeline
PipelineRole:
Type: AWS::IAM::Role
DependsOn: PipelineKMSKey
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [codepipeline.amazonaws.com]
Version: '2012-10-17'
Path: /
Policies:
- PolicyName: CodePipelineAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 's3:*'
- 'cloudformation:CreateStack'
- 'cloudformation:DescribeStacks'
- 'cloudformation:DeleteStack'
- 'cloudformation:UpdateStack'
- 'cloudformation:CreateChangeSet'
- 'cloudformation:ExecuteChangeSet'
- 'cloudformation:DeleteChangeSet'
- 'cloudformation:DescribeChangeSet'
- 'cloudformation:SetStackPolicy'
- 'cloudformation:ValidateTemplate'
- 'iam:PassRole'
- 'sns:Publish'
- 'codebuild:CreateReportGroup'
- 'codebuild:CreateReport'
- 'codebuild:UpdateReport'
- 'codebuild:BatchPutTestCases'
- 'codebuild:BatchPutCodeCoverages'
- 'codebuild:StartBuild'
- 'codebuild:BatchGetBuilds'
- 'codebuild:BatchGetBuildBatches'
- 'codebuild:StartBuildBatch'
- 'kms:*'
Effect: Allow
Resource: '*'
# IAM role to be assumed by cloudwatch event rules to trigger a code pipeline
CloudWatchPipelineTriggerRole:
DependsOn: FileUpdatePipeline
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-cicd-trigger
Path: /service-role/
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- events.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: !Sub ${AWS::StackName}-cicd-cw-trigger-policy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- codepipeline:StartPipelineExecution
Resource: # Allow CloudWatch to trigger any pipeline
- !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${FileUpdatePipeline}
- !Sub arn:aws:events:${AWS::Region}:${AWS::AccountId}:rule/${FileUpdatePipeline}-code-check-in-trigger
# Cloudwatch event rule, to trigger the pipeline when the code is uploaded to the Source S3 bucket
S3CodeCheckCloudWatchEvent:
DependsOn: FileUpdatePipeline
Type: AWS::Events::Rule
Properties:
Description: CloudWatch event rule to trigger Code pipeline on s3 upload/update
EventPattern:
source:
- aws.s3
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- s3.amazonaws.com
requestParameters:
bucketName:
- !Ref FileSourceBucket
key:
- !Ref FileSourceZip
eventName:
- PutObject
- CompleteMultipartUpload
- CopyObject
Name: !Join [ '-', [ !Ref FileUpdatePipeline, 'code-check-in-trigger' ]]
State: ENABLED
Targets:
- Arn: !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${FileUpdatePipeline}
Id: !Sub ${FileUpdatePipeline}
RoleArn: !GetAtt CloudWatchPipelineTriggerRole.Arn
#Pipeline updates
FileUpdatePipeline:
Type: AWS::CodePipeline::Pipeline
DependsOn: PipelineKMSKey
Properties:
RoleArn: !GetAtt PipelineRole.Arn
RestartExecutionOnUpdate: true
Name: !Sub Pipeline-${AWS::StackName}
ArtifactStore:
Type: S3
Location: !Ref PipelineBucket
EncryptionKey:
Id: !GetAtt PipelineKMSKey.Arn
Type: KMS
# Pipeline has different stages, Source/build/deploy etc.
Stages:
# Source stage, downloading the code from the given S3 bucket/prefix
- Name: Source-S3-Buckets
Actions:
- Name: Source
ActionTypeId:
Category: Source
Owner: AWS
Version: 1
Provider: S3
Configuration:
S3Bucket: !Ref FileSourceBucket
S3ObjectKey: !Ref FileSourceZip
PollForSourceChanges: false
OutputArtifacts:
- Name: S3Artifacts
RunOrder: 1
- Name: Build-extfiles-deploy
Actions:
- Name: Deploy-extfiles-updates
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: 1
RunOrder: 1
InputArtifacts:
- Name: S3Artifacts
OutputArtifacts:
- Name: BuildArtifactDev
Configuration:
ProjectName: !Ref CodeBuildProjectDev
Outputs:
LogicalID:
Description: Pipeline
Value: !Ref FileUpdatePipeline