-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
678 lines (643 loc) · 19.1 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: velinfo.fr serveless application
Globals:
Function:
Timeout: 30
Handler: app.lambdaHandler
Runtime: nodejs14.x
Environment:
Variables:
NODE_OPTIONS: --enable-source-maps
Parameters:
CDNCertificateArn:
Type: String
CDNApiCertificateArn:
Type: String
HostedZoneId:
Type: String
TwitterConsumerKey:
Type: String
TwitterConsumerSecret:
Type: String
TwitterAccessToken:
Type: String
TwitterAccessTokenSecret:
Type: String
AlarmNotificationArn:
Type: String
Resources:
# Lambdas
FetchStationsContent:
Type: AWS::Serverless::Function
DependsOn: StationsContentTable
Properties:
FunctionName: FetchStationsContent
Description: Fetch the station content from the Paris API every minute and update the current stations content dynamo table
CodeUri: lambda/FetchStationsContent
MemorySize: 160
Events:
EveryMinute:
Type: Schedule
Properties:
Schedule: rate(1 minute)
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref StationsContentTable
FetchStationsCharacteristics:
Type: AWS::Serverless::Function
DependsOn: StationCharacteristicsTable
Properties:
FunctionName: FetchStationsCharacteristics
Description: Fetch the station characteristics from the Velib API every hour and update the current characteristics dynamo table
CodeUri: lambda/FetchStationsCharacteristics
MemorySize: 160
Events:
EveryHour:
Type: Schedule
Properties:
Schedule: rate(1 hour)
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref StationCharacteristicsTable
ComputeUsage:
Type: AWS::Serverless::Function
DependsOn:
- StationsContentTable
- StationUsageStatisticsTable
- DailyNetworkUsageStatisticsTable
Properties:
FunctionName: ComputeUsage
Description: Process every update of the current content table to compute usage stats for each station
CodeUri: lambda/ComputeUsage
Events:
StreamContent:
Type: DynamoDB
Properties:
Stream:
!GetAtt StationsContentTable.StreamArn
StartingPosition: LATEST
BatchSize: 1
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref StationUsageStatisticsTable
- DynamoDBCrudPolicy:
TableName: !Ref DailyNetworkUsageStatisticsTable
ComputeMedianUsage:
Type: AWS::Serverless::Function
DependsOn:
- StationUsageStatisticsTable
- MedianUsageTable
- DailyNetworkUsagePredictionsTable
Properties:
FunctionName: ComputeMedianUsage
Description: Compute the median usage per week day and 5 minute timeslot
CodeUri: lambda/ComputeMedianUsage
Events:
EveryHour:
Type: Schedule
Properties:
Schedule: rate(5 minutes)
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref StationUsageStatisticsTable
- DynamoDBCrudPolicy:
TableName: !Ref MedianUsageTable
- DynamoDBCrudPolicy:
TableName: !Ref DailyNetworkUsagePredictionsTable
ComputeStationsStates:
Type: AWS::Serverless::Function
DependsOn:
- StationStateTable
- StationsContentTable
- StationUsageStatisticsTable
- MedianUsageTable
Properties:
FunctionName: ComputeStationsStates
Description: Compute the state of each station
CodeUri: lambda/ComputeStationsStates
MemorySize: 160
Environment:
Variables:
LOCKED_THRESHOLD_MINUTES_MIN: 30
LOCKED_ACTIVITY_THRESHOLD: 30
UNLOCKED_ACTIVITY_THRESHOLD: 2
GLOBAL_RATIO_MIN: 0
GLOBAL_RATIO_MAX: 2
Events:
StreamContent:
Type: DynamoDB
Properties:
Stream:
!GetAtt StationsContentTable.StreamArn
StartingPosition: LATEST
BatchSize: 1
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref StationStateTable
- DynamoDBCrudPolicy:
TableName: !Ref StationStateChangesTable
- DynamoDBCrudPolicy:
TableName: !Ref StationUsageStatisticsTable
- DynamoDBCrudPolicy:
TableName: !Ref MedianUsageTable
GetStations:
Type: AWS::Serverless::Function
DependsOn:
- StationStateTable
- StationsContentTable
- StationCharacteristicsTable
Properties:
FunctionName: GetStations
Description: Get the current stations states
CodeUri: lambda/GetStations
MemorySize: 1769 #minimal memory to have a full vCPU
Tracing: Active
Events:
GetApi:
Type: HttpApi
Properties:
ApiId: !Ref HttpApi
Path: /stations
Method: GET
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref StationStateTable
- DynamoDBCrudPolicy:
TableName: !Ref StationsContentTable
- DynamoDBCrudPolicy:
TableName: !Ref StationCharacteristicsTable
GetPredictionByStations:
Type: AWS::Serverless::Function
DependsOn:
- MedianUsageTable
Properties:
FunctionName: GetPredictionByStations
Description: Get todays predicted activity for each stations
CodeUri: lambda/GetPredictionByStations
MemorySize: 1769 #minimal memory to have a full vCPU
Tracing: Active
Events:
GetApi:
Type: HttpApi
Properties:
ApiId: !Ref HttpApi
Path: /prediction/by-station
Method: GET
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref MedianUsageTable
GetActivityByStations:
Type: AWS::Serverless::Function
DependsOn:
- StationUsageStatisticsTable
Properties:
FunctionName: GetActivityByStations
Description: Get todays activity for each stations
CodeUri: lambda/GetActivityByStations
MemorySize: 1769 #minimal memory to have a full vCPU
Tracing: Active
Events:
GetApi:
Type: HttpApi
Properties:
ApiId: !Ref HttpApi
Path: /activity/by-station
Method: GET
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref StationUsageStatisticsTable
GetStateChanges:
Type: AWS::Serverless::Function
DependsOn:
- StationStateChangesTable
Properties:
FunctionName: GetStateChanges
Description: Get last station state changes
CodeUri: lambda/GetStateChanges
MemorySize: 1769 #minimal memory to have a full vCPU
Tracing: Active
Events:
GetApi:
Type: HttpApi
Properties:
ApiId: !Ref HttpApi
Path: /state-changes
Method: GET
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref StationStateChangesTable
GetNetworkActivity:
Type: AWS::Serverless::Function
DependsOn:
- DailyNetworkUsageStatisticsTable
Properties:
FunctionName: GetNetworkActivity
Description: Get today's network's activity
CodeUri: lambda/GetNetworkActivity
MemorySize: 1769 #minimal memory to have a full vCPU
Tracing: Active
Events:
GetApi:
Type: HttpApi
Properties:
ApiId: !Ref HttpApi
Path: /activity
Method: GET
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref DailyNetworkUsageStatisticsTable
GetNetworkPrediction:
Type: AWS::Serverless::Function
DependsOn:
- DailyNetworkUsagePredictionsTable
Properties:
FunctionName: GetNetworkPrediction
Description: Get today's network's predictions
CodeUri: lambda/GetNetworkPrediction
MemorySize: 1769 #minimal memory to have a full vCPU
Tracing: Active
Events:
GetApi:
Type: HttpApi
Properties:
ApiId: !Ref HttpApi
Path: /prediction
Method: GET
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref DailyNetworkUsagePredictionsTable
PublishStateChangeTweet:
Type: AWS::Serverless::Function
Properties:
FunctionName: PublishStateChangeTweet
Description: Tweet when a station state change
CodeUri: lambda/PublishStateChangeTweet
Environment:
Variables:
TWITTER_CONSUMER_KEY: !Ref TwitterConsumerKey
TWITTER_CONSUMER_SECRET: !Ref TwitterConsumerSecret
TWITTER_ACCESS_TOKEN: !Ref TwitterAccessToken
TWITTER_ACCESS_TOKEN_SECRET: !Ref TwitterAccessTokenSecret
Events:
StreamStateChanges:
Type: DynamoDB
Properties:
Stream:
!GetAtt StationStateChangesTable.StreamArn
StartingPosition: LATEST
BatchSize: 1
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref StationCharacteristicsTable
# Dynamo tables
StationsContentTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: StationsContent
KeySchema:
- AttributeName: id
KeyType: HASH
- AttributeName: dateTime
KeyType: RANGE
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: dateTime
AttributeType: S
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 4
StreamSpecification:
StreamViewType: NEW_IMAGE
TimeToLiveSpecification:
AttributeName : timetolive
Enabled : true
StationCharacteristicsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: StationsCharacteristics
KeySchema:
- AttributeName: id
KeyType: HASH
- AttributeName: dateTime
KeyType: RANGE
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: dateTime
AttributeType: S
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 3
TimeToLiveSpecification:
AttributeName : timetolive
Enabled : true
StationUsageStatisticsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: StationUsageStatistics
KeySchema:
- AttributeName: day
KeyType: HASH
- AttributeName: timeslot
KeyType: RANGE
AttributeDefinitions:
- AttributeName: day
AttributeType: S
- AttributeName: timeslot
AttributeType: S
ProvisionedThroughput:
ReadCapacityUnits: 9
WriteCapacityUnits: 1
TimeToLiveSpecification:
AttributeName : timetolive
Enabled : true
DailyNetworkUsageStatisticsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: DailyNetworkUsageStatistics
KeySchema:
- AttributeName: day
KeyType: HASH
AttributeDefinitions:
- AttributeName: day
AttributeType: S
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TimeToLiveSpecification:
AttributeName : timetolive
Enabled : true
DailyNetworkUsagePredictionsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: DailyNetworkUsagePredictions
KeySchema:
- AttributeName: weekday
KeyType: HASH
AttributeDefinitions:
- AttributeName: weekday
AttributeType: N
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TimeToLiveSpecification:
AttributeName : timetolive
Enabled : true
MedianUsageTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: MedianUsage
KeySchema:
- AttributeName: weekday
KeyType: HASH
- AttributeName: timeslot
KeyType: RANGE
AttributeDefinitions:
- AttributeName: weekday
AttributeType: N
- AttributeName: timeslot
AttributeType: S
ProvisionedThroughput:
ReadCapacityUnits: 9
WriteCapacityUnits: 1
StationStateTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: StationState
KeySchema:
- AttributeName: id
KeyType: HASH
AttributeDefinitions:
- AttributeName: id
AttributeType: S
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 4
StationStateChangesTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: StationStateChanges
KeySchema:
- AttributeName: day
KeyType: HASH
- AttributeName: datetime
KeyType: RANGE
AttributeDefinitions:
- AttributeName: day
AttributeType: S
- AttributeName: datetime
AttributeType: S
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TimeToLiveSpecification:
AttributeName : timetolive
Enabled : true
StreamSpecification:
StreamViewType: NEW_IMAGE
#API
HttpApi:
Type: AWS::Serverless::HttpApi
Properties:
CorsConfiguration:
AllowOrigins:
- "https://www.velinfo.fr"
#Website
WebsiteS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: velinfo-frontend
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: index.html
CDNOriginIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: "Cloudfront Origin identity for www.velinfo.Fr"
WebsiteS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref WebsiteS3Bucket
PolicyDocument:
Statement:
- Action:
- "s3:GetObject"
Effect: Allow
Principal:
AWS: !Sub "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${CDNOriginIdentity}"
Resource: !Sub "arn:aws:s3:::${WebsiteS3Bucket}/*"
Version: "2012-10-17"
CDN:
Type: "AWS::CloudFront::Distribution"
Properties:
DistributionConfig:
Aliases:
- 'www.velinfo.fr'
HttpVersion: http2
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
CachedMethods:
- GET
- HEAD
Compress: true
ForwardedValues:
QueryString: True
TargetOriginId: website
ViewerProtocolPolicy: redirect-to-https
DefaultRootObject: index.html
CustomErrorResponses:
- ErrorCachingMinTTL: 10
ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /index.html
- ErrorCachingMinTTL: 10
ErrorCode: 403
ResponseCode: 200
ResponsePagePath: /index.html
Enabled: True
Origins:
- DomainName: !GetAtt WebsiteS3Bucket.RegionalDomainName
Id: website
S3OriginConfig:
OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CDNOriginIdentity}"
PriceClass: PriceClass_100
ViewerCertificate:
AcmCertificateArn: !Ref CDNCertificateArn
MinimumProtocolVersion: TLSv1.2_2018
SslSupportMethod: sni-only
ApiCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: ApiCachePolicy
DefaultTTL: 60
MinTTL: 60
MaxTTL: 60
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
EnableAcceptEncodingGzip: true
HeadersConfig:
HeaderBehavior: whitelist
Headers:
- Access-Control-Request-Headers
- Access-Control-Request-Method
- Origin
QueryStringsConfig:
QueryStringBehavior: none
ApiCDN:
Type: "AWS::CloudFront::Distribution"
Properties:
DistributionConfig:
Aliases:
- 'api.velinfo.fr'
HttpVersion: http2
DefaultCacheBehavior:
CachePolicyId: !Ref ApiCachePolicy
AllowedMethods:
- GET
- HEAD
- OPTIONS
CachedMethods:
- GET
- HEAD
- OPTIONS
Compress: true
ForwardedValues:
QueryString: false
Cookies:
Forward: none
TargetOriginId: api
ViewerProtocolPolicy: redirect-to-https
Enabled: True
Origins:
- DomainName: !Sub "${HttpApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}"
Id: api
CustomOriginConfig:
OriginProtocolPolicy: https-only
PriceClass: PriceClass_100
ViewerCertificate:
AcmCertificateArn: !Ref CDNApiCertificateArn
MinimumProtocolVersion: TLSv1.2_2018
SslSupportMethod: sni-only
DNS:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneId: !Ref HostedZoneId
RecordSets:
- Name: !Sub 'www.velinfo.fr'
Type: A
AliasTarget:
DNSName: !GetAtt CDN.DomainName
HostedZoneId: Z2FDTNDATAQYW2 #CloudFront official hosted zone
- Name: !Sub 'api.velinfo.fr'
Type: A
AliasTarget:
DNSName: !GetAtt ApiCDN.DomainName
HostedZoneId: Z2FDTNDATAQYW2 #CloudFront official hosted zone
# Alerts
FetchStationsContentError:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: FetchStationsContentError
AlarmActions:
- !Ref AlarmNotificationArn
MetricName: Errors
Namespace: AWS/Lambda
Statistic: Sum
Dimensions:
- Name: FunctionName
Value: FetchStationsContent
Period: 300
EvaluationPeriods: 1
Threshold: 2
ComparisonOperator: GreaterThanOrEqualToThreshold
FetchStationsCharacteristicsError:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: FetchStationsCharacteristicsError
AlarmActions:
- !Ref AlarmNotificationArn
MetricName: Errors
Namespace: AWS/Lambda
Statistic: Sum
Dimensions:
- Name: FunctionName
Value: FetchStationsCharacteristics
Period: 21600
EvaluationPeriods: 1
Threshold: 2
ComparisonOperator: GreaterThanOrEqualToThreshold
AnyLambdaError:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: AnyLambdaError
AlarmActions:
- !Ref AlarmNotificationArn
MetricName: Errors
Namespace: AWS/Lambda
Statistic: Sum
Dimensions: []
Period: 900
EvaluationPeriods: 1
DatapointsToAlarm: 1
Threshold: 2
ComparisonOperator: GreaterThanOrEqualToThreshold
Outputs:
WebsiteBucketUrl:
Description: 'S3 Bucket Url'
Value: !GetAtt 'WebsiteS3Bucket.WebsiteURL'
ApiUrl:
Description: 'HTTP API Url'
Value: !Sub "https://${HttpApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/"
CDNDomainName:
Description: 'HTTP API Url'
Value: !GetAtt CDN.DomainName
ApiCDNDomainName:
Description: 'HTTP API Url'
Value: !GetAtt ApiCDN.DomainName