-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack.yml
1105 lines (1057 loc) · 32.5 KB
/
stack.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
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file describes the serverless infrastructure stack that serves as the back-end for the Litter Map application.
#
# Read about the concept of "infrastructure as code":
#
# https://learn.microsoft.com/en-us/devops/deliver/what-is-infrastructure-as-code
#
# Quick reference links:
#
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy.html
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-resources-and-properties.html
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/metadata-section-structure.html
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html
# https://theburningmonk.com/cloudformation-ref-and-getatt-cheatsheet/
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: Global location submission and retrieval system
#
# Common attributes can be set for various resource types, however they are only somewhat overridable:
#
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html#sam-specification-template-anatomy-globals-overrideable
#
Globals:
Function:
Handler: index.handler
Runtime: nodejs16.x
CodeUri: functions
Architectures:
- arm64 # Choose the ARM processor (cost effective)
Resources:
API:
Type: AWS::Serverless::HttpApi
Properties:
Description: Backend API
StageName: !Ref APIStageName
Auth:
Authorizers: {}
CommonLib:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: lib-common
ContentUri: layers/common
CompatibleRuntimes:
- nodejs14.x
RetentionPolicy: Delete
Metadata:
BuildMethod: makefile
PostgresLib:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: lib-postgres
ContentUri: layers/postgres
CompatibleRuntimes:
- nodejs14.x
RetentionPolicy: Delete
Metadata:
BuildMethod: makefile
DefaultFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: default-response
Description: Lets you know you've reached an invalid endpoint
InlineCode: |-
exports.handler = async () => {
return {
statusCode: 404,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: "This endpoint does not exist. Please pick up that litter."
})
}
}
Events:
Default:
Type: HttpApi
Properties:
ApiId: !Ref API
ScaleImageFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: scale-image
Description: Generate a scaled version of an image in the media store
CodeUri: functions/scale-image/build/scale-image.zip
Handler: scale-image # The name of the main executable file inside the zip package
Runtime: provided.al2 # Amazon Linux 2 runtime is required for ARM binaries
Role: !GetAtt ScaleImageFunctionRole.Arn
Environment:
Variables:
MEDIA_BUCKET: !Ref MediaBucket
# DEBUG_OUTPUT: 1 # Return debug info instead of actual image
Events:
ScaleImage:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /scale-image
Method: GET
LogEventFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: log-event
Description: Record a single event in the event log table
Role: !GetAtt LogEventFunctionRole.Arn
Layers:
- !Ref CommonLib
Environment:
Variables:
TABLE_NAME: !Ref EventsTable
Metadata:
BuildMethod: makefile
AddLocationFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: add-location
Description: Add a location
Role: !GetAtt AddLocationFunctionRole.Arn
Layers:
- !Ref CommonLib
- !Ref PostgresLib
Environment:
Variables:
SESSIONS_TABLE: !Ref SessionsTable
SESSION_LIFETIME_IN_DAYS: !Ref SessionLifetimeInDays
PGHOST: !If [DoRDS, !GetAtt MainDB.Endpoint.Address, ""]
PGPORT: !If [DoRDS, !GetAtt MainDB.Endpoint.Port, ""]
PGDATABASE: !Ref DBName
PGUSER: writer
PGPASSWORD: !Ref DBWriterPassword
DB_GEOMETRY_TYPE_OID: !Ref DBGeometryTypeObjectID
MEDIA_BUCKET: !Ref MediaBucket
ALLOW_ANONYMOUS_SUBMIT: !Ref AllowAnonymousSubmit
Events:
AddLocation:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /add
Method: POST
Metadata:
BuildMethod: makefile
DanielFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: daniel
Description: Add own location to map
Role: !GetAtt DanielFunctionRole.Arn
Layers:
- !Ref CommonLib
Environment:
Variables:
MEDIA_BUCKET: !Ref MediaBucket
Events:
Daniel:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /daniel
Method: POST
Metadata:
BuildMethod: makefile
GetLocationFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: get-location
Description: Retrieve a location by its id
Role: !GetAtt GetLocationFunctionRole.Arn
Layers:
- !Ref CommonLib
- !Ref PostgresLib
Environment:
Variables:
PGHOST: !If [DoRDS, !GetAtt MainDB.Endpoint.Address, ""]
PGPORT: !If [DoRDS, !GetAtt MainDB.Endpoint.Port, ""]
PGDATABASE: !Ref DBName
PGUSER: reader
PGPASSWORD: !Ref DBReaderPassword
DB_GEOMETRY_TYPE_OID: !Ref DBGeometryTypeObjectID
USERS_TABLE: !Ref UsersTable
Events:
GetLocationById:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /id/{id}
Method: GET
GetLocationsByRadius:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /radius
Method: GET
Metadata:
BuildMethod: makefile
InfoFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: info
Description: Look up privileged information
Role: !GetAtt InfoFunctionRole.Arn
Layers:
- !Ref CommonLib
Environment:
Variables:
SESSIONS_TABLE: !Ref SessionsTable
SESSION_LIFETIME_IN_DAYS: !Ref SessionLifetimeInDays
USERS_TABLE: !Ref UsersTable
Events:
GetProfileInfo:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /profile
Method: GET
Metadata:
BuildMethod: makefile
CloudfrontGeolocationFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: cloudfront-geolocation
Description: Look up privileged information
Role: !GetAtt CloudfrontGeolocationFunctionRole.Arn
Layers:
- !Ref CommonLib
Events:
GetProfileInfo:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /mylocation
Method: GET
Metadata:
BuildMethod: makefile
LoginFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: login
Description: Login proxy (redirects to third-party sign-in dialog)
Role: !GetAtt LoginFunctionRole.Arn
Layers:
- !Ref CommonLib
Environment:
Variables:
SESSIONS_TABLE: !Ref SessionsTable
SESSION_LIFETIME_IN_DAYS: !Ref SessionLifetimeInDays
CLIENTID_GOOGLE: !Ref GoogleClientId
Events:
Login:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /login/{service}
Method: GET
Logout:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /logout
Method: GET
Metadata:
BuildMethod: makefile
AuthFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: auth
Description: Receive authorization from third-party sign-in dialog and start an authenticated user session
Role: !GetAtt AuthFunctionRole.Arn
Layers:
- !Ref CommonLib
Environment:
Variables:
SESSIONS_TABLE: !Ref SessionsTable
SESSION_LIFETIME_IN_DAYS: !Ref SessionLifetimeInDays
USERS_TABLE: !Ref UsersTable
CLIENTID_GOOGLE: !Ref GoogleClientId
CLIENTSECRET_GOOGLE: !Ref GoogleClientSecret
Events:
Authentication:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /auth/{service}
Method: GET
Metadata:
BuildMethod: makefile
GetUploadLinkFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: get-upload-link
Description: Generate a signed upload link to the media bucket
Role: !GetAtt GetUploadLinkFunctionRole.Arn
Layers:
- !Ref CommonLib
Environment:
Variables:
SESSIONS_TABLE: !Ref SessionsTable
SESSION_LIFETIME_IN_DAYS: !Ref SessionLifetimeInDays
MEDIA_BUCKET: !Ref MediaBucket
MAX_UPLOAD_FILE_SIZE: !Ref MaxUploadFileSize
Events:
GetUploadLink:
Type: HttpApi
Properties:
ApiId: !Ref API
Path: /getuploadlink
Method: GET
Metadata:
BuildMethod: makefile
DBInitFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: db-init
Description: Administrative lambda that initilizes the main database
Role: !GetAtt DBAccessFunctionRole.Arn
Timeout: 10
Layers:
- !Ref PostgresLib
Environment:
Variables:
PGHOST: !If [DoRDS, !GetAtt MainDB.Endpoint.Address, ""]
PGPORT: !If [DoRDS, !GetAtt MainDB.Endpoint.Port, ""]
PGDATABASE: !Ref DBName
PGUSER: !Ref DBAdminUser
PGPASSWORD: !Ref DBAdminPassword
DB_WRITER_PASSWORD: !Ref DBWriterPassword
DB_READER_PASSWORD: !Ref DBReaderPassword
Metadata:
BuildMethod: makefile
DBRunFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: db-run
Description: Runs a database query (as administrator)
Role: !GetAtt DBAccessFunctionRole.Arn
Layers:
- !Ref CommonLib
- !Ref PostgresLib
Environment:
Variables:
PGHOST: !If [DoRDS, !GetAtt MainDB.Endpoint.Address, ""]
PGPORT: !If [DoRDS, !GetAtt MainDB.Endpoint.Port, ""]
PGDATABASE: !Ref DBName
DB_ADMIN: !Ref DBAdminUser
DB_ADMIN_PASSWORD: !Ref DBAdminPassword
DB_WRITER_PASSWORD: !Ref DBWriterPassword
DB_READER_PASSWORD: !Ref DBReaderPassword
Metadata:
BuildMethod: makefile
ScaleImageFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
AddLocationFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
DanielFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
GetLocationFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
InfoFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
CloudfrontGeolocationFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
LoginFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
AuthFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
GetUploadLinkFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
LogEventFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: eventlog-write
PolicyDocument:
Version: 2012-10-17
Statement:
- Action: dynamodb:PutItem
Resource: !GetAtt EventsTable.Arn
Effect: Allow
DBAccessFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
AllowDBAccessPolicy:
Type: AWS::IAM::Policy
Condition: DoRDS
Properties:
PolicyName: allow-rds-connect
PolicyDocument:
Version: 2012-10-17
Statement:
- Action: rds-db:connect
Resource: !Sub arn:aws:rds:${AWS::Region}:${AWS::AccountId}:db:${MainDB}
Effect: Allow
Roles:
- Ref: DBAccessFunctionRole
- Ref: AddLocationFunctionRole
- Ref: GetLocationFunctionRole
AllowUsersAccessPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: allow-users-access
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
Resource: !GetAtt UsersTable.Arn
Effect: Allow
Roles:
- Ref: InfoFunctionRole
- Ref: AuthFunctionRole
AllowUsersReadAccessPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: allow-users-read-access
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- dynamodb:GetItem
- dynamodb:BatchGetItem
Resource: !GetAtt UsersTable.Arn
Effect: Allow
Roles:
- Ref: GetLocationFunctionRole
AllowSessionsAccessPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: allow-sessions-access
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
Resource: !GetAtt SessionsTable.Arn
Effect: Allow
Roles:
- Ref: InfoFunctionRole
- Ref: LoginFunctionRole
- Ref: AuthFunctionRole
- Ref: GetUploadLinkFunctionRole
- Ref: AddLocationFunctionRole
AllowLogEventPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: allow-log-event
PolicyDocument:
Version: 2012-10-17
Statement:
- Action: lambda:InvokeFunction
Resource: !GetAtt LogEventFunction.Arn
Effect: Allow
Roles:
- Ref: LoginFunctionRole
- Ref: AuthFunctionRole
- Ref: AddLocationFunctionRole
- Ref: DanielFunctionRole
AllowGetPutMediaPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: allow-get-put-media
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- s3:ListBucket # Without this, nonexistent keys return Access Denied
Resource: !Sub "arn:aws:s3:::${MediaBucket}"
Effect: Allow
- Action:
- s3:GetObject
- s3:PutObject
- s3:PutObjectAcl
Resource: !Sub "arn:aws:s3:::${MediaBucket}/*"
Effect: Allow
Roles:
- Ref: ScaleImageFunctionRole
- Ref: DanielFunctionRole
AllowPutMediaPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: allow-put-media
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- s3:PutObject
- s3:PutObjectAcl
Resource: !Sub "arn:aws:s3:::${MediaBucket}/*"
Effect: Allow
Roles:
- Ref: GetUploadLinkFunctionRole
- Ref: DanielFunctionRole
AllowTagMediaPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: allow-tag-media
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- s3:PutObjectTagging
Resource: !Sub "arn:aws:s3:::${MediaBucket}/*"
Effect: Allow
Roles:
- Ref: GetUploadLinkFunctionRole
- Ref: AddLocationFunctionRole
- Ref: ScaleImageFunctionRole
- Ref: DanielFunctionRole
# Main database that stores the world
MainDB:
Type: AWS::RDS::DBInstance
Condition: DoRDS
Properties:
DBInstanceIdentifier: !Ref DBName
DBSnapshotIdentifier: !If [HasDBSnapshotIdentifier, !Ref DBSnapshotIdentifier, !Ref 'AWS::NoValue']
DBName: !Ref DBName
Engine: postgres
EngineVersion: !Ref DBPostgresVersion
AllowMajorVersionUpgrade: true
AutoMinorVersionUpgrade: true
DBInstanceClass: !Ref DBInstanceClass
AllocatedStorage: !Ref DBAllocatedStorage
BackupRetentionPeriod: 0 # no automated backups
MasterUsername: !Ref DBAdminUser
MasterUserPassword: !Ref DBAdminPassword
# Users
UsersTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: users
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id # Keyed by user id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: !Ref UsersTableReadCapacity
WriteCapacityUnits: !Ref UsersTableWriteCapacity
# Sessions
SessionsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: sessions
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id # Keyed by user id
KeyType: HASH
TimeToLiveSpecification:
AttributeName: expires_at
Enabled: true
ProvisionedThroughput:
ReadCapacityUnits: !Ref SessionsTableReadCapacity
WriteCapacityUnits: !Ref SessionsTableWriteCapacity
# Event log
EventsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: event-log
AttributeDefinitions:
- AttributeName: date_key
AttributeType: S
- AttributeName: time_key
AttributeType: S
KeySchema:
- AttributeName: date_key # Keyed by date followed by time
KeyType: HASH
- AttributeName: time_key
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: !Ref EventLogReadCapacity
WriteCapacityUnits: !Ref EventLogWriteCapacity
# S3 bucket that hosts the user uploaded media files
MediaBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
WebsiteConfiguration:
IndexDocument: " " # The documentation claims this is optional, but the deployment will fail if it's empty
RoutingRules:
- RoutingRuleCondition:
HttpErrorCodeReturnedEquals: 403 # not 404
KeyPrefixEquals: media/
RedirectRule:
HostName: !Ref Hostname # Deploying with this field blank, the redirect will be to the raw bucket web URL
Protocol: https
ReplaceKeyPrefixWith: api/scale-image?key=
HttpRedirectCode: 302
LifecycleConfiguration:
Rules:
# Schedule regular removal of unverified uploads
- Id: DeleteUnverified
ExpirationInDays: 1
Status: Enabled
TagFilters:
- Key: verified
Value: false
# Schedule removal of cached thumbnails
- Id: DeleteThumbnails
ExpirationInDays: 2 # TODO: Make this a configurable stack parameter
Status: Enabled
TagFilters:
- Key: temp
Value: ""
CorsConfiguration:
CorsRules:
- AllowedHeaders:
- "*"
AllowedMethods:
- GET
- POST
- HEAD
AllowedOrigins:
- "*" # TODO: In production, should be domain name
# S3 bucket that hosts the front-end (single page application)
WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: index.html # Single-page application handles routing on the front-end
WebsiteDelivery:
Type: AWS::CloudFront::Distribution
DependsOn:
- MediaBucket
- WebsiteBucket
Properties:
DistributionConfig:
Enabled: !If [HasCDN, true, false]
Origins:
- Id: BackendAPI
DomainName: !Sub ${API}.execute-api.${AWS::Region}.${AWS::URLSuffix}
CustomOriginConfig:
HTTPSPort: 443
OriginProtocolPolicy: https-only
- Id: Media
DomainName: !Sub ${MediaBucket}.s3-website-${AWS::Region}.amazonaws.com
CustomOriginConfig:
OriginProtocolPolicy: http-only # S3 "web" server only supports plain HTTP
ConnectionAttempts: 2
ConnectionTimeout: 2
- Id: FrontendApplication
DomainName: !Sub ${WebsiteBucket}.s3-website-${AWS::Region}.amazonaws.com
CustomOriginConfig:
OriginProtocolPolicy: http-only # S3 "web" server only supports plain HTTP
ConnectionAttempts: 1
ConnectionTimeout: 2
CacheBehaviors:
#
# Let api/* requests go to the API gateway
#
- PathPattern: !Sub ${APIStageName}/*
TargetOriginId: BackendAPI
AllowedMethods:
- GET
- HEAD
- POST
- PUT
- DELETE
- PATCH
- OPTIONS
ForwardedValues:
Headers:
# Specify headers that the back-end API needs to receive
- Referer
- CloudFront-Viewer-Latitude
- CloudFront-Viewer-Longitude
QueryString: true
Cookies:
Forward: all
MaxTTL: 0
MinTTL: 0
DefaultTTL: 0
Compress: true
ViewerProtocolPolicy: redirect-to-https
#
# Let media/* requests go to the media bucket
#
- PathPattern: media/*
TargetOriginId: Media
AllowedMethods:
- GET
- HEAD
ForwardedValues:
QueryString: false
Cookies:
Forward: none
DefaultTTL: 0 # Disable caching by default (objects will have their own Cache-Control metadata)
Compress: true
ViewerProtocolPolicy: redirect-to-https
DefaultCacheBehavior:
#
# Let default requests go to the website bucket
#
TargetOriginId: FrontendApplication
AllowedMethods:
- GET
- HEAD
ForwardedValues:
QueryString: false
Cookies:
Forward: none
Compress: true
ViewerProtocolPolicy: redirect-to-https
DefaultRootObject: index.html
HttpVersion: http2
IPV6Enabled: true
ViewerCertificate:
CloudFrontDefaultCertificate: true
Parameters:
APIStageName:
Description: API stage
Type: String
Default: api
DoNotDeployRDS:
Description: Do not deploy RDS
Type: Number
Default: 0
DBInstanceClass:
Description: Database instance class
Type: String
Default: db.t2.micro
DBPostgresVersion:
Description: PostgreSQL version
Type: String
Default: 12.7
DBAllocatedStorage:
Description: Allocated storage (in GB)
Type: Number
Default: 5
DBSnapshotIdentifier:
Description: Provide the database snapshot id if you don't want a new database
Type: String
DBName:
Description: Database name
Type: String
AllowedPattern: "[a-zA-Z][a-zA-Z0-9]*"
ConstraintDescription: Must begin with a letter and contain only alphanumeric characters
Default: littermap
DBAdminUser:
Description: Database admin username
Type: String
AllowedPattern: "[a-zA-Z][a-zA-Z0-9]*"
ConstraintDescription: Must begin with a letter and contain only alphanumeric characters
Default: litteradmin
DBAdminPassword:
Description: Database admin password
Type: String
MinLength: 8
MaxLength: 128
AllowedPattern: "[^\\s@\"\\/]*"
ConstraintDescription: 'The password must be 8 to 128 characters and can include any printable ASCII character except "/", """, or "@"'
Default: pristine
DBWriterPassword:
Description: Database write access password
Type: String
MinLength: 8
MaxLength: 128
AllowedPattern: "[^\\s@\"\\/]*"
ConstraintDescription: 'The password must be 8 to 128 characters and can include any printable ASCII character except "/", """, or "@"'
Default: recorder
DBReaderPassword:
Description: Database reader access password
Type: String
MinLength: 8
MaxLength: 128
AllowedPattern: "[^\\s@\"\\/]*"
ConstraintDescription: 'The password must be 8 to 128 characters and can include any printable ASCII character except "/", """, or "@"'
Default: observer
DBGeometryTypeObjectID:
Description: Geometry type object ID (provide this after the PostGIS extension has been initialized)
Type: Number
Default: -1
GoogleClientId:
Description: Client ID issued by Google for third-party sign-in authentication
Type: String
GoogleClientSecret:
Description: Client secret issued by Google for third-party sign-in authentication
Type: String
UsersTableReadCapacity:
Description: Users table provisioned read capacity
Type: Number
Default: 1
UsersTableWriteCapacity:
Description: Users table provisioned write capacity
Type: Number
Default: 1
SessionLifetimeInDays:
Description: How many days will browser sessions expire after they were created
Type: Number
Default: 3
SessionsTableReadCapacity:
Description: Session table provisioned read capacity
Type: Number
Default: 1
SessionsTableWriteCapacity:
Description: Session table provisioned write capacity
Type: Number
Default: 1
EventLogReadCapacity:
Description: Event log table provisioned read capacity
Type: Number
Default: 1
EventLogWriteCapacity:
Description: Event log table provisioned write capacity
Type: Number
Default: 1
AllowAnonymousSubmit:
Description: Allow users who are not logged in to submit a location
Type: Number
AllowedValues:
- 0
- 1
Default: 0
MaxUploadFileSize:
Description: Maximum allowed upload size
Type: Number
Default: 12582910
EnableCDN: