-
Notifications
You must be signed in to change notification settings - Fork 8
/
template.yaml
366 lines (341 loc) · 12.5 KB
/
template.yaml
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
AWSTemplateFormatVersion: '2010-09-09'
Description: OpenCV Mask RCNN running in an AWS Lambda Layer. This also creates an S3 bucket, API Gateway.
Transform: AWS::Serverless-2016-10-31
Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: opencv-lambda-mask-image-api
Description: An API Gateway with a Lambda Integration for OpenCV Object Detection Demo
FailOnWarnings: true
EndpointConfiguration:
Types:
- REGIONAL
ApiGatewayResource:
Type: AWS::ApiGateway::Resource
Properties:
ParentId: !GetAtt ApiGatewayRestApi.RootResourceId
PathPart: 'OpenCVMaskFunction'
RestApiId: !Ref ApiGatewayRestApi
ApiGatewayIamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: ''
Effect: 'Allow'
Principal:
Service:
- 'apigateway.amazonaws.com'
Action:
- 'sts:AssumeRole'
Path: '/'
Policies:
- PolicyName: LambdaAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action: 'lambda:*'
Resource: !GetAtt OpenCVMaskLambda.Arn
ApiGatewayMethodPOST:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref ApiGatewayRestApi
ResourceId: !Ref ApiGatewayResource
HttpMethod: ANY
AuthorizationType: NONE
ApiKeyRequired: false
Integration:
Type: AWS
IntegrationHttpMethod: POST
Uri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${OpenCVMaskLambda.Arn}/invocations'
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Methods: "'POST,OPTIONS'"
method.response.header.Access-Control-Allow-Origin: "'*'"
ResponseTemplates:
application/json: ''
RequestTemplates:
application/json: '{"statusCode": 200}'
RequestParameters:
method.request.querystring.name: false
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json: 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: false
method.response.header.Access-Control-Allow-Methods: false
method.response.header.Access-Control-Allow-Origin: false
ApiGatewayStage:
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId: !Ref ApiGatewayDeployment
Description: Lambda API Stage Demo
RestApiId: !Ref ApiGatewayRestApi
StageName: 'Demo'
ApiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn: ApiGatewayMethodPOST
Properties:
Description: Lambda API Deployment
RestApiId: !Ref ApiGatewayRestApi
Permission:
Type: 'AWS::Lambda::Permission'
DependsOn: ApiGatewayStage
Properties:
FunctionName:
'Fn::GetAtt':
- OpenCVMaskLambda
- Arn
Action: 'lambda:InvokeFunction'
Principal: apigateway.amazonaws.com
SourceArn:
'Fn::Join':
- ''
- - 'arn:aws:execute-api:'
- Ref: 'AWS::Region'
- ':'
- Ref: 'AWS::AccountId'
- ':'
- Ref: ApiGatewayRestApi
- /*
MaskRCNNModelLayer:
Type: AWS::Lambda::LayerVersion
DependsOn: CopyZipsMaskInvoke
Properties:
CompatibleRuntimes:
- python3.7
- python3.8
Content:
S3Bucket: !Ref MaskBucket
S3Key: lambda-layer-zip/mask-rcnn.zip
Description: OpenCV Mask RCNN Model Layer
LayerName: opencv-mask-model-layer
OpenCVSourceLayer:
Type: AWS::Lambda::LayerVersion
DependsOn: CopyZipsMaskInvoke
Properties:
CompatibleRuntimes:
- python3.7
- python3.8
Content:
S3Bucket: !Ref MaskBucket
S3Key: lambda-layer-zip/opencv-python3-7-6.zip
Description: OpenCV Mask Source Layer
LayerName: opencv-mask-source-layer
OpenCVMaskLambda:
Type: AWS::Lambda::Function
DependsOn: CopyZipsMaskInvoke
Properties:
Code:
S3Bucket: !Ref MaskBucket
S3Key: mask-rcnn-code-zip/Lambda_Function_Python.zip
Description: OpenCV Mask Image Lambda
Environment:
Variables:
PYTHONPATH: /opt/
FunctionName: OpenCVMaskFunction
Handler: lambda_function.lambda_handler
Layers:
- !Ref OpenCVSourceLayer
- !Ref MaskRCNNModelLayer
MemorySize: 3008
Runtime: python3.7
Timeout: 900
Role:
Fn::GetAtt:
- LambdaRole
- Arn
LambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName:
Fn::Sub: lambda-role-opencv-mask-rcnn
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Version: 2012-10-17
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AWSLambdaExecute
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/AmazonRekognitionFullAccess
Path: /
CopyZipsLambda:
Type: AWS::Lambda::Function
Properties:
Description: Copies objects from a source S3 bucket to a destination
FunctionName: OpenCVMaskInitFileCopier
Handler: index.handler
Runtime: python3.7
Role: !GetAtt LambdaRole.Arn
Timeout: 240
Code:
ZipFile: |
import json
import logging
import threading
import boto3
import cfnresponse
def copy_objects(source_bucket, dest_bucket, prefix, objects):
s3 = boto3.client('s3')
for o in objects:
key = prefix + o
copy_source = {
'Bucket': source_bucket,
'Key': key
}
print('copy_source: %s' % copy_source)
print('dest_bucket = %s'%dest_bucket)
print('key = %s' %key)
s3.copy_object(CopySource=copy_source, Bucket=dest_bucket,
Key=key)
s3.put_object_acl(ACL = "public-read", Bucket=dest_bucket, Key = key)
def delete_objects(bucket, prefix, objects):
s3 = boto3.client('s3')
objects = {'Objects': [{'Key': prefix + o} for o in objects]}
s3.delete_objects(Bucket=bucket, Delete=objects)
def timeout(event, context):
logging.error('Execution is about to time out, sending failure response to CloudFormation')
cfnresponse.send(event, context, cfnresponse.FAILED, {}, None)
def handler(event, context):
# make sure we send a failure to CloudFormation if the function
# is going to timeout
timer = threading.Timer((context.get_remaining_time_in_millis() / 1000.00) - 0.5, timeout, args=[event, context])
timer.start()
print('Received event: %s' % json.dumps(event))
status = cfnresponse.SUCCESS
try:
source_bucket = event['ResourceProperties']['SourceBucket']
dest_bucket = event['ResourceProperties']['DestBucket']
prefix = event['ResourceProperties']['Prefix']
objects = event['ResourceProperties']['Objects']
if event['RequestType'] == 'Delete':
delete_objects(dest_bucket, prefix, objects)
else:
copy_objects(source_bucket, dest_bucket, prefix, objects)
except Exception as e:
logging.error('Exception: %s' % e, exc_info=True)
status = cfnresponse.FAILED
finally:
timer.cancel()
cfnresponse.send(event, context, status, {}, None)
S3ApiEndPointLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: OpenCVMaskInitCreateApiGatewayURLFile
Code:
ZipFile: |
import boto3
import os
import cfnresponse
import logging
import threading
import json
def timeout(event, context):
logging.error('Execution is about to time out, sending failure response to CloudFormation')
cfnresponse.send(event, context, cfnresponse.FAILED, {}, None)
def lambda_handler(event, context):
status = cfnresponse.SUCCESS
timer = threading.Timer((context.get_remaining_time_in_millis() / 1000.00) - 0.5, timeout, args=[event, context])
timer.start()
apiString = os.getenv('ApiEndpoint')
bodyString = f"var appVariables = {{apiEndpoint: '{apiString}'}};"
try:
aBucket = os.getenv('Bucket')
client = boto3.client('s3')
if event['RequestType'] == 'Delete':
client.delete_object(Bucket=aBucket, Key='demo-ui-config.js')
else:
client.put_object(Body=bodyString, Bucket=aBucket, Key='demo-ui-config.js')
client.put_object_acl(ACL = "public-read", Bucket = aBucket, Key = 'demo-ui-config.js')
except Exception as e:
logging.error('Exception: %s' % e, exc_info=True)
status = cfnresponse.FAILED
finally:
timer.cancel()
cfnresponse.send(event, context, status, {})
return True
Environment:
Variables:
ApiEndpoint: !Sub 'https://${ApiGatewayRestApi}.execute-api.${AWS::Region}.amazonaws.com/${ApiGatewayStage}/${OpenCVMaskLambda}'
Bucket: !Ref MaskBucket
Runtime: python3.7
Handler: index.lambda_handler
Role:
Fn::GetAtt:
- LambdaRole
- Arn
DependsOn:
- ApiGatewayStage
- MaskBucket
CopyZipsMaskInvoke:
Type: AWS::CloudFormation::CustomResource
DependsOn:
- CopyZipsLambda
- MaskBucket
Version: "1.0"
Properties:
ServiceToken: !GetAtt CopyZipsLambda.Arn
DestBucket: !Ref MaskBucket
SourceBucket: mask-rcnn-source
Prefix: ''
Objects:
- mask-rcnn-code-zip/Lambda_Function_Python.zip
- lambda-layer-zip/mask-rcnn.zip
- lambda-layer-zip/opencv-python3-7-6.zip
- index.html
- style.css
- images/cat.jpg
- images/dog.jpg
- images/face.jpg
- images/puppies.jpg
- images/spinner.gif
PrimerInvoke:
Type: AWS::CloudFormation::CustomResource
DependsOn: S3ApiEndPointLambda
Version: "1.0"
Properties:
ServiceToken: !GetAtt S3ApiEndPointLambda.Arn
MaskBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: index.html
AccessControl: PublicRead
PublicAccessBlockConfiguration:
BlockPublicAcls: false
BlockPublicPolicy: false
IgnorePublicAcls: false
RestrictPublicBuckets: false
BucketName: !Join
- "-"
- - "mask-rcnn-demo"
- !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split
- "/"
- !Ref "AWS::StackId"
Outputs:
BucketName:
Value: !Ref MaskBucket
Endpoint:
Value: !Sub 'https://${ApiGatewayRestApi}.execute-api.${AWS::Region}.amazonaws.com/${ApiGatewayStage}'