-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudformation.yaml
330 lines (312 loc) · 9.56 KB
/
cloudformation.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
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: Serverless image processor
Parameters:
CDNCertificateArn:
Type: String
Description: The ARN for your custom certificate from AWS Certificate Manager
Default: ""
CDNAliases:
Type: CommaDelimitedList
Description: Domains aliases associated with CloudFront distribution
Default: ""
S3FunctionBucket:
Type: String
ConstraintDescription: Must be a valid S3 Bucket.
MinLength: "1"
MaxLength: "64"
AllowedPattern: "[a-zA-Z][a-zA-Z0-9-.]*"
S3ImageBucket:
Type: String
ConstraintDescription: Must be a valid S3 Bucket.
MinLength: "1"
MaxLength: "64"
AllowedPattern: "[a-zA-Z][a-zA-Z0-9-.]*"
S3ImageKeyPattern:
Type: String
Default: ":filename"
SERVICENAME:
Type: String
MinLength: "1"
Default: imaginarium
Conditions:
UseCustomCDNCertificate: !Not [!Equals ["", !Ref CDNCertificateArn]]
Resources:
ImaginariumApiGWRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: "apigateway.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: !Sub "${AWS::StackName}-agw-cloud-watch-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:DescribeLogGroups"
- "logs:DescribeLogStreams"
- "logs:PutLogEvents"
- "logs:GetLogEvents"
- "logs:FilterLogEvents"
Resource: "*"
- PolicyName: !Sub "${AWS::StackName}-agw-lambda-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "lambda:InvokeFunction"
Resource:
- "*"
ImaginariumApiGWAccount:
Type: "AWS::ApiGateway::Account"
Properties:
CloudWatchRoleArn: !GetAtt ImaginariumApiGWRole.Arn
ImaginariumApiStage:
DependsOn: [ImaginariumApiGWAccount]
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId: !Ref ImaginariumApiDeployment
MethodSettings:
- DataTraceEnabled: False
HttpMethod: "*"
LoggingLevel: ERROR
ResourcePath: "/*"
RestApiId: !Ref ImaginariumApi
StageName: LATEST
ImaginariumApiDeployment:
Type: "AWS::ApiGateway::Deployment"
Properties:
RestApiId: !Ref ImaginariumApi
StageName: production
ImaginariumApi:
DependsOn: [ImageHandlerFunction, ImaginariumApiGWRole]
Type: "AWS::ApiGateway::RestApi"
Properties:
Name: !Sub ${SERVICENAME}
EndpointConfiguration:
Types:
- REGIONAL
Body:
swagger: 2.0
info:
title: !Sub ${SERVICENAME} API
basePath: /production
schemes:
- https
x-amazon-apigateway-binary-media-types:
- "image/webp"
- "image/png"
- "image/jpeg"
- "text/html"
# this is here because apigateway takes it from text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 :()
paths:
"/{proxy+}":
get:
parameters:
- name: proxy
in: path
required: true
type: string
produces:
# by default it products html because errors
# otherwise it will treat everything else as binary
- application/json
responses:
"200":
description: Success
headers:
Content-Type:
type: string
x-amazon-apigateway-integration:
credentials: !GetAtt ImaginariumApiGWRole.Arn
responses:
"200":
statusCode: 200
uri: !Join
- ""
- - "arn:aws:apigateway:"
- !Ref "AWS::Region"
- ":"
- "lambda:path/2015-03-31/functions/"
- !GetAtt
- ImageHandlerFunction
- Arn
- /invocations
httpMethod: POST
type: aws_proxy
ImageHandlerLogGroup:
Type: "AWS::Logs::LogGroup"
Properties:
RetentionInDays: 1
LogGroupName: !Join
- ""
- - /aws/lambda/
- !Ref ImageHandlerFunction
ImageHandlerRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- "sts:AssumeRole"
Path: /
RoleName: !Join
- ""
- - !Ref "AWS::StackName"
- Role
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
ImageHandlerS3CodePolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: !Join
- ""
- - !Ref "AWS::StackName"
- S3CodeReadPolicy
Roles:
- !Ref ImageHandlerRole
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "s3:GetObject"
Resource:
- !Sub arn:aws:s3:::${S3FunctionBucket}/*
ImageHandlerS3ImagesReadPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: !Join
- ""
- - !Ref "AWS::StackName"
- S3ImagesReadPolicy
Roles:
- !Ref ImageHandlerRole
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "s3:GetObject"
Resource:
- !Sub arn:aws:s3:::${S3ImageBucket}/*
ImageHandlerLogPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: !Join
- ""
- - !Ref "AWS::StackName"
- LogPolicy
Roles:
- !Ref ImageHandlerRole
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*"
ImageHandlerPermission:
DependsOn: [ImageHandlerFunction]
Type: "AWS::Lambda::Permission"
Properties:
Action: "lambda:invokeFunction"
FunctionName: !GetAtt ImageHandlerFunction.Arn
Principal: apigateway.amazonaws.com
SourceAccount: !Ref AWS::AccountId
SourceArn: !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:*/*/*/*"
ImageHandlerFunction:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: ./dist
Description: Imaginarium image handler
FunctionName: !Sub ${SERVICENAME}
Handler: index.handler
MemorySize: 1536 # more computing power
Runtime: nodejs10.x
Timeout: 10 # 10 seconds timeout
Tags:
Service: !Ref SERVICENAME
Environment:
Variables:
S3_IMAGE_BUCKET: !Ref S3ImageBucket
S3_IMAGE_KEY_PATTERN: !Ref S3ImageKeyPattern
Role: !GetAtt ImageHandlerRole.Arn
CDN:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases: !Ref CDNAliases
Enabled: True
HttpVersion: http2
Comment: !Sub ${AWS::StackName} CDN distribution
PriceClass: "PriceClass_100" # eu, usa, ca
CustomErrorResponses:
- ErrorCode: 405
ErrorCachingMinTTL: 10
- ErrorCode: 500
ErrorCachingMinTTL: 10
- ErrorCode: 501
ErrorCachingMinTTL: 10
- ErrorCode: 502
ErrorCachingMinTTL: 10
- ErrorCode: 503
ErrorCachingMinTTL: 10
- ErrorCode: 504
ErrorCachingMinTTL: 10
Origins:
- DomainName: !Sub "${ImaginariumApi}.execute-api.${AWS::Region}.amazonaws.com"
OriginPath: /production
Id: !Ref ImaginariumApi
CustomOriginConfig:
HTTPSPort: 443
OriginProtocolPolicy: https-only
OriginSSLProtocols:
- TLSv1
- TLSv1.1
- TLSv1.2
DefaultCacheBehavior:
AllowedMethods: ["HEAD", "GET"]
DefaultTTL: 0 # do not cache if Cache-Control headers are not present
ForwardedValues:
Cookies:
Forward: none
QueryString: True
Headers:
- Accept # cache based on Accept header so we can have one URL for multiple accepted types
ViewerProtocolPolicy: redirect-to-https
TargetOriginId: !Ref ImaginariumApi
ViewerCertificate: !If
- UseCustomCDNCertificate
- AcmCertificateArn: !Ref CDNCertificateArn
SslSupportMethod: sni-only
- CloudFrontDefaultCertificate: True
Tags:
- Key: Service
Value: !Ref SERVICENAME
Outputs:
ApiUrl:
Description: URL
Value: !Sub "https://${ImaginariumApi}.execute-api.${AWS::Region}.amazonaws.com/production"
CDNId:
Description: CDN ID
Value: !Ref CDN
CDNUrl:
Description: CDN URL
Value: !GetAtt CDN.DomainName