-
Notifications
You must be signed in to change notification settings - Fork 1
/
serverless.yml
563 lines (530 loc) · 19.2 KB
/
serverless.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
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
org: animl
app: animl
service: animl-ingest
provider:
name: aws
stage: ${opt:stage, 'dev'}
region: us-west-2
profile: animl
ecr:
images:
zip:
path: ./ingest-zip/
platform: linux/amd64
image:
path: ./ingest-image/
platform: linux/amd64
delete:
path: ./ingest-delete/
platform: linux/amd64
iam:
role:
managedPolicies:
- 'arn:aws:iam::aws:policy/AWSLambdaExecute'
statements:
- Effect: 'Allow'
Action:
- 's3:DeleteObject'
Resource:
- Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: S3BucketAnimlimagesingestion
- Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: S3BucketAnimlimagesingestion
- '/*'
- Effect: Allow
Action:
- 'batch:SubmitJob'
- 'batch:Describe*'
- 'batch:ListJobs'
Resource: '*'
- Effect: Allow
Action:
- sqs:DeleteQueue
Resource:
- !Sub 'arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:${AWS::StackName}-*'
- Effect: Allow
Action:
- cloudwatch:DescribeAlarms
Resource:
- !Sub 'arn:aws:cloudwatch:${AWS::Region}:${AWS::AccountId}:alarm:*'
- Effect: Allow
Action:
- cloudwatch:DeleteAlarms
Resource:
- !Sub 'arn:aws:cloudwatch:${AWS::Region}:${AWS::AccountId}:alarm:${AWS::StackName}-*'
- Effect: Allow
Action:
- 'lambda:GetFunction'
- 'lambda:InvokeFunction'
Resource:
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:exif-api
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:exif-api-*
- Effect: Allow
Action:
- lambda:GetEventSourceMapping
- lambda:DeleteEventSourceMapping
Resource: '*'
- Effect: Allow
Action:
- cloudformation:ListStacks
Resource:
- !Sub 'arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/*'
- Effect: Allow
Action:
- cloudformation:DeleteStack
- cloudformation:ListStacks
- cloudformation:DescribeStacks
- cloudformation:DescribeStackEvents
- cloudformation:DescribeStackResources
Resource:
- !Sub 'arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AWS::StackName}-*'
- Effect: Allow
Action:
- ssm:GetParameter
- ssm:GetParameters
Resource:
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*
environment:
STAGE: ${opt:stage, self:provider.stage, 'dev'}
API_KEY: '{{resolve:secretsmanager:api-key-${self:provider.stage}:SecretString:apikey}}'
functions:
IngestZip:
image:
name: zip
name: IngestZip-${opt:stage, self:provider.stage, 'dev'}
IngestDelete:
image:
name: delete
name: IngestDelete-${opt:stage, self:provider.stage, 'dev'}
events:
- schedule: rate(1 hour)
IngestImage:
image:
name: image
name: IngestImage-${opt:stage, self:provider.stage, 'dev'}
reservedConcurrency: 100
maximumRetryAttempts: 0
timeout: 20
events:
- s3:
bucket: animl-images-ingestion-${opt:stage, self:provider.stage, 'dev'}
event: s3:ObjectCreated:*
existing: true
resources:
Resources:
# TODO: break up resources into separate files:
# https://medium.com/swlh/create-deploy-a-serverless-react-app-to-s3-cloudfront-on-aws-4f83fa605ff0
# AWS Resources naming conventions in Serverless (for reference):
# https://www.serverless.com/framework/docs/providers/aws/guide/resources/
BatchZipNotifyDeleteSNS:
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub ${AWS::StackName}-delete
Subscription:
- Protocol: lambda
Endpoint: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:IngestDelete-${opt:stage, self:provider.stage, 'dev'}
BatchZipNotifyDeleteSNSInvokePermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Ref BatchZipNotifyDeleteSNS
FunctionName: !Sub IngestDelete-${opt:stage, self:provider.stage, 'dev'}
BatchZipIngestQueue:
Type: AWS::Batch::JobQueue
Properties:
ComputeEnvironmentOrder:
- Order: 1
ComputeEnvironment:
Ref: BatchComputeEnvironment
State: ENABLED
Priority: 1
JobQueueName: animl-batch-ingestion-${opt:stage, self:provider.stage, 'dev'}
BatchServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: batch.amazonaws.com
Action: sts:AssumeRole
Path: '/service-role/'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole
BatchJobRole:
Type: AWS::IAM::Role
Properties:
Path: '/'
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: ecs-tasks.amazonaws.com
Policies:
- PolicyName:
Fn::Join:
- '-'
- - !Ref 'AWS::StackName'
- 'etl-policy'
PolicyDocument:
Statement:
- Effect: Allow
Action:
- batch:DescribeJobs
Resource: '*'
- Effect: Allow
Action:
- sqs:*
Resource:
- !Sub 'arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:${AWS::StackName}-*'
- Effect: Allow
Action:
- cloudformation:CreateStack
Resource:
- !Sub 'arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/*'
- Effect: Allow
Action:
- cloudwatch:DescribeAlarms
Resource:
- !Sub 'arn:aws:cloudwatch:${AWS::Region}:${AWS::AccountId}:alarm:*'
- Effect: Allow
Action:
- lambda:GetEventSourceMapping
- lambda:CreateEventSourceMapping
Resource: '*'
- Effect: Allow
Action:
- cloudwatch:DeleteAlarms
- cloudwatch:PutMetricAlarm
- cloudwatch:SetAlarmState
Resource:
- !Sub 'arn:aws:cloudwatch:${AWS::Region}:${AWS::AccountId}:alarm:${AWS::StackName}-*'
- Effect: Allow
Action:
- cloudformation:DeleteStack
- cloudformation:ListStacks
- cloudformation:DescribeStacks
- cloudformation:DescribeStackEvents
- cloudformation:DescribeStackResources
Resource:
- !Sub 'arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AWS::StackName}-*'
- Effect: Allow
Action: '*'
Resource:
- "arn:aws:s3:::animl-images-ingestion-${opt:stage, self:provider.stage, 'dev'}/*"
- "arn:aws:s3:::animl-images-ingestion-${opt:stage, self:provider.stage, 'dev'}"
- Effect: Allow
Action:
- ecs:DescribeContainerInstances
Resource: '*'
- Effect: Allow
Action:
- ssm:GetParameter
- ssm:GetParameters
Resource:
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*
BatchComputeEnvironment:
Type: AWS::Batch::ComputeEnvironment
Properties:
Type: MANAGED
ServiceRole:
Fn::GetAtt:
- BatchServiceRole
- Arn
ComputeEnvironmentName:
Fn::Join:
- '-'
- - !Ref 'AWS::StackName'
- 'batch-ingest'
State: ENABLED
ComputeResources:
Type: FARGATE
MaxvCpus: 128
Subnets:
- 'subnet-c958fd83'
SecurityGroupIds:
- !Ref BatchSecurityGroup
BatchJobDefinition:
Type: AWS::Batch::JobDefinition
Properties:
Type: container
JobDefinitionName:
Fn::Join:
- '-'
- - !Ref 'AWS::StackName'
- 'batch-job'
PlatformCapabilities:
- FARGATE
RetryStrategy:
Attempts: 1
ContainerProperties:
FargatePlatformConfiguration:
PlatformVersion: 'LATEST'
EphemeralStorage:
SizeInGiB: 75
NetworkConfiguration:
AssignPublicIp: 'ENABLED'
Environment:
- Name: 'StackName'
Value: !Ref 'AWS::StackName'
- Name: 'STAGE'
Value: ${opt:stage, self:provider.stage, 'dev'}
- Name: 'API_KEY'
Value: '{{resolve:secretsmanager:api-key-${self:provider.stage}:SecretString:apikey}}'
ResourceRequirements:
- Type: 'VCPU'
Value: 1
- Type: 'MEMORY'
Value: 5120
ExecutionRoleArn:
Fn::GetAtt:
- BatchExecRole
- Arn
JobRoleArn:
Fn::GetAtt:
- BatchJobRole
- Arn
ReadonlyRootFilesystem: false
Image: !Sub '${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/serverless-animl-ingest-${self:provider.stage}:zip'
BatchExecRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: ecs-tasks.amazonaws.com
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
Path: '/'
BatchSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: 'vpc-ad3816d5'
GroupDescription:
Fn::Join:
- ' '
- - !Ref 'AWS::StackName'
- ' Batch Security Group'
SecurityGroupIngress: []
# Ingestion bucket
S3BucketAnimlimagesingestion:
Type: AWS::S3::Bucket
Properties:
BucketName: animl-images-ingestion-${opt:stage, self:provider.stage, 'dev'}
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
CorsConfiguration:
CorsRules:
- AllowedHeaders: ['*']
AllowedMethods: ['PUT']
AllowedOrigins: ['*']
ExposedHeaders: ['ETag']
# Bucket policy for animl-images-ingestion
S3BucketPolicyAnimlImagesIngestionBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: S3BucketAnimlimagesingestion
PolicyDocument:
Statement:
- Action:
- 's3:*'
Effect: 'Allow'
Resource:
- "arn:aws:s3:::animl-images-ingestion-${opt:stage, self:provider.stage, 'dev'}/*"
- "arn:aws:s3:::animl-images-ingestion-${opt:stage, self:provider.stage, 'dev'}"
Principal:
AWS: 'arn:aws:iam::830244800171:user/animl-base'
# Serving bucket
S3BucketAnimlimagesserving:
Type: AWS::S3::Bucket
Properties:
BucketName: animl-images-serving-${opt:stage, self:provider.stage, 'dev'}
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
# Parking lot bucket
S3BucketAnimlimagesparkinglot:
Type: AWS::S3::Bucket
Properties:
BucketName: animl-images-parkinglot-${opt:stage, self:provider.stage, 'dev'}
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
# Dead letter bucket
S3BucketAnimlimagesdeadletter:
Type: AWS::S3::Bucket
Properties:
BucketName: animl-images-dead-letter-${opt:stage, self:provider.stage, 'dev'}
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
# Lambda Permission grants the bucket permission to invoke the function
LambdaPermissionAnimlimagesingestion:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Fn::GetAtt: # grabs function arn
- IngestImageLambdaFunction
- Arn
Principal: s3.amazonaws.com
Action: lambda:InvokeFunction
SourceAccount:
Ref: AWS::AccountId
SourceArn: arn:aws:s3:::animl-images-ingestion-${opt:stage, self:provider.stage, 'dev'}
# TODO: SSL certificate for image serving bucket Cloudfront distrobution?
# only need if we want a custom CNAME
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-viewercertificate.html
# Origin Access Identity for serving bucket Cloudfront distrobution
CloudfrontOriginAccessIdentityAnimlimagesserving:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: "orign access identity for animl images serving ${opt:stage, self:provider.stage, 'dev'} bucket"
# Cloudfront distrobution for serving bucket
# API docs - https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudfront-distribution.html
CloudfrontDistributionAnimlimagesserving:
DependsOn:
- S3BucketAnimlimagesserving
- CloudfrontOriginAccessIdentityAnimlimagesserving
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: animl-images-serving-${opt:stage, self:provider.stage, 'dev'}.s3.amazonaws.com
Id: !Ref S3BucketAnimlimagesserving
S3OriginConfig:
OriginAccessIdentity:
Fn::Join:
- ''
- - 'origin-access-identity/cloudfront/'
- !Ref CloudfrontOriginAccessIdentityAnimlimagesserving
Enabled: 'true'
Comment: "Cloudfront distro for animl images serving ${opt:stage, self:provider.stage, 'dev'} bucket"
Logging:
IncludeCookies: 'false'
Bucket: animllogs.s3.amazonaws.com
Prefix: animl-images-serving-${opt:stage, self:provider.stage, 'dev'}
# Aliases:
# - mysite.example.com
# - yoursite.example.com
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
- OPTIONS
TargetOriginId: !Ref S3BucketAnimlimagesserving
MinTTL: 86400 # 1 day - perhaps adjust for staging?
MaxTTL: 31536000 # 1 year
ForwardedValues:
QueryString: 'false'
Cookies:
Forward: none
# TrustedSigners:
# - 1234567890EX
# - 1234567891EX
ViewerProtocolPolicy: 'redirect-to-https'
# PriceClass: PriceClass_200
ViewerCertificate:
CloudFrontDefaultCertificate: 'true'
# Bucket policy for serving bucket to allow cloudfront to access it
S3BucketPolicyAnimlImagesServingBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: S3BucketAnimlimagesserving
PolicyDocument:
Statement:
- Action:
- 's3:GetObject'
Effect: 'Allow'
Resource:
- "arn:aws:s3:::animl-images-serving-${opt:stage, self:provider.stage, 'dev'}/*"
- "arn:aws:s3:::animl-images-serving-${opt:stage, self:provider.stage, 'dev'}"
Principal:
AWS:
Fn::Join:
- ''
- - 'arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity '
- !Ref CloudfrontOriginAccessIdentityAnimlimagesserving
# SSM Param for ingestion bucket
SSMParameterAnimlimagesingestionbucket:
Type: AWS::SSM::Parameter
Properties:
Description: Animl images ingestion bucket name
Name: /images/ingestion-bucket-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value: animl-images-ingestion-${opt:stage, self:provider.stage, 'dev'}
# SSM Param for batch queue
SSMParameterAnimlBatchQueue:
Type: AWS::SSM::Parameter
Properties:
Description: Animl images batch queue
Name: /images/batch-queue-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value:
Ref: BatchZipIngestQueue
# SSM Param for batch Job Def
SSMParameterAnimlBatchJob:
Type: AWS::SSM::Parameter
Properties:
Description: Animl images batch job
Name: /images/batch-job-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value:
Ref: BatchJobDefinition
# SSM Param - serving bucket
SSMParameterAnimlimagesservingbucket:
Type: AWS::SSM::Parameter
Properties:
Description: Animl images serving bucket name
Name: /images/serving-bucket-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value: animl-images-serving-${opt:stage, self:provider.stage, 'dev'}
# SSM Param - parkinglot bucket
SSMParameterAnimlimagesparkinglotbucket:
Type: AWS::SSM::Parameter
Properties:
Description: Animl images parking lot bucket name
Name: /images/parkinglot-bucket-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value: animl-images-parkinglot-${opt:stage, self:provider.stage, 'dev'}
# SSM Param - dead-letter bucket
SSMParameterAnimlimagesdeadletterbucket:
Type: AWS::SSM::Parameter
Properties:
Description: Animl images dead-letter bucket name
Name: /images/dead-letter-bucket-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value: animl-images-dead-letter-${opt:stage, self:provider.stage, 'dev'}
# SSM Param - serving bucket cloudfront url
SSMParameterAnimlimagesservingdistrobution:
Type: AWS::SSM::Parameter
Properties:
Description: Animl images serving cloudfront url
Name: /images/url-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value:
Fn::GetAtt: # grabs cloudfront url
- CloudfrontDistributionAnimlimagesserving
- DomainName