-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.yaml
194 lines (182 loc) · 5.61 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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
thumbnailer
SAM Template for thumbnailer of images stored on S3 bucket stack,
including CloudFront distribution and Lambda function resizing images.
Parameters:
CloudFrontPriceClass:
Type: String
Description: The CloudFront distribution price class.
Default: 'PriceClass_200'
AllowedValues:
- 'PriceClass_100'
- 'PriceClass_200'
- 'PriceClass_All'
ContentAge:
Type: Number
Description: The value (in seconds) of Cache-Control header used to control how long objects stay in the cache.
Default: 604800
MinValue: 60
LongEdgeMin:
Type: Number
Description: The smallest allowed value of image width or height.
Default: 40
MinValue: 40
MaxValue: 400
LongEdgeMax:
Type: Number
Description: The biggest allowed value of image width or height.
Default: 2000
MinValue: 400
MaxValue: 4000
Environment:
Type: String
Default: DEV
SentryDsn:
Type: String
Description: Sentry DSN
Globals:
Api:
BinaryMediaTypes:
- image~1jpg
- image~1png
Auth:
ApiKeyRequired: true
Function:
Runtime: python3.7
MemorySize: 512
Timeout: 10
Tracing: Active
Environment:
Variables:
ENV: !Ref Environment
LOG_LEVEL: INFO
SENTRY_DSN: !Ref SentryDsn
Resources:
ThumbnailerFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: thumbnailer/
Handler: app.lambda_handler
Environment:
Variables:
CONTENT_AGE_IN_SECONDS: !Ref ContentAge
LONG_EDGE_MIN: !Ref LongEdgeMin
LONG_EDGE_MAX: !Ref LongEdgeMax
Policies:
- AWSXrayWriteOnlyAccess
- Version: '2012-10-17' # Policy Document
Statement:
- Effect: Allow
Action:
- s3:Get*
Resource:
- '*'
Events:
InfoAPI:
Type: Api
Properties:
Path: /thumbnailer/info/{uri}
Method: get
ThumbmnailAPI:
Type: Api
Properties:
Path: /thumbnailer/thumbnail/{uri}/long-edge/{long_edge_pixels}
Method: get
FitAPI:
Type: Api
Properties:
Path: /thumbnailer/fit/{uri}/width/{width_pixels}/height/{height_pixels}
Method: get
ThumbnailerApiKey:
Type: AWS::ApiGateway::ApiKey
Properties:
Enabled: true
ThumbnailerApiUsagePlan:
Type: AWS::ApiGateway::UsagePlan
DependsOn:
- ServerlessRestApi
- ServerlessRestApiProdStage
Properties:
ApiStages:
- ApiId: !Ref ServerlessRestApi
Stage: !Ref ServerlessRestApi.Stage
ThumbnailerApiUsagePlanKey:
Type: AWS::ApiGateway::UsagePlanKey
DependsOn:
- ThumbnailerApiKey
- ThumbnailerApiUsagePlan
Properties:
KeyId: !Ref ThumbnailerApiKey
KeyType: API_KEY
UsagePlanId: !Ref ThumbnailerApiUsagePlan
CloudFrontOriginBucket:
Type: AWS::S3::Bucket
CloudFrontOriginBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref CloudFrontOriginBucket
PolicyDocument:
Statement:
-
Action:
- s3:GetObject
Effect: Allow
Resource: !Join [ "", [ "arn:aws:s3:::", !Ref CloudFrontOriginBucket, "/*" ] ]
Principal:
CanonicalUser: !GetAtt CloudFrontOriginAccessIdentity.S3CanonicalUserId
CloudFrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: 'CloudFront OAI for Thumbnailer'
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
DependsOn:
- ThumbnailerFunction
Properties:
DistributionConfig:
DefaultCacheBehavior:
Compress: true
ForwardedValues:
QueryString: false
TargetOriginId: !Ref CloudFrontOriginBucket
ViewerProtocolPolicy: redirect-to-https
CacheBehaviors:
- PathPattern: "thumbnailer/*"
Compress: false
ForwardedValues:
Headers:
- "Uri-Prefix"
QueryString: false
TargetOriginId: !Ref ThumbnailerFunction
ViewerProtocolPolicy: redirect-to-https
Enabled: true
HttpVersion: http2
Origins:
- DomainName:
!Join [ "", [ !Ref CloudFrontOriginBucket, ".s3.amazonaws.com" ] ]
Id: !Ref CloudFrontOriginBucket
S3OriginConfig:
OriginAccessIdentity:
!Join [ "", [ "origin-access-identity/cloudfront/", !Ref CloudFrontOriginAccessIdentity ] ]
- DomainName: !Sub "${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com"
Id: !Ref ThumbnailerFunction
OriginPath: "/Prod"
OriginCustomHeaders:
- HeaderName: Accept
HeaderValue: "image/jpg"
CustomOriginConfig:
OriginProtocolPolicy: https-only
PriceClass: !Ref CloudFrontPriceClass
Outputs:
ThumbnailerApiUrl:
Description: "API Gateway endpoint URL for Prod stage for Thumbnailer function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/thumbnailer"
CdnApiUrl:
Description: "Thumbnailer API URL"
Value: !Join [ "", [ "https://", !GetAtt CloudFrontDistribution.DomainName, "/thumbnailer" ] ]
ThumbnailerApiKey:
Description: "API Key Id, check the value and set in X-Api-Key header"
Value: !Ref ThumbnailerApiKey