-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.yml
418 lines (408 loc) · 12.4 KB
/
template.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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Imgproxy running on AWS Lambda
Parameters:
FunctionName:
Type: String
Description: A name for the function. If you don't specify a name, stack name is used.
ImageUri:
Type: String
Description: >-
The URI of the Docker image to use for the function.
The image must be in an Amazon Elastic Container Registry (Amazon ECR) repository.
MemorySize:
Type: Number
Description: >-
Amount of memory in megabytes to give to the function.
AWS Lambda will allocate CPU power linearly in proportion to the amount of memory configured,
so it's recommended to allocate at least 2048 MB of memory to the function.
Default: 2048
MinValue: 1024
MaxValue: 10240
Timeout:
Type: Number
Description: The amount of time in seconds that Lambda allows a function to run before stopping it.
Default: 60
MinValue: 1
MaxValue: 60
EnvironmentSystemsManagerParametersPath:
Type: String
Description: >-
A path of AWS Systems Manager Parameter Store parameters that should be loaded as environment
variables. The path should start with a slash (/) but should not have a slash (/) at the end.
For example, if you want to load the IMGPROXY_KEY variable from the /imgproxy/prod/IMGPROXY_KEY
parameter, the value should be /imgproxy/prod. If not set, imgproxy will load environment
variables from the /${StackName} path.
Default: ''
S3Objects:
Type: CommaDelimitedList
Description: >-
ARNs of S3 objects (comma delimited) that imgproxy should have access to.
You can grant access to multiple objects with a single ARN by using wildcards.
Example: arn:aws:s3:::my-images-bucket/*,arn:aws:s3:::my-assets-bucket/images/*
Default: ''
S3AssumeRoleARN:
Type: String
Description: >-
ARN of IAM Role that S3 client should assume.
This allows you to provide imgproxy access to third-party S3 buckets that the assummed IAM Role has access to
Default: ''
S3MultiRegion:
Type: String
Description: >-
Should imgproxy be able to access S3 buckets in other regions?
By default, imgproxy can access only S3 buckets located in the same region as imgproxy
Default: 'No'
AllowedValues:
- 'Yes'
- 'No'
S3ClientSideDecryption:
Type: String
Description: >-
Should imgproxy use S3 decryption client?
The decription client will be used forall objects in all S3 buckets, so unecrypted objects won't be accessable
Default: 'No'
AllowedValues:
- 'Yes'
- 'No'
PathPrefix:
Type: String
Description: Path prefix, beginning with a slash (/).Do not add a slash (/) at the end of the path
Default: ''
CreateCloudFrontDistribution:
Type: String
Description: >-
Should caching CloudFront distribution be created?
Default: 'Yes'
AllowedValues:
- 'Yes'
- 'No'
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Lambda Function
Parameters:
- FunctionName
- ImageUri
- MemorySize
- Timeout
- Label:
default: imgproxy Configuration
Parameters:
- EnvironmentSystemsManagerParametersPath
- Label:
default: S3 integration
Parameters:
- S3Objects
- S3AssumeRoleARN
- S3MultiRegion
- S3ClientSideDecryption
- Label:
default: Endpoint
Parameters:
- PathPrefix
- CreateCloudFrontDistribution
ParameterLabels:
FunctionName:
default: Function name
ImageUri:
default: Docker image
MemorySize:
default: Memory size
Timeout:
default: Timeout
EnvironmentSystemsManagerParametersPath:
default: Systems Manager Parameter Store parameters path (optional)
S3Objects:
default: S3 objects (optional)
S3AssumeRoleARN:
default: IAM Role ARN to assume (optional)
S3MultiRegion:
default: Enable multi-region mode
S3ClientSideDecryption:
default: Enable client-side decryption
PathPrefix:
default: Path prefix (optional)
CreateCloudFrontDistribution:
default: Create CloudForont distribution?
Conditions:
HaveFunctionName: !Not
- !Equals
- !Ref 'FunctionName'
- ''
HaveEnvironmentSystemsManagerParametersPath: !Not
- !Equals
- !Ref 'EnvironmentSystemsManagerParametersPath'
- ''
HaveS3Objects: !Not
- !Equals
- !Join
- ''
- !Ref 'S3Objects'
- ''
HaveS3AssumeRole: !Not
- !Equals
- !Ref 'S3AssumeRoleARN'
- ''
EnableS3MultiRegion: !Equals
- !Ref 'S3MultiRegion'
- 'Yes'
EnableS3ClientSideDecryption: !Equals
- !Ref 'S3ClientSideDecryption'
- 'Yes'
HavePathPrefix: !Not
- !Equals
- !Ref 'PathPrefix'
- ''
DeployCloudFront: !Equals
- !Ref 'CreateCloudFrontDistribution'
- 'Yes'
Mappings:
OriginShieldRegionMap:
# Regions with origin shield
us-east-2:
Region: us-east-2
us-east-1:
Region: us-east-1
us-west-2:
Region: us-west-2
ap-south-1:
Region: ap-south-1
ap-northeast-2:
Region: ap-northeast-2
ap-southeast-1:
Region: ap-southeast-1
ap-southeast-2:
Region: ap-southeast-2
ap-northeast-1:
Region: ap-northeast-1
eu-central-1:
Region: eu-central-1
eu-west-1:
Region: eu-west-1
eu-west-2:
Region: eu-west-2
sa-east-1:
Region: sa-east-1
# Regions without origin shield
us-west-1:
Region: us-west-2
af-south-1:
Region: eu-west-1
ap-east-1:
Region: ap-southeast-1
ca-central-1:
Region: us-east-1
eu-south-1:
Region: eu-central-1
eu-west-3:
Region: eu-west-2
eu-north-1:
Region: eu-west-2
me-south-1:
Region: ap-south-1
Resources:
ImgproxyFunction:
Type: AWS::Serverless::Function
Properties:
Architectures: [arm64]
FunctionName: !If
- HaveFunctionName
- !Ref 'FunctionName'
- !Ref 'AWS::StackName'
PackageType: Image
ImageUri: !Ref ImageUri
MemorySize: !Ref MemorySize
Timeout: !Ref Timeout
Environment:
Variables:
PORT: '8080'
IMGPROXY_LOG_FORMAT: json
IMGPROXY_ENV_AWS_SSM_PARAMETERS_PATH: !If
- HaveEnvironmentSystemsManagerParametersPath
- !Ref 'EnvironmentSystemsManagerParametersPath'
- !Join
- ''
- - /
- !Ref 'AWS::StackName'
IMGPROXY_USE_S3: '1'
IMGPROXY_S3_ASSUME_ROLE_ARN: !If
- HaveS3AssumeRole
- !Ref 'S3AssumeRoleARN'
- !Ref 'AWS::NoValue'
IMGPROXY_S3_MULTI_REGION: !If
- EnableS3MultiRegion
- '1'
- !Ref 'AWS::NoValue'
IMGPROXY_S3_USE_DECRYPTION_CLIENT: !If
- EnableS3ClientSideDecryption
- '1'
- !Ref 'AWS::NoValue'
IMGPROXY_PATH_PREFIX: !If
- HavePathPrefix
- !Ref 'PathPrefix'
- !Ref 'AWS::NoValue'
IMGPROXY_CLOUD_WATCH_SERVICE_NAME: !If
- HaveFunctionName
- !Ref 'FunctionName'
- !Ref 'AWS::StackName'
IMGPROXY_CLOUD_WATCH_NAMESPACE: imgproxy
IMGPROXY_CLOUD_WATCH_REGION: !Ref 'AWS::Region'
FunctionUrlConfig:
AuthType: NONE
InvokeMode: RESPONSE_STREAM
LoggingConfig:
LogFormat: JSON
Policies:
- Statement:
- Sid: CloudWatch
Effect: Allow
Action:
- cloudwatch:PutMetricData
- cloudwatch:PutMetricStream
Resource:
- '*'
- Sid: SystemsManagerAccess
Effect: Allow
Action:
- ssm:GetParametersByPath
Resource:
- !Join
- ''
- - 'arn:aws:ssm:'
- !Ref 'AWS::Region'
- ':'
- !Ref 'AWS::AccountId'
- :parameter
- !If
- HaveEnvironmentSystemsManagerParametersPath
- !Ref 'EnvironmentSystemsManagerParametersPath'
- !Join
- ''
- - /
- !Ref 'AWS::StackName'
- !If
- HaveS3Objects
- Sid: S3Access
Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectVersion
Resource: !Ref 'S3Objects'
- !Ref 'AWS::NoValue'
- !If
- HaveS3AssumeRole
- Sid: IAMRoleAssume
Effect: Allow
Action:
- sts:AssumeRole
Resource: !Ref 'S3AssumeRoleARN'
- !Ref 'AWS::NoValue'
- !If
- EnableS3ClientSideDecryption
- Sid: KMSDecrypt
Effect: Allow
Action:
- kms:Decrypt
Resource: !Join
- ':'
- - arn:aws:kms:*
- !Ref 'AWS::AccountId'
- key/*
- !Ref 'AWS::NoValue'
Tracing: Active
CloudFrontCachePolicy:
Type: AWS::CloudFront::CachePolicy
Condition: DeployCloudFront
Properties:
CachePolicyConfig:
Name: !Join
- '-'
- - !Ref 'AWS::StackName'
- cache-policy
DefaultTTL: 31536000
MaxTTL: 31536000
MinTTL: 1
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
EnableAcceptEncodingBrotli: false
EnableAcceptEncodingGzip: false
HeadersConfig:
HeaderBehavior: whitelist
Headers:
- Accept
QueryStringsConfig:
QueryStringBehavior: none
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Condition: DeployCloudFront
Properties:
DistributionConfig:
Enabled: true
Origins:
- DomainName: !Select
- 2
- !Split ['/', !GetAtt 'ImgproxyFunctionUrl.FunctionUrl']
Id: !Join
- '-'
- - !Ref 'AWS::StackName'
- origin
CustomOriginConfig:
OriginProtocolPolicy: https-only
OriginReadTimeout: !Ref 'Timeout'
OriginPath: !Ref 'PathPrefix'
OriginShield:
Enabled: true
OriginShieldRegion: !FindInMap
- OriginShieldRegionMap
- !Ref 'AWS::Region'
- Region
DefaultCacheBehavior:
TargetOriginId: !Join
- '-'
- - !Ref 'AWS::StackName'
- origin
CachePolicyId: !Ref 'CloudFrontCachePolicy'
ViewerProtocolPolicy: redirect-to-https
PriceClass: PriceClass_All
ViewerCertificate:
CloudFrontDefaultCertificate: true
Outputs:
FunctionArn:
Description: The ARN of the function
Value: !GetAtt 'ImgproxyFunction.Arn'
DirectURL:
Description: The direct URL endpoint for imgproxy
Value: !GetAtt 'ImgproxyFunctionUrl.FunctionUrl'
CloudFrontURL:
Description: The CloudFront endpoint for imgproxy
Value: !GetAtt 'CloudFrontDistribution.DomainName'
Condition: DeployCloudFront
HowToConfigure:
Description: How to configure imgproxy
Value: !Join
- ''
- - 'imgproxy loads AWS Systems Manager Parameter Store parameters from the path '
- !If
- HaveEnvironmentSystemsManagerParametersPath
- !Ref 'EnvironmentSystemsManagerParametersPath'
- !Join
- ''
- - /
- !Ref 'AWS::StackName'
- ' as environment variables at launch. For example, if you create a parameter named '
- !Join
- /
- - !If
- HaveEnvironmentSystemsManagerParametersPath
- !Ref 'EnvironmentSystemsManagerParametersPath'
- !Join
- ''
- - /
- !Ref 'AWS::StackName'
- IMGPROXY_KEY
- ', it will be loaded as the IMGPROXY_KEY environment variable.'
- ' If you change the parameter value, you need to restart the imgproxy service to pick up'
- ' the new value.'