This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless_template.yml
305 lines (286 loc) · 9.44 KB
/
serverless_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
service: image-service
provider:
name: aws
runtime: nodejs8.10
region: us-east-1
stage: ${opt:stage, self:custom.defaultStage}
profile: ${self:provider.stage, 'default'}
endpointType: REGIONAL
timeout: 30
deploymentBucket:
name: "pinster-serverless-deploy-${self:provider.stage}"
package:
exclude:
- .git/**
- .circleci/
- lambda/test
- node_modules
- "*.md"
- "*.rb"
- Gemfile
- Gemfile.lock
- .eslintrc.json
iamRoleStatements:
- Effect: 'Allow'
Action:
- "sns:Publish"
Resource:
- "${self:custom.failure_notification_sns_arn}"
- Effect: "Allow"
Action:
- "s3:*"
Resource:
- "arn:aws:s3:::${self:custom.imageHostBucket}/*"
- "arn:aws:s3:::${self:custom.imageUploadBucket}/*"
- Effect: "Allow"
Action:
- "states:StartExecution"
Resource:
- "*"
- Effect: "Allow"
Action:
- "rekognition:*"
Resource:
- "*"
- Effect: "Allow"
Action:
- "lambda:InvokeFunction"
Resource:
- "arn:aws:lambda:::${self:custom.imageUploadBucket}/*"
custom:
logRetentionInDays: 14
defaultStage: dev
customDomain:
domainName: images.image-service-${self:provider.stage}.pinster.io
basePath: '(none)'
stage: ${self:provider.stage}
createRoute53Record: true
certificateName: images.image-service-dev.pinster.io # This is the name of the cert, it covers prod too.
endpointType: 'regional'
failure_notification_sns: ImageServiceFailureSNS${self:provider.stage}
failure_notification_sns_arn:
Fn::Join:
- ":"
- - arn
- aws
- sns
- Ref: AWS::Region
- Ref: AWS::AccountId
- ${self:custom.failure_notification_sns}
imageHostBucket: image-service-${self:provider.stage}.pinster.io
imageHostBucketUrl: https://${self:custom.imageHostBucket}
imageUploadBucket: image-service-upload-${self:provider.stage}.pinster.io
thumbnailProcessorName: thumbnail-processor-${self:provider.stage}
pinsterApiUrl: https://api-${self:provider.stage}.pinster.io
imageServiceUrl: https://${self:custom.customDomain.domainName}
functions:
imageUploader:
handler: lambda/lambda.upload
events:
- http:
method: post
cors: true
path: /
memorySize: 1024
environment:
BUCKET_NAME: ${self:custom.imageUploadBucket}
API_PUBLIC_KEY: API_PUBLIC_KEY_CHANGE_ME
PINSTER_API_URL: ${self:custom.pinsterApiUrl}
IMAGE_SERVICE_URL: ${self:custom.imageServiceUrl}
startExecution:
handler: lambda/lambda.startExecution
events:
- s3:
bucket: ${self:custom.imageUploadBucket}
event: s3:ObjectCreated:*
rules:
- prefix: raw/
memorySize: 128
environment:
STATEMACHINE_ARN: "arn:aws:states:#{AWS::Region}:#{AWS::AccountId}:stateMachine:${self:custom.thumbnailProcessorName}"
generateThumbnail:
handler: lambda/lambda.generateThumbnail
events:
- http:
method: get
path: generate
cors: true
memorySize: 2048
environment:
BUCKET: ${self:custom.imageHostBucket}
URL: ${self:custom.imageHostBucketUrl}
moderate:
handler: lambda/lambda.moderate
memorySize: 128
moveImage:
handler: lambda/lambda.moveImage
memorySize: 512
environment:
BUCKET: ${self:custom.imageHostBucket}
PREFIX: ""
URL: ${self:custom.imageHostBucketUrl}
notifySuccess:
handler: lambda/lambda.notifySuccess
memorySize: 128
environment:
PINSTER_API_URL: ${self:custom.pinsterApiUrl}
PRIVATE_KEY: PRIVATE_KEY_CHANGE_ME
IMAGE_SERVICE_URL: ${self:custom.imageServiceUrl}
notifyFailure:
handler: lambda/lambda.notifyFailure
memorySize: 128
environment:
PINSTER_API_URL: ${self:custom.pinsterApiUrl}
SNS_ARN: ${self:custom.failure_notification_sns_arn}
PRIVATE_KEY: PRIVATE_KEY_CHANGE_ME
IMAGE_SERVICE_URL: ${self:custom.imageServiceUrl}
stepFunctions:
stateMachines:
imageUploadHandlerMachine:
name: ${self:custom.thumbnailProcessorName}
definition:
Comment: "StateMachine for images"
StartAt: Moderate
States:
Moderate:
Type: Task
Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-moderate"
Next: MoveImage
Catch:
- ErrorEquals:
- ModerationThresholdExceeded
Next: NotifyFailure
MoveImage:
Type: Task
Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-moveImage"
Next: NotifySuccess
NotifySuccess:
Type: Task
Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-notifySuccess"
End: true
NotifyFailure:
Type: Task
Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-notifyFailure"
End: true
resources:
Resources:
# if the bucket name is changed then this resource name must be changed! This is a stupid limitation of serverless
# This is handled by serverless.rb
# https://github.com/serverless/serverless/issues/2486
# https://github.com/serverless/serverless/issues/2749
S3BucketImageserviceuploadpinsterio:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: ${self:custom.imageUploadBucket}
AccessControl: PublicReadWrite
CorsConfiguration:
CorsRules:
- AllowedHeaders: ['*']
AllowedMethods: [PUT, POST]
AllowedOrigins: ['*']
MaxAge: '3600'
S3BucketPermissionsImageUpload:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: ${self:custom.imageUploadBucket}
PolicyDocument:
Statement:
- Principal: "*"
Action:
- s3:PutObject
Effect: Allow
Sid: "AddPerm"
Resource: arn:aws:s3:::${self:custom.imageUploadBucket}/*
ImageHostBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: ${self:custom.imageHostBucket}
AccessControl: PublicReadWrite
CorsConfiguration:
CorsRules:
- AllowedHeaders: ['*']
AllowedMethods: [GET, PUT, HEAD]
AllowedOrigins: ['*']
MaxAge: '3600'
WebsiteConfiguration:
IndexDocument: index.html
RoutingRules:
- RedirectRule:
HostName: ${self:custom.customDomain.domainName}
HttpRedirectCode: 307
Protocol: https
ReplaceKeyPrefixWith: generate?key=
RoutingRuleCondition:
HttpErrorCodeReturnedEquals: 404
S3BucketPermissionsImageHost:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: ${self:custom.imageHostBucket}
PolicyDocument:
Statement:
- Principal: "*"
Action:
- s3:GetObject
- s3:PutObject
Effect: Allow
Sid: "AddPerm"
Resource: arn:aws:s3:::${self:custom.imageHostBucket}/*
ImageHostCloudfront:
Type: AWS::CloudFront::Distribution
DependsOn:
- ImageHostBucket
Properties:
DistributionConfig:
Aliases:
- ${self:custom.imageHostBucket}
Comment: "Image hosting for ${self:provider.stage}"
Origins:
- DomainName: "${self:custom.imageHostBucket}.s3-website-us-east-1.amazonaws.com"
Id: S3Origin
CustomOriginConfig:
HTTPPort: '80'
HTTPSPort: '443'
OriginProtocolPolicy: http-only
Enabled: true
HttpVersion: 'http2'
DefaultRootObject: index.html
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
Compress: true
#This prevents CloudFront from caching our 307 redirect when we need to create a thumbnail on the fly.
#This also means that any item we want to serve from CloudFront MUST have a Cache-Control header set.
#Otherwise CloudFront will not cache anything.
DefaultTTL: 0
TargetOriginId: S3Origin
ForwardedValues:
QueryString: true
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
PriceClass: PriceClass_100
ViewerCertificate:
AcmCertificateArn: arn:aws:acm:us-east-1:582149114309:certificate/4527b2fc-14a6-417c-a35c-82f5426e516a
SslSupportMethod: sni-only
ImageHostDNSName:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneName: pinster.io.
RecordSets:
- Name: "${self:custom.imageHostBucket}"
Type: CNAME
TTL: 900
ResourceRecords:
- Fn::GetAtt:
- ImageHostCloudfront
- DomainName
ImageServiceFailureSNS:
Type: "AWS::SNS::Topic"
Properties:
TopicName: "${self:custom.failure_notification_sns}"
plugins:
- serverless-step-functions
- serverless-pseudo-parameters
- serverless-domain-manager
- serverless-plugin-log-retention