-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
1037 lines (997 loc) · 36.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
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 is an AWS Serverless Application Model (SAM) template
# It defines the "stack" of AWS resources needed to run the SAM app
# -----------------------------------------------------------------------------
AWSTemplateFormatVersion: "2010-09-09"
# -----------------------------------------------------------------------------
# This file is written in SAM syntax, and gets transfomed and expanded into a
# CloudFormation template. The CloudFormation template is what actually gets
# deployed. The main point is that SAM syntax is much simpler, with a lot less
# boilerplate around declaring resources like Lambda functions and their
# accompanying execution roles.
# -----------------------------------------------------------------------------
Transform: AWS::Serverless-2016-10-31
# -----------------------------------------------------------------------------
# This particular template defines the back end of the Tacocat photo gallery
# -----------------------------------------------------------------------------
Description: Tacocat Gallery image storage, database, and API
# -----------------------------------------------------------------------------
# Parameters that can be specified when deploying this stack
# This is the only way to define constants for use elsewhere in the template
# -----------------------------------------------------------------------------
Parameters:
Env:
Description: Environment for which the infrastructure will be deployed
Type: String
AllowedValues:
- dev
- test
- prod
ConstraintDescription: Must be either 'dev', 'test' or 'prod'
GalleryAppDomain:
Description: Domain of the Tacocat photo gallery web app
Type: String
AllowedValues:
- staging-pix.tacocat.com
- test-pix.tacocat.com
- pix.tacocat.com
ConstraintDescription: Must be either 'staging-pix.tacocat.com', 'test-pix.tacocat.com' or 'pix.tacocat.com'
DomainCertificateArn:
Description: ARN of the ACM certificate for the GalleryAppDomain domain
Type: String
AllowedValues:
- arn:aws:acm:us-east-1:010410881828:certificate/092ab57c-f87d-4c44-a72c-63cfbbb55800 # staging-pix.tacocat.com
- arn:aws:acm:us-east-1:010410881828:certificate/9fbd8ee9-f1d1-42a5-b97c-9713ce6e076d # test-pix.tacocat.com
- arn:aws:acm:us-east-1:010410881828:certificate/e142cda0-96db-4651-92fd-35dd9a52e9a2 # pix.tacocat.com
RedisHost:
Description: Hostname and port of the Redis search instance
Type: String
AllowedValues:
- redis-14907.c321.us-east-1-2.ec2.cloud.redislabs.com:14907 # dev
- redis-13894.c321.us-east-1-2.ec2.cloud.redislabs.com:13894 # prod
Default: redis-14907.c321.us-east-1-2.ec2.cloud.redislabs.com:14907
RedisSearchUsername:
Description: Name of user permitted to execute a search on Redis
Type: String
AllowedValues:
- devsearcher
- prodsearcher
ConstraintDescription: Must be devsearcher or prodsearcher
Default: devsearcher
RedisWriteUsername:
Description: Name of user permitted to write to Redis
Type: String
AllowedValues:
- devwriter
- prodwriter
ConstraintDescription: Must be devwriter or prodwriter
Default: devwriter
# -----------------------------------------------------------------------------
# Conditions are boolean conditions that can be used to control resources
# -----------------------------------------------------------------------------
Conditions:
IsProd: !Equals [ !Ref Env, "prod" ]
# -----------------------------------------------------------------------------
# Configuration properties inherited by all resources, such as Lambda functions
# -----------------------------------------------------------------------------
Globals:
Function:
Runtime: nodejs20.x
MemorySize: 256
Timeout: 100 # TODO decrease this; the default is 3!
LoggingConfig:
LogFormat: JSON
Environment:
Variables:
GALLERY_APP_DOMAIN: !Ref GalleryAppDomain
Api:
Name: !Sub Tacocat Gallery API - ${Env}
Domain:
DomainName: !Sub api.${GalleryAppDomain}
CertificateArn: !Ref DomainCertificateArn
Cors:
AllowMethods: "'GET,OPTIONS,HEAD,POST,PUT,PATCH,DELETE'"
AllowHeaders: "'Content-Type'"
AllowOrigin: !Sub "'https://${GalleryAppDomain}'"
AllowCredentials: true
# -----------------------------------------------------------------------------
# Resources are the actual AWS assets to be provisioned
# -----------------------------------------------------------------------------
Resources:
# -----------------------------------------------------------------------------
# S3 bucket for original images
# -----------------------------------------------------------------------------
OriginalImagesBucket:
Type: AWS::S3::Bucket
DeletionPolicy: !If [ "IsProd", "Retain", "Delete" ] # don't delete in prod
UpdateReplacePolicy: !If [ "IsProd", "Retain", "Delete" ] # don't delete in prod
Properties:
BucketName: !Sub ${AWS::StackName}-original-images
VersioningConfiguration:
Status: Enabled
LifecycleConfiguration:
Rules:
- Id: Delete Old Versions
NoncurrentVersionExpirationInDays: 24 # delete old versions after 24 days
ExpiredObjectDeleteMarker: true
Status: Enabled
CorsConfiguration:
CorsRules:
- AllowedHeaders:
- "*"
AllowedMethods:
- PUT # used by presigned URLs to upload images
AllowedOrigins:
# - "*"
- !Sub "https://${GalleryAppDomain}"
ExposedHeaders:
- ETag
- x-amz-version-id # Necessary so that client can receive version ID of uploaded image
# -----------------------------------------------------------------------------
# S3 bucket for derived images
# -----------------------------------------------------------------------------
DerivedImagesBucket:
Type: AWS::S3::Bucket
DeletionPolicy: !If [ "IsProd", "Retain", "Delete" ] # don't delete in prod
UpdateReplacePolicy: !If [ "IsProd", "Retain", "Delete" ] # don't delete in prod
Properties:
BucketName: !Sub ${AWS::StackName}-derived-images
# -----------------------------------------------------------------------------
# DynamoDB table containing the album and image metadata
# -----------------------------------------------------------------------------
GalleryItemDDBTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: !If [ "IsProd", "Retain", "Delete" ] # don't delete in prod
UpdateReplacePolicy: !If [ "IsProd", "Retain", "Delete" ] # don't delete in prod
Properties:
TableName: !Sub ${AWS::StackName}-items
AttributeDefinitions:
- AttributeName: parentPath
AttributeType: S
- AttributeName: itemName
AttributeType: S
KeySchema:
- AttributeName: parentPath
KeyType: HASH
- AttributeName: itemName
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 3
WriteCapacityUnits: 3
StreamSpecification:
StreamViewType: NEW_IMAGE
# -----------------------------------------------------------------------------
# CloudFront CDN distribution that serves the images
# -----------------------------------------------------------------------------
# CloudFront distibution
ImageDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
Comment: !Sub Images for ${AWS::StackName}
Aliases:
- !Sub img.${GalleryAppDomain}
ViewerCertificate:
AcmCertificateArn: !Ref DomainCertificateArn
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1.2_2021
Origins:
- Id: original-images-bucket
DomainName: !GetAtt OriginalImagesBucket.DomainName
S3OriginConfig:
OriginAccessIdentity: !Sub origin-access-identity/cloudfront/${OriginalImagesCloudFrontOriginAccessIdentity}
- Id: derived-images-bucket
DomainName: !GetAtt DerivedImagesBucket.DomainName
S3OriginConfig:
OriginAccessIdentity: !Sub origin-access-identity/cloudfront/${DerivedImagesCloudFrontOriginAccessIdentity}
- Id: generate-derived-image-lambda
DomainName: # parse DomainName from FunctionUrl which looks like 'https://12341232.lambda-url.on.aws/'
!Select [
2,
!Split [
"/",
!Select [
1,
!Split [
":",
!GetAtt GenerateDerivedImageFunctionUrl.FunctionUrl,
],
],
],
]
CustomOriginConfig:
OriginProtocolPolicy: https-only
CacheBehaviors:
- PathPattern: /i/*
TargetOriginId: derived-images-group
Compress: true
ViewerProtocolPolicy: redirect-to-https
CachePolicyId: !Ref ImmutableCachePolicy
ResponseHeadersPolicyId: !Ref ImmutableResponseHeadersPolicy
ForwardedValues:
QueryString: false
Cookies:
Forward: none
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt DerivedImagesUrlRewriteFunction.FunctionMetadata.FunctionARN
DefaultCacheBehavior:
TargetOriginId: original-images-bucket
Compress: true
ViewerProtocolPolicy: redirect-to-https
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6 # CachingOptimized managed policy - use this if not using a custom ForwardedValues
ForwardedValues:
QueryString: false
Cookies:
Forward: none
OriginGroups:
Quantity: 1
Items:
- Id: derived-images-group
FailoverCriteria:
StatusCodes:
Quantity: 2
Items: [403, 404] # S3 will return 403 instead of 404 when s3:ListBucket permission is not given
Members:
Quantity: 2
Items:
- OriginId: derived-images-bucket
- OriginId: generate-derived-image-lambda
# Set Cache-Control header to cache forever
ImmutableResponseHeadersPolicy:
Type: AWS::CloudFront::ResponseHeadersPolicy
Properties:
ResponseHeadersPolicyConfig:
Name: !Sub ImmutableResponseHeadersPolicy-${AWS::StackName}
Comment: !Sub Immutable response headers policy for ${AWS::StackName}
CustomHeadersConfig:
Items:
- Header: Cache-Control
Value: public, max-age=31536000, immutable
Override: true
# Caching policy to cache forever
ImmutableCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: !Sub ImmutableCachePolicy-${AWS::StackName}
Comment: Cache for a year
DefaultTTL: 31536000 # 1 year
MaxTTL: 31536000 # 1 year
MinTTL: 31536000 # 1 year
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
EnableAcceptEncodingBrotli: true
EnableAcceptEncodingGzip: true
HeadersConfig:
HeaderBehavior: none
QueryStringsConfig:
QueryStringBehavior: none
# Allow CloudFront to read from the original images bucket
OriginalImagesBucketCloudFrontReadPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref OriginalImagesBucket
PolicyDocument:
Statement:
- Action: s3:GetObject
Effect: Allow
Resource: !Sub arn:aws:s3:::${OriginalImagesBucket}/*
Principal:
CanonicalUser: !GetAtt OriginalImagesCloudFrontOriginAccessIdentity.S3CanonicalUserId
# CloudFront identity of the original image bucket
OriginalImagesCloudFrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !Ref OriginalImagesBucket
# Allow CloudFront to read from the derived images bucket
DerivedImagesBucketCloudFrontReadPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref DerivedImagesBucket
PolicyDocument:
Statement:
- Action: s3:GetObject
Effect: Allow
Resource: !Sub arn:aws:s3:::${DerivedImagesBucket}/*
Principal:
CanonicalUser: !GetAtt DerivedImagesCloudFrontOriginAccessIdentity.S3CanonicalUserId
# CloudFront identity of the derived image bucket
DerivedImagesCloudFrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !Ref DerivedImagesBucket
# CloudFront Function to rewrite URL to derived images bucket
DerivedImagesUrlRewriteFunction:
Type: AWS::CloudFront::Function
Properties:
Name: !Sub RewriteDerivedImagesUrl-${AWS::StackName}
AutoPublish: true
FunctionConfig:
Comment: !Sub Rewrite URL to derived images bucket for ${AWS::StackName}
Runtime: cloudfront-js-2.0
FunctionCode: |+2
function handler(event) {
const r = event.request;
const v = r.querystring['version'], c = r.querystring['crop'], s = r.querystring['size'];
if (!v) return { statusCode: 400, statusDescription: 'Missing Version' };
if (!s) return { statusCode: 400, statusDescription: 'Missing Size' };
r.uri += path(v) + path(s) + crop(c);
r.querystring = {};
return r;
}
function path(p) { return p ? '/' + p.value : ''; }
function crop(p) { return p ? '/crop=' + p.value : ''; }
# ---------------------------------------------------------------------------
# Lambda to generate derived images
# ---------------------------------------------------------------------------
GenerateDerivedImageFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-GenerateDerivedImage
CodeUri: app/src/lambdas/generateDerivedImage/
Handler: generateDerivedImageLambda.handler
Description: Generates derived images
Environment:
Variables:
ORIGINAL_IMAGES_BUCKET: !Ref OriginalImagesBucket
DERIVED_IMAGES_BUCKET: !Ref DerivedImagesBucket
JPEG_QUALITY: 85
Policies:
- S3ReadPolicy:
BucketName: !Ref OriginalImagesBucket
- S3WritePolicy:
BucketName: !Ref DerivedImagesBucket
Layers:
# ARN of the Lambda Layer containing the Sharp image processing library
# I tried setting this as a parameter, but it didn't update when I changed it
- arn:aws:lambda:us-east-1:010410881828:layer:Sharp-0_33_0:1
FunctionUrlConfig:
AuthType: NONE #TODO: authentication
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Sourcemap: true
Target: es2020
EntryPoints:
- generateDerivedImageLambda.ts
External:
- sharp
# ---------------------------------------------------------------------------
# Lambda to shlep DynamoDB data to Redis
# ---------------------------------------------------------------------------
DynamoToRedisFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-DynamoToRedis
CodeUri: app/src/lambdas/dynamoToRedis/
Handler: dynamoToRedisLambda.handler
Description: Shlep DynamoDB data to Redis
Events:
DynamoDBEvent:
Type: DynamoDB
Properties:
Stream: !GetAtt GalleryItemDDBTable.StreamArn
StartingPosition: LATEST
BatchSize: 100
Environment:
Variables:
REDIS_HOST: !Ref RedisHost
REDIS_WRITE_USERNAME: !Ref RedisWriteUsername
REDIS_WRITE_PASSWORD: !If [ "IsProd",
"{{resolve:secretsmanager:Tacocat/Gallery/Authentication:SecretString:RedisWritePasswordProd}}",
"{{resolve:secretsmanager:Tacocat/Gallery/Authentication:SecretString:RedisWritePasswordDev}}" ]
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Sourcemap: true
Target: es2020
EntryPoints:
- dynamoToRedisLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to reset Redis search engine
# ---------------------------------------------------------------------------
ResetRedisFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-ResetRedis
CodeUri: app/src/lambdas/api/
Handler: resetRedisLambda.handler
Description: Reset Redis search engine
Events:
Api:
Type: Api
Properties:
Path: /redis-reset
Method: POST
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
REDIS_HOST: !Ref RedisHost
REDIS_WRITE_USERNAME: !Ref RedisWriteUsername
REDIS_WRITE_PASSWORD: !If [ "IsProd",
"{{resolve:secretsmanager:Tacocat/Gallery/Authentication:SecretString:RedisWritePasswordProd}}",
"{{resolve:secretsmanager:Tacocat/Gallery/Authentication:SecretString:RedisWritePasswordDev}}" ]
Policies:
- DynamoDBReadPolicy:
TableName: !Ref GalleryItemDDBTable
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- resetRedisLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to search for albums and images
# ---------------------------------------------------------------------------
SearchFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-Search
CodeUri: app/src/lambdas/api/
Handler: searchLambda.handler
Description: Search for albums and images in DynamoDB
Events:
Api:
Type: Api
Properties:
Path: /search/{searchTerms+}
Method: GET
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
REDIS_HOST: !Ref RedisHost
REDIS_SEARCH_USERNAME: !Ref RedisSearchUsername
REDIS_SEARCH_PASSWORD: !If [ "IsProd",
"{{resolve:secretsmanager:Tacocat/Gallery/Authentication:SecretString:RedisSearchPasswordProd}}",
"{{resolve:secretsmanager:Tacocat/Gallery/Authentication:SecretString:RedisSearchPasswordDev}}" ]
Policies:
- DynamoDBReadPolicy:
TableName: !Ref GalleryItemDDBTable
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- searchLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to get latest album
# ---------------------------------------------------------------------------
GetLatestAlbumFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-GetLatestAlbum
CodeUri: app/src/lambdas/api/
Handler: getLatestAlbumLambda.handler
Description: Get latest album from DynamoDB
Events:
Api:
Type: Api
Properties:
Path: /latest-album
Method: GET
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
Policies:
- DynamoDBReadPolicy:
TableName: !Ref GalleryItemDDBTable
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- getLatestAlbumLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to determine whether album exists
# ---------------------------------------------------------------------------
AlbumExistsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-AlbumExists
CodeUri: app/src/lambdas/api/
Handler: albumExistsLambda.handler
Description: Determine whether album exists
Events:
Api:
Type: Api
Properties:
Path: /album/{albumPath+}
Method: HEAD
ApiRootEvent:
Type: Api
Properties:
Path: /album # root album
Method: HEAD
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
Policies:
- DynamoDBReadPolicy:
TableName: !Ref GalleryItemDDBTable
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- albumExistsLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to determine whether image exists
# ---------------------------------------------------------------------------
ImageExistsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-ImageExists
CodeUri: app/src/lambdas/api/
Handler: imageExistsLambda.handler
Description: Determine whether image exists
Events:
Api:
Type: Api
Properties:
Path: /image/{imagePath+}
Method: HEAD
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
Policies:
- DynamoDBReadPolicy:
TableName: !Ref GalleryItemDDBTable
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- imageExistsLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to retrieve album
# ---------------------------------------------------------------------------
GetAlbumFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-GetAlbum
CodeUri: app/src/lambdas/api/
Handler: getAlbumLambda.handler
Description: Get album and its children from DynamoDB
Events:
Api:
Type: Api
Properties:
Path: /album/{albumPath+}
Method: GET
ApiRootEvent:
Type: Api
Properties:
Path: /album # root album
Method: GET
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
Policies:
- DynamoDBReadPolicy:
TableName: !Ref GalleryItemDDBTable
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- getAlbumLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to create new album
# ---------------------------------------------------------------------------
CreateAlbumFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-CreateAlbum
CodeUri: app/src/lambdas/api/
Handler: createAlbumLambda.handler
Description: Create new album in DynamoDB
Events:
Api:
Type: Api
Properties:
Path: /album/{albumPath+}
Method: PUT
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
Policies:
- DynamoDBWritePolicy:
TableName: !Ref GalleryItemDDBTable
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- createAlbumLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to update existing album
# ---------------------------------------------------------------------------
UpdateAlbumFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-UpdateAlbum
CodeUri: app/src/lambdas/api/
Handler: updateAlbumLambda.handler
Description: Update existing album in DynamoDB
Events:
Api:
Type: Api
Properties:
Path: /album/{albumPath+}
Method: PATCH
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref GalleryItemDDBTable
- Statement:
- Effect: Allow
Action:
- dynamodb:PartiQLUpdate
Resource: !GetAtt GalleryItemDDBTable.Arn
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- updateAlbumLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to update existing image
# ---------------------------------------------------------------------------
UpdateImageFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-UpdateImage
CodeUri: app/src/lambdas/api/
Handler: updateImageLambda.handler
Description: Update existing image in DynamoDB
Events:
Api:
Type: Api
Properties:
Path: /image/{imagePath+}
Method: PATCH
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
Policies:
- DynamoDBWritePolicy:
TableName: !Ref GalleryItemDDBTable
- Statement:
- Effect: Allow
Action:
- dynamodb:PartiQLUpdate
Resource: !GetAtt GalleryItemDDBTable.Arn
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- updateImageLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to set album's thumbnail
# ---------------------------------------------------------------------------
SetAlbumThumbnailFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-SetAlbumThumbnail
CodeUri: app/src/lambdas/api/
Handler: setAlbumThumbnailLambda.handler
Description: Set album thumbnail in DynamoDB
Events:
Api:
Type: Api
Properties:
Path: /album-thumb/{albumPath+}
Method: PATCH
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref GalleryItemDDBTable
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- setAlbumThumbnailLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to recut a thumbnail
# ---------------------------------------------------------------------------
RecutThumbnailFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-RecutThumbnail
CodeUri: app/src/lambdas/api/
Handler: recutThumbnailLambda.handler
Description: Recut thumbnail in DynamoDB
Events:
Api:
Type: Api
Properties:
Path: /thumb/{imagePath+}
Method: PATCH
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref GalleryItemDDBTable
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- recutThumbnailLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to rename an image
# ---------------------------------------------------------------------------
RenameImageFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-RenameImage
CodeUri: app/src/lambdas/api/
Handler: renameImageLambda.handler
Description: Rename image in DynamoDB and S3
Events:
Api:
Type: Api
Properties:
Path: /image-rename/{imagePath+}
Method: POST
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
ORIGINAL_IMAGES_BUCKET: !Ref OriginalImagesBucket
DERIVED_IMAGES_BUCKET: !Ref DerivedImagesBucket
Policies:
- S3CrudPolicy:
BucketName: !Ref OriginalImagesBucket
- S3CrudPolicy:
BucketName: !Ref DerivedImagesBucket
- DynamoDBCrudPolicy:
TableName: !Ref GalleryItemDDBTable
- Statement:
- Effect: Allow
Action:
- dynamodb:PartiQLUpdate
Resource: !GetAtt GalleryItemDDBTable.Arn
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- renameImageLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to rename an album
# ---------------------------------------------------------------------------
RenameAlbumFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-RenameAlbum
CodeUri: app/src/lambdas/api/
Handler: renameAlbumLambda.handler
Description: Rename album in DynamoDB and S3
Events:
Api:
Type: Api
Properties:
Path: /album-rename/{albumPath+}
Method: POST
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
ORIGINAL_IMAGES_BUCKET: !Ref OriginalImagesBucket
DERIVED_IMAGES_BUCKET: !Ref DerivedImagesBucket
Policies:
- S3CrudPolicy:
BucketName: !Ref OriginalImagesBucket
- S3CrudPolicy:
BucketName: !Ref DerivedImagesBucket
- DynamoDBCrudPolicy:
TableName: !Ref GalleryItemDDBTable
- Statement:
- Effect: Allow
Action:
- dynamodb:PartiQLUpdate
Resource: !GetAtt GalleryItemDDBTable.Arn
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- renameAlbumLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to delete album
# ---------------------------------------------------------------------------
DeleteAlbumFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-DeleteAlbum
CodeUri: app/src/lambdas/api/
Handler: deleteAlbumLambda.handler
Description: Delete album from DynamoDB
Events:
Api:
Type: Api
Properties:
Path: /album/{albumPath+}
Method: DELETE
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref GalleryItemDDBTable
- Statement:
- Effect: Allow
Action:
- dynamodb:PartiQLSelect
Resource: !GetAtt GalleryItemDDBTable.Arn
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- deleteAlbumLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to generate presigned URLs to upload images
# ---------------------------------------------------------------------------
GenerateUploadImageUrlsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-GenerateUploadUrls
CodeUri: app/src/lambdas/api/
Handler: generateUploadUrlsLambda.handler
Description: Generate presigned URLs to upload images
Events:
Api:
Type: Api
Properties:
Path: /presigned/{albumPath+}
Method: POST
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
ORIGINAL_IMAGES_BUCKET: !Ref OriginalImagesBucket
Policies:
- S3WritePolicy: # needed to generate presigned URLs with the authority to upload images
BucketName: !Ref OriginalImagesBucket
- DynamoDBReadPolicy:
TableName: !Ref GalleryItemDDBTable
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- generateUploadUrlsLambda.ts
# ---------------------------------------------------------------------------
# API Gateway lambda to delete image
# ---------------------------------------------------------------------------
DeleteImageFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-DeleteImage
CodeUri: app/src/lambdas/api/
Handler: deleteImageLambda.handler
Description: Delete image from DynamoDB and S3
Events:
Api:
Type: Api
Properties:
Path: /image/{imagePath+}
Method: DELETE
Environment:
Variables:
GALLERY_ITEM_DDB_TABLE: !Ref GalleryItemDDBTable
ORIGINAL_IMAGES_BUCKET: !Ref OriginalImagesBucket
DERIVED_IMAGES_BUCKET: !Ref DerivedImagesBucket
Policies:
- S3CrudPolicy:
BucketName: !Ref OriginalImagesBucket
- S3CrudPolicy:
BucketName: !Ref DerivedImagesBucket
- DynamoDBCrudPolicy:
TableName: !Ref GalleryItemDDBTable
- Statement:
- Effect: Allow
Action:
- dynamodb:PartiQLUpdate
Resource: !GetAtt GalleryItemDDBTable.Arn
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2020
Sourcemap: true
EntryPoints:
- deleteImageLambda.ts
# ---------------------------------------------------------------------------
# Lambda that processes uploaded image, extracts metadata and saves to DyanmoDB
# ---------------------------------------------------------------------------
ProcessImageUploadFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-ProcessImageUpload
CodeUri: app/src/lambdas/processImageUpload/
Handler: processImageUploadLambda.handler
Description: Lambda that processes images uploaded to S3, extracts metadata and saves to DyanmoDB
Policies:
- S3ReadPolicy:
BucketName: !Sub "${AWS::StackName}-original-images"
- DynamoDBCrudPolicy:
TableName: !Ref GalleryItemDDBTable
Events:
FileUploadEvent: