-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.yaml
349 lines (344 loc) · 11 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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Image hosting site stack
Globals:
Function:
Timeout: 90
Parameters:
UploadBucketName:
Description: Name of the upload S3 bucket to create
Type: String
EmailNotificationEnabled:
Description: Send notification email on image processing completion
Type: String
Default: "true"
AllowedValues:
- "true"
- "false"
NotificationEmail:
Description: Email address to notify on processing completion (not required if EmailNotificationEnabled is false)
Type: String
SiteName:
Description: Name for site - used in HTML template for title
Type: String
NoDomain:
Description: If `true`, the CloudFront distribution is deployed without a custom domain
Type: String
Default: "false"
AllowedValues:
- "true"
- "false"
Domain:
Description: Domain name that the image site will be hosted on (not required if NoDomain is true) - eg img.example.com
Type: String
CertArn:
Description: ARN of the ACM certificate to use with the CloudFront distribution (not required if NoDomain is true)
Type: String
CreateUploadIamUser:
Description: Whether to create an IAM user for upload to the ingest bucket - user created if `true`
Type: String
Default: "false"
AllowedValues:
- "true"
- "false"
CloudFrontRedirectFunctionName:
Description: Name for the / -> /index.html CloudFront Function. Probably leave as default if not deploying more than one stack.
Type: String
Default: img_site_folder_index_redirect
AllowedPattern: ^[a-zA-Z0-9-_]{1,64}$
CloudFrontCachePolicyName:
Description: Name for the CloudFront cache policy. Probably leave as default if not deploying more than one stack.
Type: String
Default: img-site-cache-policy
Conditions:
CreateUser: !Equals
- !Ref CreateUploadIamUser
- true
CloudFrontNoDomain: !Equals
- !Ref NoDomain
- true
EmailAlertEnabled: !Equals
- !Ref EmailNotificationEnabled
- true
Resources:
UploadBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
BucketName: !Ref UploadBucketName
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
BucketKeyEnabled: true
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
LifecycleConfiguration:
# Clear input objects after 2d
Rules:
- Id: 2dExpiryRule
Status: Enabled
ExpirationInDays: 2
VersioningConfiguration:
Status: Suspended
UploadUser:
Type: AWS::IAM::User
Condition: CreateUser
Properties:
Policies:
- PolicyName: img-site-upload-bucket-write
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
- s3:ListBucket
Resource:
- !Sub "arn:${AWS::Partition}:s3:::${UploadBucket}"
- !Sub "arn:${AWS::Partition}:s3:::${UploadBucket}/*"
HostingBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
BucketKeyEnabled: true
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
LifecycleConfiguration:
# Immediately move uploaded objects to intelligent storage tier, and delete non-current (eg manually deleted) objects after 14d
Rules:
- Id: 0dIntelligentTierRule
Status: Enabled
Transitions:
- TransitionInDays: 0
StorageClass: INTELLIGENT_TIERING
- Id: 14dNoncurrentExpiryRule
Status: Enabled
NoncurrentVersionExpiration:
NoncurrentDays: 14
VersioningConfiguration:
Status: Enabled
LoggingBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
AccessControl: LogDeliveryWrite
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerPreferred
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
BucketKeyEnabled: true
ObjectLockEnabled: true
ObjectLockConfiguration:
# Keep logs for at least 7 days (in GOVERNANCE mode so overridable)
ObjectLockEnabled: Enabled
Rule:
DefaultRetention:
Days: 7
Mode: GOVERNANCE
LifecycleConfiguration:
# Clear logs after 60d, and clear any 'deleted' or overridden ones after 3d
Rules:
- Id: 60dLogRetention
Status: Enabled
ExpirationInDays: 60
- Id: 3dNoncurrentExpiryRule
Status: Enabled
NoncurrentVersionExpiration:
NoncurrentDays: 3
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
VersioningConfiguration:
Status: Enabled
CloudFrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: Image hosting site access identity
S3CloudFrontReadPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref HostingBucket
PolicyDocument:
Statement:
- Action: "s3:GetObject"
Effect: Allow
Resource: !Sub "arn:${AWS::Partition}:s3:::${HostingBucket}/*"
Principal:
CanonicalUser: !GetAtt CloudFrontOriginAccessIdentity.S3CanonicalUserId
CloudFrontCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: !Ref CloudFrontCachePolicyName
MaxTTL: 31536000 # 1 year
DefaultTTL: 604800 # 7 days
MinTTL: 86400 # 1 day
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
QueryStringsConfig:
QueryStringBehavior: none
EnableAcceptEncodingGzip: true
HeadersConfig:
HeaderBehavior: none
CloudFrontRedirectFunction:
# Function to redirect example.com/directory/ to example.com/directory/index.html
Type: AWS::CloudFront::Function
Properties:
AutoPublish: true
FunctionCode: |
function handler(event) {
var request = event.request;
request.uri = request.uri.replace(/\/$/, '\/index.html');
return request;
}
FunctionConfig:
Comment: Redirect directory level requests to index.html
Runtime: cloudfront-js-1.0
Name: !Ref CloudFrontRedirectFunctionName
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases:
- !If [CloudFrontNoDomain, !Ref AWS::NoValue, !Ref Domain]
Comment: !Ref HostingBucket
CustomErrorResponses:
- ErrorCachingMinTTL: 300
ErrorCode: 403
ResponseCode: 403
ResponsePagePath: "/error.html"
- ErrorCachingMinTTL: 300
ErrorCode: 404
ResponseCode: 403
ResponsePagePath: "/error.html"
DefaultCacheBehavior:
CachePolicyId: !Ref CloudFrontCachePolicy
AllowedMethods:
- GET
- HEAD
- OPTIONS
CachedMethods:
- GET
- HEAD
- OPTIONS
Compress: true
ViewerProtocolPolicy: "redirect-to-https"
TargetOriginId: img-site-s3origin
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt CloudFrontRedirectFunction.FunctionARN
Enabled: true
IPV6Enabled: true
HttpVersion: http2
Logging:
Bucket: !GetAtt LoggingBucket.DomainName
Prefix: "cloudfront/"
Origins:
- DomainName: !GetAtt "HostingBucket.DomainName"
Id: img-site-s3origin
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}'
PriceClass: "PriceClass_All"
ViewerCertificate:
!If
- CloudFrontNoDomain
- !Ref AWS::NoValue
- AcmCertificateArn: !Ref CertArn
MinimumProtocolVersion: TLSv1
SslSupportMethod: sni-only
NotificationTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: ImageIngest
FifoTopic: false
Subscription:
- !If
- EmailAlertEnabled
- Protocol: email
Endpoint: !Ref NotificationEmail
- !Ref AWS::NoValue
IngestFunction:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: makefile
Properties:
CodeUri: ingest-fn/
Handler: bootstrap
Runtime: provided.al2023
Architectures:
- arm64
MemorySize: 128
Tracing: Active
Events:
S3Event:
Type: S3
Properties:
Bucket: !Ref UploadBucket
Events: s3:ObjectCreated:*
Policies:
- S3ReadPolicy:
BucketName: !Ref UploadBucketName
- S3WritePolicy:
BucketName: !Ref HostingBucket
- SNSPublishMessagePolicy:
TopicName: !GetAtt NotificationTopic.TopicName
Environment:
Variables:
HOSTING_BUCKET: !Ref HostingBucket
SITE_NAME: !Ref SiteName
DOMAIN:
!If
- CloudFrontNoDomain
- !GetAtt CloudFrontDistribution.DomainName
- !Ref Domain
SNS_TOPIC_ARN: !Ref NotificationTopic
Outputs:
CloudFrontDistributionDomainName:
Description: "Domain for CloudFront distribution"
Value: !GetAtt CloudFrontDistribution.DomainName
Export:
Name: !Sub "${AWS::StackName}-CF-domain"
CloudFrontDistributionId:
Description: "ID of the CloudFront distribution"
Value: !Ref CloudFrontDistribution
Export:
Name: !Sub "${AWS::StackName}-CF-id"
HostingBucketName:
Description: "Name of the hosting bucket"
Value: !Ref HostingBucket
Export:
Name: !Sub "${AWS::StackName}-hosting-bucket"
UploadBucketName:
Description: "Name of the upload bucket"
Value: !Ref UploadBucket
Export:
Name: !Sub "${AWS::StackName}-upload-bucket"
UploadUser:
Condition: CreateUser
Description: "Name of created IAM upload user"
Value: !Ref UploadUser
Export:
Name: !Sub "${AWS::StackName}-upload-iam-user"