-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserverless.yml
220 lines (209 loc) · 7.15 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
service: lambdaautotraining
provider:
name: aws
runtime: nodejs10.x
stage: dev
region: us-east-1
role: IamRole
environment:
SERVICE: ${self:service}
REGION: ${self:provider.region}
BUCKET: ${self:service}-${opt:stage, self:provider.stage}
STAGE: ${opt:stage, self:provider.stage}
DYNAMODB_TABLE_MODELS: ${self:service}-models-${opt:stage, self:provider.stage}
DYNAMODB_TABLE_DATA: ${self:service}-data-${opt:stage, self:provider.stage}
FUNCTION_PREFIX: ${self:service}-${opt:stage, self:provider.stage}-
IAM_ROLE: ${self:service}-${opt:stage, self:provider.stage}-${self:provider.region}-managedrole
API_URL:
Fn::Join:
- ""
- - "https://"
- Ref: "ApiGatewayRestApi"
- ".execute-api.${self:provider.region}.amazonaws.com/${self:provider.stage}"
package:
exclude:
- py/**
functions:
upload:
handler: js/upload.upload
events:
- http:
path: upload
method: post
train:
handler: js/train.train
environment:
AMI_ID: ami-0f812849f5bc97db5
KEY_NAME: ec2testing
INSTANCE_TYPE: t2.micro
SPOT_DURATION: 60
VALID_HRS: 4
ECR_ID: 530583866435
ECR_REPO: lambda-auto-training/lambda-auto-training-${opt:stage, self:provider.stage}
events:
- stream:
type: dynamodb
arn:
Fn::GetAtt: [DataDynamoDbTable, StreamArn]
batchSize: 100
batchWindow: 300
infer:
handler: js/infer.infer
events:
- http:
path: infer
method: post
test:
handler: js/test.test
events:
- schedule:
rate: rate(2 minutes)
enabled: false
s3proxy:
handler: js/s3proxy.s3proxy
events:
- http:
path: s3proxy/{key}/{filename}
method: get
resources:
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:provider.environment.BUCKET}
ModelsDynamoDbTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
-
AttributeName: created
AttributeType: N
KeySchema:
-
AttributeName: created
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:provider.environment.DYNAMODB_TABLE_MODELS}
DataDynamoDbTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
-
AttributeName: created
AttributeType: N
KeySchema:
-
AttributeName: created
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:provider.environment.DYNAMODB_TABLE_DATA}
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
IamRole:
Type: AWS::IAM::Role
Properties:
Path: /
RoleName: ${self:service}-${opt:stage, self:provider.stage}-${self:provider.region}-managedrole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- ec2.amazonaws.com # added for transfer in train.js
Action: sts:AssumeRole
Policies:
- PolicyName: ${self:service}-${opt:stage, self:provider.stage}-${self:provider.region}-managedpolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
# Begin default Serverless policies
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- 'Fn::Join':
- ':'
-
- 'arn:aws:logs'
- Ref: 'AWS::Region'
- Ref: 'AWS::AccountId'
- 'log-group:/aws/lambda/*:*:*'
- Effect: "Allow"
Action:
- "s3:PutObject"
Resource:
Fn::Join:
- ""
- - "arn:aws:s3:::"
- "Ref" : "ServerlessDeploymentBucket"
# Begin custom policies
- Effect: Allow
Action:
- s3:*
Resource:
Fn::Join:
- ""
- - "arn:aws:s3:::"
- ${self:provider.environment.BUCKET}
- "/*"
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:GetRecords
- dynamodb:GetShardIterator
- dynamodb:DescribeStream
- dynamodb:ListStreams
- dynamodb:BatchWriteItem
Resource:
- "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE_MODELS}"
- "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE_DATA}"
- { Fn::GetAtt: [ DataDynamoDbTable, Arn ] }
# For custom role stream policy must be explicitly defined
- Effect: Allow
Action:
- dynamodb:GetRecords
- dynamodb:GetShardIterator
- dynamodb:DescribeStream
- dynamodb:ListStreams
Resource:
- "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE_DATA}/stream/*"
- Effect: Allow
Action:
- ec2:RunInstances
- ec2:TerminateInstances
- cloudwatch:PutMetricAlarm
- cloudwatch:DescribeAlarms
- cloudwatch:EnableAlarmActions
Resource: "*"
# Allow for transferring role to EC2 and creating EC2 instance profile
- Effect: Allow
Action:
- iam:PassRole
- iam:GetInstanceProfile
- iam:CreateInstanceProfile
- iam:AddRoleToInstanceProfile
Resource: "*"
# Used only by EC2 when role is transfered
- Effect: Allow
Action:
- ecr:GetAuthorizationToken
- ecr:GetDownloadUrlForLayer
- ecr:BatchGetImage
- ecr:BatchCheckLayerAvailability
Resource:
- "*"