-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
282 lines (263 loc) · 8.08 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
org: animl
app: animl
service: animl-api
plugins:
- serverless-plugin-typescript
- serverless-offline
custom:
serverlessPluginTypescript:
tsConfigFileLocation: './tsconfig.json'
package:
patterns:
- '!backups/**'
- '!.build/**'
provider:
name: aws
stage: ${opt:stage, 'dev'}
region: us-west-2
profile: animl
environment:
REGION: ${self:provider.region}
STAGE: ${self:provider.stage}
ACCOUNT: ${aws:accountId}
iam:
role:
statements:
- Effect: Allow
Action:
- sqs:*
Resource:
- !Sub 'arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:animl-ingest-${self:provider.stage}-batch-*'
- Fn::GetAtt:
- inferenceQueue
- Arn
- Fn::GetAtt:
- taskQueue
- Arn
- Effect: Allow
Action:
- sagemaker:InvokeEndpoint
Resource: '*'
- Effect: Allow
Action:
- ses:SendEmail
- ses:SendRawEmail
Resource: '*'
- Effect: Allow
Action:
- ssm:GetParameters
Resource: !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*
- Effect: Allow
Action:
- secretsmanager:GetSecretValue
Resource: '*'
- Effect: 'Allow'
Action:
- 'lambda:InvokeFunction'
Resource:
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:IngestDelete-${self:provider.stage}
- Effect: 'Allow'
Action:
- 's3:PutObject'
- 's3:GetObject'
Resource:
- Fn::Join:
- ''
- - 'arn:aws:s3:::'
- 'animl-images-ingestion-${self:provider.stage}'
- Fn::Join:
- ''
- - 'arn:aws:s3:::'
- 'animl-images-ingestion-${self:provider.stage}'
- '/*'
- Effect: 'Allow'
Action:
- 'cognito-idp:ListGroups'
- 'cognito-idp:AdminGetUser'
- 'cognito-idp:ListUsers'
- 'cognito-idp:ListUsersInGroup'
- 'cognito-idp:AdminListGroupsForUser'
- 'cognito-idp:AdminRemoveUserFromGroup'
- 'cognito-idp:AdminAddUserToGroup'
- 'cognito-idp:AdminCreateUser'
- 'cognito-idp:AdminConfirmSignUp'
- 'cognito-idp:CreateGroup'
Resource:
- !Sub arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/{{resolve:ssm:/application/cognito/userPoolId:1}}
- Effect: 'Allow'
Action:
- 's3:PutObject'
- 's3:GetObject'
Resource:
- Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: S3BucketAnimlexporteddata
- Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: S3BucketAnimlexporteddata
- '/*'
- Effect: 'Allow'
Action:
- 's3:DeleteObject'
Resource:
- Fn::Join:
- ''
- - 'arn:aws:s3:::'
- 'animl-images-serving-${self:provider.stage}'
- Fn::Join:
- ''
- - 'arn:aws:s3:::'
- 'animl-images-serving-${self:provider.stage}/*'
apiGateway:
apiKeys:
- name: animlApiKeyInternal-${self:provider.stage}
value: '{{resolve:secretsmanager:api-key-${self:provider.stage}:SecretString:apikey}}'
functions:
graphql:
handler: src/api/handler.server
runtime: nodejs20.x
events:
- http:
path: /
method: post
cors: true
- http:
path: /internal
method: post
cors: true
private: true
- http:
path: /external
method: post
cors:
headers: '*'
authorizer:
name: CognitoAuthN
type: COGNITO_USER_POOLS
arn: !Sub arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/{{resolve:ssm:/application/cognito/userPoolId:1}}
memorySize: 3008
timeout: 30
inference:
handler: src/ml/handler.inference
runtime: nodejs20.x
reservedConcurrency: 20
events:
- sqs:
arn:
Fn::GetAtt:
- inferenceQueue
- Arn
batchSize: 1
functionResponseType: ReportBatchItemFailures
timeout: 120
batchinference:
handler: src/ml/handler.inference
runtime: nodejs20.x
reservedConcurrency: 8
memorySize: 2048
timeout: 120
task:
handler: src/task/handler.handler
runtime: nodejs20.x
reservedConcurrency: 10
events:
- sqs:
arn:
Fn::GetAtt:
- taskQueue
- Arn
batchSize: 1
timeout: 900
resources:
Resources:
# Secrets Manager secret for API key
apiKey:
Type: AWS::SecretsManager::Secret
Properties:
Description: API Key that will allow access for the APIs
GenerateSecretString:
GenerateStringKey: apikey
SecretStringTemplate: '{"user": "api"}'
Name: api-key-${self:provider.stage}
# Inference SQS queue
inferenceQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: inferenceQueue-${self:provider.stage}
VisibilityTimeout: 120
RedrivePolicy:
deadLetterTargetArn:
'Fn::GetAtt':
- inferenceDeadLetterQueue
- Arn
maxReceiveCount: 5
# Inference - dead-letter SQS queue
inferenceDeadLetterQueue:
Type: 'AWS::SQS::Queue'
Properties:
QueueName: inferenceDLQ-${self:provider.stage}
MessageRetentionPeriod: 1209600 # 14 days in seconds
# Task SQS queue
taskQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: taskQueue-${self:provider.stage}
VisibilityTimeout: 900
RedrivePolicy:
deadLetterTargetArn:
'Fn::GetAtt':
- taskDeadLetterQueue
- Arn
maxReceiveCount: 5
# Task - dead-letter SQS queue
taskDeadLetterQueue:
Type: 'AWS::SQS::Queue'
Properties:
QueueName: taskDLQ-${self:provider.stage}
MessageRetentionPeriod: 1209600 # 14 days in seconds
# SSM Param - API URL
SSMParameterApiurl:
Type: AWS::SSM::Parameter
Properties:
Description: Animl API url
Name: /api/url-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value: !Sub https://${ApiGatewayRestApi}.execute-api.${AWS::Region}.amazonaws.com/${opt:stage, self:provider.stage, 'dev'}/internal/
# SSM Param - inference queue url
SSMParameterInferencequeue:
Type: AWS::SSM::Parameter
Properties:
Description: Inference queue url
Name: /ml/inference-queue-url-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value: !Ref inferenceQueue
# SSM Param - task queue url
SSMParameterTaskqueue:
Type: AWS::SSM::Parameter
Properties:
Description: Task queue url
Name: /tasks/task-queue-url-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value: !Ref taskQueue
# Exported data bucket
S3BucketAnimlexporteddata:
Type: AWS::S3::Bucket
Properties:
BucketName: animl-exported-data-${opt:stage, self:provider.stage, 'dev'}
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
# SSM Param - exported data bucket
SSMParameterAnimlexporteddata:
Type: AWS::SSM::Parameter
Properties:
Description: Animl exported data bucket name
Name: /exports/exported-data-bucket-${opt:stage, self:provider.stage, 'dev'}
Type: String
Value: animl-exported-data-${opt:stage, self:provider.stage, 'dev'}
# NOTE: SSM Params /ml/megadetector-api-key and /db/mongo-db-url
# contain secret keys so need to be created manually in the AWS console