-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudformation.yml
More file actions
358 lines (341 loc) · 8.8 KB
/
cloudformation.yml
File metadata and controls
358 lines (341 loc) · 8.8 KB
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
AWSTemplateFormatVersion: '2010-09-09'
#
# CloudFormation input parameters
#
Parameters:
TLD:
Type: String
Description: TLD name needed by Route53 to perform DNS (example.com)
Domain:
Type: String
Description: Domain name for your website (example.com)
Redirect:
Type: String
Description: Redirect to Domain (www.example.com redirects to example.com)
Default: ''
GitHubOwner:
Type: String
Description: GitHub repository owner
Default: ''
GitHubRepo:
Type: String
Default: aws-codepipeline-nested-stack
Description: GitHub repository name
Default: ''
GitHubToken:
Type: String
Description: GitHub repository OAuth token
Default: ''
GitHubBranch:
Type: String
Description: GitHub repository branch
Default: ''
Conditions:
NeedsRedirect: !Not [!Equals [!Ref Redirect, '']]
NeedsCICD: !And
- !Not [!Equals [!Ref GitHubOwner, '']]
- !Not [!Equals [!Ref GitHubRepo, '']]
- !Not [!Equals [!Ref GitHubToken, '']]
- !Not [!Equals [!Ref GitHubBranch, '']]
#
# CloudFormation resources
#
Resources:
#
# Website buckets
#
WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref Domain
AccessControl: PublicRead
WebsiteConfiguration:
IndexDocument: 'index.html'
ErrorDocument: '404.html'
RedirectBucket:
Type: AWS::S3::Bucket
Condition: NeedsRedirect
Properties:
BucketName: !Ref Redirect
AccessControl: BucketOwnerFullControl
WebsiteConfiguration:
RedirectAllRequestsTo:
HostName: !Ref Domain
#
# SSL Certificate needed by CloudFront.
#
SSL:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Ref Domain
DomainValidationOptions:
- DomainName: !Ref Domain
ValidationDomain: !Ref TLD
#
# CloudFront CDN
#
CDN:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases:
- !Ref Domain
Enabled: true
PriceClass: 'PriceClass_All'
CacheBehaviors:
-
TargetOriginId: !Ref WebsiteBucket
PathPattern: '*.js'
ViewerProtocolPolicy: 'redirect-to-https'
MinTTL: 0
AllowedMethods:
- 'HEAD'
- 'GET'
CachedMethods:
- 'HEAD'
- 'GET'
ForwardedValues:
QueryString: true
Cookies:
Forward: 'none'
-
TargetOriginId: !Ref WebsiteBucket
PathPattern: '*.css'
ViewerProtocolPolicy: 'redirect-to-https'
MinTTL: 0
AllowedMethods:
- 'HEAD'
- 'GET'
CachedMethods:
- 'HEAD'
- 'GET'
ForwardedValues:
QueryString: true
Cookies:
Forward: 'none'
DefaultCacheBehavior:
TargetOriginId: !Ref WebsiteBucket
ViewerProtocolPolicy: 'redirect-to-https'
MinTTL: 0
AllowedMethods:
- 'HEAD'
- 'GET'
CachedMethods:
- 'HEAD'
- 'GET'
ForwardedValues:
QueryString: false
Cookies:
Forward: none
Origins:
-
Id: !Ref WebsiteBucket
DomainName:
Fn::Join:
- ''
- - !Ref Domain
- '.s3-website-'
- !Ref AWS::Region
- '.amazonaws.com'
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: 'http-only'
Restrictions:
GeoRestriction:
RestrictionType: 'none'
ViewerCertificate:
SslSupportMethod: 'sni-only'
MinimumProtocolVersion: 'TLSv1'
AcmCertificateArn: !Ref SSL
#
# Route53 DNS record set to map our domain to our CDN
#
DomainDNS:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneName:
Fn::Join:
- ''
- - !Ref TLD
- '.'
RecordSets:
-
Name: !Ref Domain
Type: 'A'
AliasTarget:
HostedZoneId: 'Z2FDTNDATAQYW2' # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget.html#cfn-route53-aliastarget-hostedzoneid
DNSName: !GetAtt CDN.DomainName
#
# Route53 DNS record set to map our redirect to our domain (www.example.com -> example.com)
#
RedirectDNS:
Type: AWS::Route53::RecordSet
Condition: NeedsRedirect
Properties:
HostedZoneName:
Fn::Join:
- ''
- - !Ref TLD
- '.'
Name: !Ref Redirect
Type: 'CNAME'
TTL: 900
ResourceRecords:
-
Fn::Join:
- ''
- - !Ref Domain
- '.s3-website-'
- !Ref AWS::Region
- '.amazonaws.com'
#
# CI/CD
#
ArtifactsBucket:
Type: AWS::S3::Bucket
Condition: NeedsCICD
Properties:
AccessControl: Private
#
# CodeBuild Permissions
#
CodeBuildRole:
Type: AWS::IAM::Role
Condition: NeedsCICD
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- sts:AssumeRole
Principal:
Service:
- codebuild.amazonaws.com
CodeBuildRolePolicy:
Type: AWS::IAM::Policy
Condition: NeedsCICD
Properties:
PolicyName: CodeBuildRolePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'
- Effect: Allow
Action:
- s3:*
Resource:
- !Sub 'arn:aws:s3:::${ArtifactsBucket}'
- !Sub 'arn:aws:s3:::${ArtifactsBucket}/*'
- !Sub 'arn:aws:s3:::${WebsiteBucket}'
- !Sub 'arn:aws:s3:::${WebsiteBucket}/*'
Roles:
- !Ref CodeBuildRole
#
# CodeBuild
#
CodeBuildProject:
Type: AWS::CodeBuild::Project
Condition: NeedsCICD
Properties:
Artifacts:
Type: CODEPIPELINE
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/eb-nodejs-6.10.0-amazonlinux-64:4.0.0
Type: LINUX_CONTAINER
EnvironmentVariables:
-
Name: 'BUCKET_NAME'
Value: !Ref WebsiteBucket
Name:
Fn::Join:
- ''
- - Ref: AWS::StackName
- '-code-build'
ServiceRole: !GetAtt CodeBuildRole.Arn
Source:
Type: CODEPIPELINE
BuildSpec: buildspec.yml
TimeoutInMinutes: 5 # must be between 5 minutes and 8 hours
#
# CodePipeline Permissions
#
CodePipelineRole:
Type: AWS::IAM::Role
Condition: NeedsCICD
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- sts:AssumeRole
Principal:
Service:
- codepipeline.amazonaws.com
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AdministratorAccess
#
# CodePipeline
#
CodePipeline:
Type: AWS::CodePipeline::Pipeline
Condition: NeedsCICD
Properties:
ArtifactStore:
Type: S3
Location: !Ref ArtifactsBucket
RestartExecutionOnUpdate: true
RoleArn: !GetAtt CodePipelineRole.Arn
Stages:
- Name: Source
Actions:
- Name: Source
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: 1
Provider: GitHub
Configuration:
Owner: !Ref GitHubOwner
Repo: !Ref GitHubRepo
Branch: !Ref GitHubBranch
OAuthToken: !Ref GitHubToken
OutputArtifacts:
- Name: SourceOutput
RunOrder: 1
- Name: Build
Actions:
- Name: BuildTestDeploy
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: 1
Configuration:
ProjectName: !Ref CodeBuildProject
InputArtifacts:
- Name: SourceOutput
OutputArtifacts:
- Name: BuildOutput
#
# Outputs to be used by other CloudFormation templates if needed.
#
#Outputs:
# LambdaFunction:
# Description: Lambda Function
# Value: !Ref LambdaFunction
# Export:
# Name: !Sub '${AWS::StackName}-LambdaFunction'
# S3Bucket:
# Description: S3 Bucket
# Value: !Ref S3Bucket
# Export:
# Name: !Sub '${AWS::StackName}-S3Bucket'