-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
464 lines (458 loc) · 13.6 KB
/
template.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
proj-pronetplat-aa
Sample SAM Template for proj-pronetplat-aa
Mappings:
stageVars:
dev:
appPackageName: com.aprmp.projpronetplataa.dev
apiSubDomain: api-dev
webSocketSubDomain: websocket-dev
prod:
appPackageName: com.aprmp.projpronetplataa
apiSubDomain: api
webSocketSubDomain: websocket
Parameters:
Stage:
Description: Stage of the current build
Type: String
AllowedValues:
- dev
- prod
FaunadbSecret:
Description: Faunadb secret
Type: String
# More info about Globals: /~https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Layers:
- !Ref dependencies
MemorySize: 4096
Runtime: nodejs14.x
EventInvokeConfig:
MaximumRetryAttempts: 0
Environment:
Variables:
stage: !Ref Stage
faunadbSecret: !Ref FaunadbSecret
appPackageName: !FindInMap
- stageVars
- !Ref Stage
- appPackageName
usersBucket: !Join
- "-"
- - !Ref Stage
- "proj-pronetplat-aa-users"
fn_invokeApi: !Join
- '-'
- - !Ref Stage
- "proj-pronetplat-aa-invokeApi"
Resources:
# Functions
httpApi:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: makefile
Properties:
Handler: httpApi/index.handler
Timeout: 30
FunctionName: !Join
- '-'
- - !Ref Stage
- "proj-pronetplat-aa-httpApi"
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref invokeApi
- S3CrudPolicy:
BucketName: !Join
- "-"
- - !Ref Stage
- "proj-pronetplat-aa-users"
Events:
ApiEvent:
Type: Api
Properties:
Method: any
Path: /{proxy+}
RestApiId: !Ref mainBackendApi
invokeApi:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: makefile
Properties:
Handler: invokeApi/index.handler
Timeout: 900
FunctionName: !Join
- '-'
- - !Ref Stage
- "proj-pronetplat-aa-invokeApi"
Policies:
- LambdaInvokePolicy:
FunctionName: !Join
- '-'
- - !Ref Stage
- "proj-pronetplat-aa-invokeApi"
- Statement:
- Action:
- ses:SendEmail
Effect: Allow
Resource: "*"
- S3CrudPolicy:
BucketName: !Join
- "-"
- - !Ref Stage
- "proj-pronetplat-aa-users"
- Statement:
- Action:
- execute-api:ManageConnections
Effect: Allow
Resource: "*"
Events:
newProfilePicture:
Type: S3
Properties:
Bucket: !Ref usersS3Bucket
Events: s3:ObjectCreated:Put
Filter:
S3Key:
Rules:
- Name: prefix
Value: newProfilePicture_
newEventCoverPicture:
Type: S3
Properties:
Bucket: !Ref usersS3Bucket
Events: s3:ObjectCreated:Put
Filter:
S3Key:
Rules:
- Name: prefix
Value: newEventCoverPicture_
# Websocket
webSocketCloudfront:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
IPV6Enabled: true
HttpVersion: http2
Aliases:
- !Sub
- ${webSocketSubDomain}.entrepic.com
- webSocketSubDomain: !FindInMap
- stageVars
- !Ref Stage
- webSocketSubDomain
Origins:
- Id: webSocketOrigin
DomainName: !Sub ${webSocket}.execute-api.${AWS::Region}.amazonaws.com
OriginPath: !Sub /${Stage}
CustomOriginConfig:
OriginProtocolPolicy: https-only
OriginSSLProtocols:
- TLSv1.2
DefaultCacheBehavior:
AllowedMethods: ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
CachePolicyId: ffac0a92-6907-4751-84fc-c9cd1af51234
OriginRequestPolicyId: 38846f4d-af2f-411d-aeb5-ce2afcfe00d2
TargetOriginId: webSocketOrigin
ViewerProtocolPolicy: https-only
ViewerCertificate:
AcmCertificateArn: arn:aws:acm:us-east-1:127336369406:certificate/ffe2d370-424a-4c2b-a944-53a74a8c1b97
MinimumProtocolVersion: TLSv1.2_2018
SslSupportMethod: sni-only
webSocketCustomDomainRoute53:
Type: AWS::Route53::RecordSet
Properties:
Type: A
HostedZoneId: Z0450109WGLMG82TZFOY
Name: !Sub
- ${webSocketSubDomain}.entrepic.com
- webSocketSubDomain: !FindInMap
- stageVars
- !Ref Stage
- webSocketSubDomain
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2 # constant for cloudfront
DNSName: !GetAtt webSocketCloudfront.DomainName
webSocket:
Type: AWS::ApiGatewayV2::Api
Properties:
Name: !Join
- '-'
- - !Ref Stage
- "proj-pronetplat-aa-webSocket"
ProtocolType: WEBSOCKET
RouteSelectionExpression: "$request.body.action"
webSocketDeployment:
Type: AWS::ApiGatewayV2::Deployment
DependsOn:
- connectRoute
- disconnectRoute
- defaultWebSocketRoute
Properties:
ApiId: !Ref webSocket
webSocketStage:
Type: AWS::ApiGatewayV2::Stage
Properties:
StageName: !Ref Stage
DeploymentId: !Ref webSocketDeployment
ApiId: !Ref webSocket
webSocketApi:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: makefile
Properties:
Handler: webSocketApi/index.handler
Timeout: 30
Policies:
- Statement:
- Action:
- execute-api:ManageConnections
Effect: Allow
Resource: "*"
FunctionName: !Join
- '-'
- - !Ref Stage
- "proj-pronetplat-aa-webSocketApi"
webSocketApiPermission:
Type: AWS::Lambda::Permission
DependsOn:
- webSocket
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref webSocketApi
Principal: apigateway.amazonaws.com
connectInteg:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref webSocket
Description: Connect Integration
IntegrationType: AWS_PROXY
IntegrationUri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${webSocketApi.Arn}/invocations
connectRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref webSocket
RouteKey: $connect
AuthorizationType: NONE
OperationName: ConnectRoute
Target: !Join
- '/'
- - 'integrations'
- !Ref connectInteg
disconnectInteg:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref webSocket
Description: Disconnect Integration
IntegrationType: AWS_PROXY
IntegrationUri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${webSocketApi.Arn}/invocations
disconnectRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref webSocket
RouteKey: $disconnect
AuthorizationType: NONE
OperationName: DisconnectRoute
Target: !Join
- '/'
- - 'integrations'
- !Ref disconnectInteg
defaultWebSocketRouteInteg:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref webSocket
Description: any websocket routes
IntegrationType: AWS_PROXY
IntegrationUri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${webSocketApi.Arn}/invocations
defaultWebSocketRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref webSocket
RouteKey: $default
AuthorizationType: NONE
OperationName: defaultWebSocketRoute
Target: !Join
- '/'
- - 'integrations'
- !Ref defaultWebSocketRouteInteg
# Lambda layers
dependencies:
Type: AWS::Serverless::LayerVersion
Metadata:
BuildMethod: makefile
Properties:
CompatibleRuntimes:
- nodejs14.x
ContentUri: dependencies
LayerName: proj-pronetplat-aa
RetentionPolicy: Delete
# API
mainBackendCloudfront:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
IPV6Enabled: true
HttpVersion: http2
Aliases:
- !Sub
- ${apiSubDomain}.entrepic.com
- apiSubDomain: !FindInMap
- stageVars
- !Ref Stage
- apiSubDomain
Origins:
- Id: mainBackendApiOrigin
DomainName: !Sub ${mainBackendApi}.execute-api.${AWS::Region}.amazonaws.com
OriginPath: !Sub /${Stage}
CustomOriginConfig:
OriginProtocolPolicy: https-only
OriginSSLProtocols:
- TLSv1.2
DefaultCacheBehavior:
AllowedMethods: ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
CachePolicyId: ffac0a92-6907-4751-84fc-c9cd1af51234
OriginRequestPolicyId: 7404f366-e3d3-47ca-8111-268449baa26e
TargetOriginId: mainBackendApiOrigin
ViewerProtocolPolicy: https-only
ViewerCertificate:
AcmCertificateArn: arn:aws:acm:us-east-1:127336369406:certificate/ffe2d370-424a-4c2b-a944-53a74a8c1b97
MinimumProtocolVersion: TLSv1.2_2018
SslSupportMethod: sni-only
mainBackendCloudfrontCustomDomainRoute53:
Type: AWS::Route53::RecordSet
Properties:
Type: A
HostedZoneId: Z0450109WGLMG82TZFOY
Name: !Sub
- ${apiSubDomain}.entrepic.com
- apiSubDomain: !FindInMap
- stageVars
- !Ref Stage
- apiSubDomain
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2 # constant for cloudfront
DNSName: !GetAtt mainBackendCloudfront.DomainName
mainBackendApi:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref Stage
EndpointConfiguration:
Type: REGIONAL
# @todo re-add waf again some time in the future
# mainBackendWebAcl:
# Type: AWS::WAFv2::WebACL
# Properties:
# DefaultAction:
# Block: {}
# Scope: CLOUDFRONT
# VisibilityConfig:
# CloudWatchMetricsEnabled: true
# MetricName: !Join
# - "-"
# - - !Ref Stage
# - "proj-pronetplat-aa-users"
# - webacl
# SampledRequestsEnabled: true
# Rules:
# - Action:
# Block: {}
# Name: RateLimit
# Statement:
# RateBasedStatement:
# AggregateKeyType: IP
# Limit: 1500
# VisibilityConfig:
# CloudWatchMetricsEnabled: true
# MetricName: !Join
# - "-"
# - - !Ref Stage
# - "proj-pronetplat-aa-users"
# - webacl-rate-limit
# SampledRequestsEnabled: true
# Priority: 0
# # - Action:
# # Allow: {}
# # Name: AllowedCounties
# # Statement:
# # GeoMatchStatement:
# # CountryCodes:
# # - AU
# # - PH
# # - US
# # - CA
# # - GB
# # VisibilityConfig:
# # CloudWatchMetricsEnabled: true
# # MetricName: !Join
# # - "-"
# # - - !Ref Stage
# # - "proj-pronetplat-aa-users"
# # - webacl-allowed-countries
# # SampledRequestsEnabled: true
# # Priority: 1
# mainBackendWebAclAssociations:
# Type: AWS::WAFv2::WebACLAssociation
# Properties:
# ResourceArn: !Sub "arn:aws:apigateway:${AWS::Region}::/restapis/${mainBackendApi}/stages/${Stage}"
# WebACLArn: !GetAtt mainBackendWebAcl.Arn
# S3 buckets
usersS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Join
- "-"
- - !Ref Stage
- "proj-pronetplat-aa-users"
CorsConfiguration:
CorsRules:
- AllowedHeaders:
- "*"
AllowedMethods:
- GET
- PUT
- HEAD
AllowedOrigins:
- "*"
MetricsConfigurations:
- Id: "profilePicture_"
Prefix: "profilePicture_"
- Id: "newProfilePicture_"
Prefix: "newProfilePicture_"
AccelerateConfiguration:
AccelerationStatus: Enabled
Tags:
- Key: "what"
Value: "proj-pronetplat-aa users s3 bucket"
Outputs:
mainBackendApi:
Description: Rest API Gateway endpoint
Value: !Sub https://${mainBackendApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}
mainBackendCloudfront:
Description: Rest API cloudfront endpoint
Value: !Sub https://${mainBackendCloudfront.DomainName}
mainBackendApiCustomDomain:
Description: Rest API Gateway custom domain
Value: !Sub
- https://${apiSubDomain}.entrepic.com
- apiSubDomain: !FindInMap
- stageVars
- !Ref Stage
- apiSubDomain
webSocket:
Description: Websocket endpoint
Value: !Sub ${webSocket.ApiEndpoint}/${Stage}
webSocketCloudfront:
Description: Websocket cloudfront endpoint
Value: !Sub https://${webSocketCloudfront.DomainName}
webSocketCustomDomain:
Description: Websocket custom domain
Value: !Sub
- wss://${webSocketSubDomain}.entrepic.com
- webSocketSubDomain: !FindInMap
- stageVars
- !Ref Stage
- webSocketSubDomain