-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.yml
377 lines (366 loc) · 10.9 KB
/
serverless.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
service: ${self:custom.baseName}-web
provider:
name: aws
stage: ${self:custom.stage}
runtime: nodejs8.10
region: us-east-1
stackName: ${self:custom.baseName}-stack
apiName: ${self:custom.baseName}-api
profile: ${self:custom.baseName}
versionFunctions: false
environment:
CDN_DEV: '//dev.cdn.${self:custom.baseDNSName}'
CDN_PROD: '//cdn.${self:custom.baseDNSName}'
stackTags:
name: ${self:custom.baseName}-web
tags:
name: ${self:custom.baseName}-web
plugins:
- serverless-offline
- serverless-plugin-bind-deployment-id
- serverless-s3-sync
package:
include:
- ../node_modules/**
exclude:
- .serverless/**
- assets/**
resources:
Conditions:
IsDev:
Fn::Equals:
- ${self:custom.stage}
- dev
IsProd:
Fn::Equals:
- ${self:custom.stage}
- prod
Resources:
# S3 Bucket -> CloudFront CDN Resources, Mappings, Policies
AssetsCDNAccessId:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: ${self:custom.baseName}-oai
AssetsOrigin:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.baseName}
AccessControl: AuthenticatedRead
Tags:
- Key: name
Value: ${self:custom.baseName}-web
AssetOriginPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: ${self:custom.baseName}
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: PolicyForCloudFrontPrivateContent
Effect: Allow
Principal:
CanonicalUser:
'Fn::GetAtt': ['AssetsCDNAccessId', 'S3CanonicalUserId']
Action:
- 's3:GetObject'
Resource: 'arn:aws:s3:::${self:custom.baseName}/*'
DependsOn:
- AssetsOrigin
AssetsCDN:
Type: AWS::CloudFront::Distribution
Condition: IsProd
Properties:
DistributionConfig:
Origins:
- OriginPath: /prod
Id: ${self:custom.baseName}
DomainName: '${self:resources.Resources.AssetsOrigin.Properties.BucketName}.s3.amazonaws.com'
S3OriginConfig:
OriginAccessIdentity:
'Fn::Join':
['/', ['origin-access-identity/cloudfront', { 'Ref': 'AssetsCDNAccessId' }]]
Aliases:
- 'cdn.${self:custom.baseDNSName}'
Enabled: true
ViewerCertificate:
AcmCertificateArn: ${self:custom.wildcardCertArn}
SslSupportMethod: sni-only
DefaultCacheBehavior:
TargetOriginId: ${self:custom.baseName}
ViewerProtocolPolicy: redirect-to-https
ForwardedValues:
QueryString: false
AllowedMethods:
- GET
- HEAD
- OPTIONS
CachedMethods:
- GET
- HEAD
- OPTIONS
Tags:
- Key: name
Value: ${self:custom.baseName}
DependsOn:
- AssetsCDNAccessId
- AssetsOrigin
AssetsCDNDev:
Type: AWS::CloudFront::Distribution
Condition: IsDev
Properties:
DistributionConfig:
Origins:
- OriginPath: /dev
Id: ${self:custom.baseName}
DomainName: '${self:resources.Resources.AssetsOrigin.Properties.BucketName}.s3.amazonaws.com'
S3OriginConfig:
OriginAccessIdentity:
'Fn::Join':
['/', ['origin-access-identity/cloudfront', { 'Ref': 'AssetsCDNAccessId' }]]
Aliases:
- 'dev.cdn.${self:custom.baseDNSName}'
Enabled: true
ViewerCertificate:
AcmCertificateArn: ${self:custom.wildcardCertArn}
SslSupportMethod: sni-only
DefaultCacheBehavior:
TargetOriginId: ${self:custom.baseName}
ViewerProtocolPolicy: redirect-to-https
ForwardedValues:
QueryString: false
AllowedMethods:
- GET
- HEAD
- OPTIONS
CachedMethods:
- GET
- HEAD
- OPTIONS
Tags:
- Key: name
Value: ${self:custom.baseName}
DependsOn:
- AssetsCDNAccessId
- AssetsOrigin
# Api Gateway Custom Domains
ApiGatewayDomainRoot:
Type: AWS::ApiGateway::DomainName
Condition: IsProd
Properties:
DomainName: ${self:custom.baseDNSName}
EndpointConfiguration:
Types:
- 'EDGE'
CertificateArn: ${self:custom.wildcardCertArn}
ApiGatewayDomainWWW:
Type: AWS::ApiGateway::DomainName
Condition: IsProd
Properties:
DomainName: 'www.${self:custom.baseDNSName}'
EndpointConfiguration:
Types:
- 'EDGE'
CertificateArn: ${self:custom.wildcardCertArn}
ApiGatewayDomainDev:
Type: AWS::ApiGateway::DomainName
Condition: IsDev
Properties:
DomainName: 'dev.${self:custom.baseDNSName}'
EndpointConfiguration:
Types:
- 'EDGE'
CertificateArn: ${self:custom.wildcardCertArn}
ApiGatewayStageProd:
Type: AWS::ApiGateway::Stage
Condition: IsProd
Properties:
StageName: prod
Description: 'www.${self:custom.baseName}'
RestApiId:
'Ref': ApiGatewayRestApi
DeploymentId:
'Ref': __deployment__
ApiGatewayStageDev:
Type: AWS::ApiGateway::Stage
Condition: IsDev
Properties:
StageName: dev
Description: 'dev.${self:custom.baseName}'
RestApiId:
'Ref': ApiGatewayRestApi
DeploymentId:
'Ref': __deployment__
# API Base Path Mapping
DomainMappingRoot:
Type: AWS::ApiGateway::BasePathMapping
Condition: IsProd
Properties:
DomainName:
'Ref': ApiGatewayDomainRoot
RestApiId:
'Ref': ApiGatewayRestApi # Created by serverless
Stage: prod
DependsOn:
- ApiGatewayDomainRoot
- ApiGatewayStageProd
DomainMappingWWW:
Type: AWS::ApiGateway::BasePathMapping
Condition: IsProd
Properties:
DomainName:
'Ref': ApiGatewayDomainWWW
RestApiId:
'Ref': ApiGatewayRestApi # Created by serverless
Stage: prod
DependsOn:
- ApiGatewayDomainWWW
- ApiGatewayStageProd
DomainMappingDev:
Type: AWS::ApiGateway::BasePathMapping
Condition: IsDev
Properties:
DomainName:
'Ref': ApiGatewayDomainDev
RestApiId:
'Ref': ApiGatewayRestApi # Created by serverless
Stage: dev
DependsOn:
- ApiGatewayDomainDev
- ApiGatewayStageDev
# DNS Resource
DNSEntryRoot:
Type: AWS::Route53::RecordSet
Condition: IsProd
Properties:
HostedZoneName: '${self:custom.baseDNSName}.'
AliasTarget:
DNSName:
'Fn::GetAtt': ['ApiGatewayDomainRoot', 'DistributionDomainName']
HostedZoneId:
'Fn::GetAtt': ['ApiGatewayDomainRoot', 'DistributionHostedZoneId']
Comment: 'The root domain for ${self:custom.baseName}'
Name: '${self:custom.baseDNSName}.'
Type: A
DependsOn:
- ApiGatewayDomainRoot
- DomainMappingRoot
DNSEntryWWW:
Type: AWS::Route53::RecordSet
Condition: IsProd
Properties:
HostedZoneName: '${self:custom.baseDNSName}.'
AliasTarget:
DNSName:
'Fn::GetAtt': ['ApiGatewayDomainWWW', 'DistributionDomainName']
HostedZoneId:
'Fn::GetAtt': ['ApiGatewayDomainWWW', 'DistributionHostedZoneId']
Comment: 'The www record for ${self:custom.baseName}'
Name: 'www.${self:custom.baseDNSName}.'
Type: A
DependsOn:
- ApiGatewayDomainWWW
- DomainMappingWWW
DNSEntryCDN:
Type: AWS::Route53::RecordSet
Condition: IsProd
Properties:
HostedZoneName: '${self:custom.baseDNSName}.'
Comment: 'The CDN record for prod stage ${self:custom.baseName}'
ResourceRecords:
- 'Fn::GetAtt': ['AssetsCDN', 'DomainName']
Name: 'cdn.${self:custom.baseDNSName}'
Type: CNAME
TTL: '900'
DependsOn:
- AssetsCDN
DNSEntryCDNDev:
Type: AWS::Route53::RecordSet
Condition: IsDev
Properties:
HostedZoneName: '${self:custom.baseDNSName}.'
Comment: 'The CDN record for the dev stage or ${self:custom.baseName}'
ResourceRecords:
- 'Fn::GetAtt': ['AssetsCDNDev', 'DomainName']
Name: 'dev.cdn.${self:custom.baseDNSName}'
Type: CNAME
TTL: '900'
DependsOn:
- AssetsCDNDev
functions: ${self:custom.functions.${self:custom.mode}}
custom:
functions:
# The functions to use locally
local:
assets:
handler: handler.assets
description: An offline assets handler
events:
- http:
path: '/assets/{proxy+}'
method: get
contentHandling: CONVERT_TO_BINARY
thanks:
handler: handler.thanks
description: A generic thank you page
events:
- http:
path: '/thanks'
method: get
contact:
handler: handler.contact
description: Handles sending a contact email from a form submission
events:
- http:
path: '/contact'
method: post
ping:
handler: handler.ping
description: A generic ping page that responds with pong and date-time
events:
- http:
path: '/ping'
method: get
index:
handler: handler.index
description: Serves the main home page
events:
- http:
path: '/'
method: get
# The function definitions to deploy remotely
remote:
thanks:
handler: handler.thanks
description: A generic thank you page
events:
- http:
path: '/thanks'
method: get
contact:
handler: handler.contact
description: Handles sending a contact email from a form submission
events:
- http:
path: '/contact'
method: post
index:
handler: handler.index
description: Serves the main home page
events:
- http:
path: '/'
method: get
stage: ${opt:stage, 'offline'}
mode: ${opt:mode, 'local'}
baseName: 'my-domain'
baseDNSName: 'mydomain.com'
wildcardCertArn: 'arn:aws:acm:us-east-1:000000000000:certificate/00000000-0000-0000-0000-000000000000'
s3Sync:
- bucketName: ${self:custom.baseName}
bucketPrefix: ${self:custom.stage}/assets/
localDir: assets/
serverless-offline:
location: dist
noAuth: true
port: 9989