This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.yaml
366 lines (356 loc) · 11.7 KB
/
build.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
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Metadata:
# Documentation for the AWS console
AWS::CloudFormation::Interface:
ParameterGroups:
- Label: Source Location
Parameters:
- Owner
- Repo
- AccessToken
- Branch
- Label: Task Runner
Parameters:
- AccessKeyID
- SecretAccessKey
ParameterLabels:
Owner:
default: GitHub repository owner
Repo:
default: GitHub or CodeCommit repository name
AccessToken:
default: GitHub OAuth access token
Branch:
default: Branch to build
AccessKeyID:
default: AWS Access Key ID
SecretAccessKey:
default: AWS Secret Access Key
Parameters:
Branch:
Type: String
Default: master
Description: The branch of the source repository to build
Owner:
Type: String
Description: The owner of the GitHub repository (if using GitHub.)
Default: ''
Repo:
Type: String
Description: The name of the repository. If using CodeCommit, this is the name of the created repository.
Default: ''
AccessToken:
Type: String
Description: The GitHub token to use to fetch source code (if using GitHub.)
Default: ''
NoEcho: true
AccessKeyID:
Type: String
Description: The AWS Access Key ID to use for the Data Pipeline Task Runner
Default: ''
SecretAccessKey:
Type: String
Description: The AWS Secret Access Key to use for the Data Pipeline Task Runner
Default: ''
NoEcho: true
Conditions:
RepoNameGiven: !Not [!Equals [!Ref Repo, ""]]
UseGitHub: !And
- !Not [!Equals [!Ref Owner, ""]]
- !Not [!Equals [!Ref Repo, ""]]
- !Not [!Equals [!Ref AccessToken, ""]]
UseCodeCommit: !Or
- !Equals [!Ref Owner, ""]
- !Equals [!Ref Repo, ""]
- !Equals [!Ref AccessToken, ""]
Outputs:
RepoURL:
Value: !If
- UseGitHub
- !Sub "https://github.com/${Owner}/${Repo}"
- !GetAtt Repository.CloneUrlHttp
Description: The HTTP URL for the source repository.
Resources:
# A IAM Role for deploying the pipeline.
Role:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service:
- !Sub "cloudformation.${AWS::URLSuffix}"
- !Sub "logs.${AWS::Region}.${AWS::URLSuffix}"
- !Sub "events.${AWS::URLSuffix}"
- !Sub "codecommit.${AWS::URLSuffix}"
- !Sub "codebuild.${AWS::URLSuffix}"
- !Sub "codedeploy.${AWS::URLSuffix}"
- !Sub "codepipeline.${AWS::URLSuffix}"
- !Sub "cloudfront.${AWS::URLSuffix}"
- !Sub "s3.${AWS::URLSuffix}"
- !Sub "apigateway.${AWS::URLSuffix}"
- !Sub "lambda.${AWS::URLSuffix}"
- !Sub "dynamodb.${AWS::URLSuffix}"
- !Sub "kinesis.${AWS::URLSuffix}"
- !Sub "ssm.${AWS::URLSuffix}"
- !Sub "route53.${AWS::URLSuffix}"
- !Sub "acm.${AWS::URLSuffix}"
- !Sub "firehose.${AWS::URLSuffix}"
- !Sub "elasticmapreduce.${AWS::URLSuffix}"
- !Sub "redshift.${AWS::URLSuffix}"
- !Sub "ec2.${AWS::URLSuffix}"
- !Sub "datapipeline.${AWS::URLSuffix}"
- !Sub "serverlessrepo.${AWS::URLSuffix}"
Policies:
- PolicyName: Bootstrap
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- cloudformation:*
- codecommit:*
- codebuild:*
- codedeploy:*
- codepipeline:*
- cloudfront:*
- events:*
- iam:*
- logs:*
- apigateway:*
- lambda:*
- dynamodb:*
- kinesis:*
- s3:*
- ssm:*
- route53:*
- acm:*
- firehose:*
- elasticmapreduce:*
- redshift:*
- ec2:*
- datapipeline:*
- serverlessrepo:*
- glue:*
- kinesisanalytics:*
- cloudwatch:*
- iam:PassRole
Effect: Allow
Resource: "*"
# A S3 Bucket to hold the build artifacts and deploy the pipeline from.
#
# Versioning must be enabled for CodePipeline and friends to use. The bucket
# is retained on delete to make the stack easier to delete.
Bucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: aws:kms
VersioningConfiguration:
Status: Enabled
# A CodeCommit repository to hold the source code.
#
# This could be replaced in the build (below) with a GitHub repository, but
# that requires a manual step of creating a access token.
Repository:
Type: AWS::CodeCommit::Repository
Condition: UseCodeCommit
DeletionPolicy: Retain
Properties:
RepositoryName: !If
- RepoNameGiven
- !Ref Repo
- !Ref AWS::StackName
# A CodePipeline pipeline to build and deploy the data pipeline.
#
# It fetches the source, builds, tests, and packages all of the lambdas,
# packages and uploads the lambdas and CloudFormation template, then
# continuously deploys it.
#
# The target stack's name is the current stack name with the suffix "-prod" to
# avoid name conflicts.
Build:
Type: AWS::CodeBuild::Project
Properties:
Name: !Sub "${AWS::StackName}-build"
ServiceRole: !Ref Role
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:2.0
EnvironmentVariables:
- Type: PLAINTEXT
Name: BUCKET
Value: !Ref Bucket
- Type: PLAINTEXT
Name: BRANCH
Value: !Ref Branch
- Type: PLAINTEXT
Name: GO111MODULE
Value: "on"
- Type: PLAINTEXT
Name: ACCESS_KEY_ID
Value: !Ref AccessKeyID
- Type: PLAINTEXT
Name: SECRET_ACCESS_KEY
Value: !Ref SecretAccessKey
Source:
Type: CODEPIPELINE
Artifacts:
Type: CODEPIPELINE
# A CI/CD pipeline for the data pipeline. Continuously deploys the data
# pipeline to a stack named "<build-stack-name>-prod".
#
# This fetches the code, uses the build job to build and upload the package,
# then deploys the stack using CloudFormation using a change set.
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
RoleArn: !GetAtt Role.Arn
ArtifactStore:
Type: S3
Location: !Ref Bucket
Stages:
- Name: Source
Actions:
- Name: Fetch
RunOrder: 1
ActionTypeId:
Category: Source
Owner: !If [UseGitHub, ThirdParty, AWS]
Provider: !If [UseGitHub, GitHub, CodeCommit]
Version: "1"
Configuration: !If
- UseGitHub
- Owner: !Ref Owner
Repo: !Ref Repo
OAuthToken: !Ref AccessToken
Branch: !Ref Branch
- RepositoryName: !GetAtt Repository.Name
BranchName: !Ref Branch
OutputArtifacts:
- Name: Source
- Name: Build
Actions:
- Name: Build
RunOrder: 1
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: "1"
Configuration:
ProjectName: !Ref Build
InputArtifacts:
- Name: Source
OutputArtifacts:
- Name: Package
- Name: Deploy
Actions:
- Name: CreateChangeSet
RunOrder: 1
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: "1"
Configuration:
ActionMode: CHANGE_SET_REPLACE
RoleArn: !GetAtt Role.Arn
StackName: !Sub "${AWS::StackName}-${Branch}"
ChangeSetName: !Sub "${AWS::StackName}-${Branch}-change-set"
TemplatePath: Package::template.yaml
TemplateConfiguration: Package::template-config.json
Capabilities: CAPABILITY_IAM,CAPABILITY_NAMED_IAM,CAPABILITY_AUTO_EXPAND
InputArtifacts:
- Name: Package
- Name: ExecuteChangeSet
RunOrder: 2
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: "1"
Configuration:
ActionMode: CHANGE_SET_EXECUTE
RoleArn: !GetAtt Role.Arn
StackName: !Sub "${AWS::StackName}-${Branch}"
ChangeSetName: !Sub "${AWS::StackName}-${Branch}-change-set"
Capabilities: CAPABILITY_IAM,CAPABILITY_NAMED_IAM,CAPABILITY_AUTO_EXPAND
# TODO
#################
# Set up a CloudFront-cached static site behind Route53 DNS.
#
# Copied/pasted from an external source. Not integrated yet.
#################
#
# Certificate:
# Type: AWS::CertificateManager::Certificate
# Conditional: DNSEnabled
# Properties:
# DomainName: !Ref DNSName
# DomainValidationOptions:
# - DomainName: !Ref DNSName
# ValidationDomain: !Ref DNSName
#
# CloudFrontOrigin:
# Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
# Conditional: DNSEnabled
# Properties:
# CloudFrontOriginAccessIdentityConfig:
# Comment: !Sub "The origin for ${DNSName}"
#
# CloudFrontDistribution:
# Type: AWS::CloudFront::Distribution
# Conditional: DNSEnabled
# Properties:
# DistributionConfig:
# Aliases:
# - !Ref DNSName
# DefaultCacheBehavior:
# AllowedMethods:
# - GET
# - HEAD
# - OPTIONS
# ForwardedValues:
# QueryString: false
# Cookies:
# Forward: none
# TargetOriginId: Root
# ViewerProtocolPolicy: https-only
# DefaultRootObject: index.html
# Enabled: true
# Origins:
# - Id: Root
# DomainName: !Sub "${Bucket}.s3.amazonaws.com"
# OriginPath: !Sub "/${Prefix}/static"
# S3OriginConfig:
# OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CloudFrontOrigin}"
# ViewerCertificate:
# AcmCertificateArn: !Ref Certificate
# SslSupportMethod: sni-only
#
# RecordSetGroup:
# Type: AWS::Route53::RecordSetGroup
# Condition: DNSEnabled
# Properties:
# HostedZoneId: !Ref HostedZoneID
# RecordSets:
# - Type: A
# Name: !Ref DNSName
# AliasTarget:
# DNSName: !GetAtt CloudFrontDistribution.DomainName
# HostedZoneId: Z2FDTNDATAQYW2
# {{- range .Domains }}
# - Type: A
# Name: !Sub "{{ .ID }}.${DNSName}"
# AliasTarget:
# DNSName: !GetAtt {{ .Name }}Domain.Outputs.DistributionDomainName
# HostedZoneId: Z2FDTNDATAQYW2
# {{- end }}