@rlyle1179/serverless-aws-batch 1.1.18 → 1.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +5 -0
- package/LICENSE +21 -21
- package/README.md +362 -362
- package/index.js +66 -66
- package/lib/awscli.js +107 -107
- package/lib/batchenvironment.js +418 -418
- package/lib/batchtask.js +390 -390
- package/lib/docker.js +139 -139
- package/lib/ecr.js +29 -29
- package/lib/generateCoreTemplate.js +120 -120
- package/lib/schedule.js +90 -90
- package/package.json +35 -35
package/README.md
CHANGED
|
@@ -1,362 +1,362 @@
|
|
|
1
|
-
# Serverless Batch
|
|
2
|
-
[](http://www.serverless.com)
|
|
3
|
-
|
|
4
|
-
A Serverless v1.x plugin that makes creating and running AWS Batch tasks as easy as creating a Serverless Lambda Function.
|
|
5
|
-
|
|
6
|
-
Tested with:
|
|
7
|
-
* Serverless >= v1.43
|
|
8
|
-
* Python 3.7
|
|
9
|
-
* Node.JS 10
|
|
10
|
-
|
|
11
|
-
**Disclaimer: This project has not yet been well tested and is not yet recommended for a production system**
|
|
12
|
-
|
|
13
|
-
## Install
|
|
14
|
-
First make sure than you have Docker installed and running
|
|
15
|
-
|
|
16
|
-
Then add the plugin to your serverless project:
|
|
17
|
-
|
|
18
|
-
```
|
|
19
|
-
npm install serverless-aws-batch --save-dev
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
Modify the `serverless.yml` file to include the plugin:
|
|
23
|
-
|
|
24
|
-
```yaml
|
|
25
|
-
plugins:
|
|
26
|
-
- serverless-aws-batch
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
Next we need to define our [AWS Batch Compute Resource](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-batch-computeenvironment-computeresources.html)
|
|
30
|
-
|
|
31
|
-
```yaml
|
|
32
|
-
provider:
|
|
33
|
-
name: aws
|
|
34
|
-
region: us-east-2
|
|
35
|
-
runtime: python3.7
|
|
36
|
-
batch:
|
|
37
|
-
Type: [EC2 | SPOT] # Required
|
|
38
|
-
BidPercentage: <Integer> # Optional. Defaults to 100 if Type = SPOT (you always pay lowest market price)
|
|
39
|
-
SecurityGroupIds: # Required
|
|
40
|
-
- <Security Group ID>
|
|
41
|
-
Subnets: # Required
|
|
42
|
-
- <VPC Subnet ID>
|
|
43
|
-
InstanceTypes: # Optional
|
|
44
|
-
- <Batch-Supported-Instance-Type> # Default c5.large (cheapest)
|
|
45
|
-
MinvCpus: <Integer> # Optional. Default 0
|
|
46
|
-
MaxvCpus: <Integer> # Optional. Default 2
|
|
47
|
-
Tags: # Optional
|
|
48
|
-
<Key>: <Value> # Default "Name": "AWS Batch Instance - <service>"
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
And then define our [AWS Batch Job Definition](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-batch-jobdefinition.html)
|
|
52
|
-
on the function definition
|
|
53
|
-
|
|
54
|
-
```yaml
|
|
55
|
-
functions:
|
|
56
|
-
hello:
|
|
57
|
-
handler: <handler definition>
|
|
58
|
-
batch:
|
|
59
|
-
ContainerProperties:
|
|
60
|
-
Memory: <Integer> # Optional. Default 2048
|
|
61
|
-
Vcpus: <Integer> # Optional. Default 1
|
|
62
|
-
Command: <Command to run in docker> # Optional. Defaults to "<handler> Ref::event"
|
|
63
|
-
JobRoleArn: <ARN> # Optional. Defaults to package.iamRoleStatements
|
|
64
|
-
Environment:
|
|
65
|
-
TEST_ENV_1: Test Value 1
|
|
66
|
-
RetryStrategy:
|
|
67
|
-
Attempts: <Integer> # Optional. Defaults to 1
|
|
68
|
-
Timeout:
|
|
69
|
-
AttemptDurationSeconds: <Integer> # Optional. Defaults to 300
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
And now you should be able to write your batch function like you would any other serverless lambda function:
|
|
73
|
-
|
|
74
|
-
```python3
|
|
75
|
-
import logging
|
|
76
|
-
|
|
77
|
-
# Setup our logger to work both locally and with both AWS CloudWatch
|
|
78
|
-
if len(logging.getLogger().handlers) > 0:
|
|
79
|
-
logging.getLogger().setLevel(logging.INFO)
|
|
80
|
-
else:
|
|
81
|
-
logging.basicConfig(level=logging.INFO)
|
|
82
|
-
logger = logging.getLogger()
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
def hello(event, context):
|
|
86
|
-
logger.info(f"Hello World: {event}")
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
```yaml
|
|
90
|
-
functions:
|
|
91
|
-
hello:
|
|
92
|
-
handler: handler.hello
|
|
93
|
-
events:
|
|
94
|
-
- http:
|
|
95
|
-
path: hello
|
|
96
|
-
method: get
|
|
97
|
-
batch:
|
|
98
|
-
ContainerProperties:
|
|
99
|
-
Memory: 2048
|
|
100
|
-
Vcpus: 1
|
|
101
|
-
RetryStrategy:
|
|
102
|
-
Attempts: 1
|
|
103
|
-
Timeout:
|
|
104
|
-
AttemptDurationSeconds: 3600
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
## Implementation
|
|
108
|
-
|
|
109
|
-
What happens behind the scenes is that the Serverless Framework's ".zip" artifact gets installed into a
|
|
110
|
-
[lambci/lambda:\<env>](https://hub.docker.com/r/lambci/lambda/) docker image and uploaded to [ECR](https://aws.amazon.com/ecr/).
|
|
111
|
-
|
|
112
|
-
* _Note: Currently using copy of the docker images (https://cloud.docker.com/u/justinram11/repository/list) that unsets the
|
|
113
|
-
ACCESS_KEY_ID and SECRET_ACCESS_KEY environmental variables so that we can use the role attached to the EC2 instance._
|
|
114
|
-
|
|
115
|
-
A "schedule" lambda function is then created with the same name as the regular Serverless Framework's lambda function
|
|
116
|
-
that can be called in to pass the input event to the AWS Batch Job. The "schedule" lambda function can also be subscribed
|
|
117
|
-
to events the same way a regular Serverless Lambda Function is.
|
|
118
|
-
|
|
119
|
-

|
|
120
|
-
|
|
121
|
-
## Example
|
|
122
|
-
|
|
123
|
-
After deploying with `sls deploy`, navigate to the [AWS Lambda Console](https://us-east-2.console.aws.amazon.com/lambda/home?region=us-east-2#/functions/serverless-demo-dev-hello?tab=graph)
|
|
124
|
-
and create a new Test Event with any event value and click the "Test" button.
|
|
125
|
-
|
|
126
|
-
The lambda function will automatically create a new AWS Batch Job which should be visible on the [AWS Batch Console](https://us-east-2.console.aws.amazon.com/batch/home?region=us-east-2#/dashboard)
|
|
127
|
-
|
|
128
|
-
Logs are visible in CloudWatch under the [/aws/batch/job](https://us-east-2.console.aws.amazon.com/cloudwatch/home?region=us-east-2#logStream:group=/aws/batch/job;streamFilter=typeLogStreamPrefix) Log Group
|
|
129
|
-
|
|
130
|
-
`serverless.yml`
|
|
131
|
-
```yaml
|
|
132
|
-
service: serverless-demo
|
|
133
|
-
|
|
134
|
-
provider:
|
|
135
|
-
name: aws
|
|
136
|
-
region: us-east-2
|
|
137
|
-
runtime: python3.7 | nodejs12.x # TODO Select one
|
|
138
|
-
# Creates a SPOT Compute Environment using the VPC defined in our Resources
|
|
139
|
-
batch:
|
|
140
|
-
Type: SPOT
|
|
141
|
-
BidPercentage: 100
|
|
142
|
-
SecurityGroupIds:
|
|
143
|
-
- Ref: AllowAllSecurityGroup
|
|
144
|
-
Subnets:
|
|
145
|
-
- Ref: PublicSubnet
|
|
146
|
-
MinvCpus: 0
|
|
147
|
-
MaxvCpus: 2
|
|
148
|
-
# Allows the Batch Job (code written in handler.py) to list all of our S3 Buckets.
|
|
149
|
-
iamRoleStatements:
|
|
150
|
-
- Effect: "Allow"
|
|
151
|
-
Action:
|
|
152
|
-
- "s3:ListAllMyBuckets"
|
|
153
|
-
Resource: "*"
|
|
154
|
-
|
|
155
|
-
plugins:
|
|
156
|
-
# TODO Required if a python project
|
|
157
|
-
# - serverless-python-requirements
|
|
158
|
-
- serverless-aws-batch
|
|
159
|
-
|
|
160
|
-
package:
|
|
161
|
-
include:
|
|
162
|
-
- handler.py
|
|
163
|
-
exclude:
|
|
164
|
-
- .serverless/**
|
|
165
|
-
- node_modules/**
|
|
166
|
-
|
|
167
|
-
functions:
|
|
168
|
-
hello:
|
|
169
|
-
handler: handler.hello
|
|
170
|
-
events:
|
|
171
|
-
- http:
|
|
172
|
-
path: hello
|
|
173
|
-
method: get
|
|
174
|
-
# Creates a Batch Job with 2GB of memory and 1 vCPU.
|
|
175
|
-
batch:
|
|
176
|
-
ContainerProperties:
|
|
177
|
-
Memory: 2048
|
|
178
|
-
Vcpus: 1
|
|
179
|
-
RetryStrategy:
|
|
180
|
-
Attempts: 1
|
|
181
|
-
Timeout:
|
|
182
|
-
AttemptDurationSeconds: 3600
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
# WARNING: Should not be used in production (allows all traffic)
|
|
186
|
-
# VPC: "Batch VPC"
|
|
187
|
-
# - CIDR: 10.224.0.0/16
|
|
188
|
-
# - Subnet: "Batch <region>a Public Subnet"
|
|
189
|
-
# - CIDR: 10.224.0.0/20
|
|
190
|
-
# - Allows all incoming and outgoing traffic
|
|
191
|
-
# - Security Group: "Batch Allow All"
|
|
192
|
-
# - Allows all incoming and outgoing traffic
|
|
193
|
-
resources:
|
|
194
|
-
Resources:
|
|
195
|
-
|
|
196
|
-
VPC:
|
|
197
|
-
Type: AWS::EC2::VPC
|
|
198
|
-
Properties:
|
|
199
|
-
CidrBlock: 10.224.0.0/16
|
|
200
|
-
EnableDnsSupport: true
|
|
201
|
-
EnableDnsHostnames: true
|
|
202
|
-
Tags:
|
|
203
|
-
- Key: Name
|
|
204
|
-
Value: Batch VPC
|
|
205
|
-
|
|
206
|
-
InternetGateway:
|
|
207
|
-
Type: AWS::EC2::InternetGateway
|
|
208
|
-
Properties:
|
|
209
|
-
Tags:
|
|
210
|
-
- Key: Name
|
|
211
|
-
Value: Batch Internet Gateway
|
|
212
|
-
|
|
213
|
-
InternetGatewayAttachment:
|
|
214
|
-
Type: AWS::EC2::VPCGatewayAttachment
|
|
215
|
-
Properties:
|
|
216
|
-
InternetGatewayId: !Ref InternetGateway
|
|
217
|
-
VpcId: !Ref VPC
|
|
218
|
-
|
|
219
|
-
PublicSubnet:
|
|
220
|
-
Type: AWS::EC2::Subnet
|
|
221
|
-
Properties:
|
|
222
|
-
VpcId: !Ref VPC
|
|
223
|
-
AvailabilityZone: ${self:provider.region}a
|
|
224
|
-
CidrBlock: 10.224.0.0/20
|
|
225
|
-
MapPublicIpOnLaunch: true
|
|
226
|
-
Tags:
|
|
227
|
-
- Key: Name
|
|
228
|
-
Value: Batch ${self:provider.region}a Public Subnet
|
|
229
|
-
|
|
230
|
-
PublicRouteTable:
|
|
231
|
-
Type: AWS::EC2::RouteTable
|
|
232
|
-
Properties:
|
|
233
|
-
VpcId: !Ref VPC
|
|
234
|
-
Tags:
|
|
235
|
-
- Key: Name
|
|
236
|
-
Value: Batch Public Routes
|
|
237
|
-
|
|
238
|
-
DefaultPublicRoute:
|
|
239
|
-
Type: AWS::EC2::Route
|
|
240
|
-
DependsOn: InternetGatewayAttachment
|
|
241
|
-
Properties:
|
|
242
|
-
RouteTableId: !Ref PublicRouteTable
|
|
243
|
-
DestinationCidrBlock: 0.0.0.0/0
|
|
244
|
-
GatewayId: !Ref InternetGateway
|
|
245
|
-
|
|
246
|
-
PublicSubnetRouteTableAssociation:
|
|
247
|
-
Type: AWS::EC2::SubnetRouteTableAssociation
|
|
248
|
-
Properties:
|
|
249
|
-
RouteTableId: !Ref PublicRouteTable
|
|
250
|
-
SubnetId: !Ref PublicSubnet
|
|
251
|
-
|
|
252
|
-
PublicNetworkAcl:
|
|
253
|
-
Type: AWS::EC2::NetworkAcl
|
|
254
|
-
DependsOn: VPC
|
|
255
|
-
Properties:
|
|
256
|
-
VpcId: !Ref VPC
|
|
257
|
-
Tags:
|
|
258
|
-
- Key: Name
|
|
259
|
-
Value: AWS Batch Public ACL
|
|
260
|
-
|
|
261
|
-
InboundPublicNetworkAclAllowAll:
|
|
262
|
-
Type: AWS::EC2::NetworkAclEntry
|
|
263
|
-
Properties:
|
|
264
|
-
NetworkAclId: !Ref PublicNetworkAcl
|
|
265
|
-
RuleNumber: 100
|
|
266
|
-
Protocol: -1
|
|
267
|
-
RuleAction: allow
|
|
268
|
-
Egress: false
|
|
269
|
-
CidrBlock: 0.0.0.0/0
|
|
270
|
-
PortRange:
|
|
271
|
-
From: 0
|
|
272
|
-
To: 65535
|
|
273
|
-
|
|
274
|
-
OutboundPublicNetworkAclAllowAll:
|
|
275
|
-
Type: AWS::EC2::NetworkAclEntry
|
|
276
|
-
Properties:
|
|
277
|
-
NetworkAclId: !Ref PublicNetworkAcl
|
|
278
|
-
RuleNumber: 100
|
|
279
|
-
Protocol: -1
|
|
280
|
-
RuleAction: allow
|
|
281
|
-
Egress: true
|
|
282
|
-
CidrBlock: 0.0.0.0/0
|
|
283
|
-
PortRange:
|
|
284
|
-
From: 0
|
|
285
|
-
To: 65535
|
|
286
|
-
|
|
287
|
-
PublicSubnetNetworkAclAssociation1:
|
|
288
|
-
Type: AWS::EC2::SubnetNetworkAclAssociation
|
|
289
|
-
Properties:
|
|
290
|
-
SubnetId: !Ref PublicSubnet
|
|
291
|
-
NetworkAclId: !Ref PublicNetworkAcl
|
|
292
|
-
|
|
293
|
-
AllowAllSecurityGroup:
|
|
294
|
-
Type: AWS::EC2::SecurityGroup
|
|
295
|
-
Properties:
|
|
296
|
-
GroupName: Batch Allow All
|
|
297
|
-
GroupDescription: "Security group for batch instances that allows all traffic"
|
|
298
|
-
VpcId: !Ref VPC
|
|
299
|
-
SecurityGroupIngress:
|
|
300
|
-
- IpProtocol: "-1"
|
|
301
|
-
CidrIp: 0.0.0.0/0
|
|
302
|
-
SecurityGroupEgress:
|
|
303
|
-
- IpProtocol: "-1"
|
|
304
|
-
CidrIp: 0.0.0.0/0
|
|
305
|
-
```
|
|
306
|
-
|
|
307
|
-
### Python
|
|
308
|
-
|
|
309
|
-
`handler.py`
|
|
310
|
-
```python3
|
|
311
|
-
import json
|
|
312
|
-
import time
|
|
313
|
-
import logging
|
|
314
|
-
import boto3
|
|
315
|
-
|
|
316
|
-
# Setup our logger to work with both AWS CloudWatch and locally
|
|
317
|
-
if len(logging.getLogger().handlers) > 0:
|
|
318
|
-
logging.getLogger().setLevel(logging.INFO)
|
|
319
|
-
else:
|
|
320
|
-
logging.basicConfig(level=logging.INFO)
|
|
321
|
-
logger = logging.getLogger()
|
|
322
|
-
|
|
323
|
-
# Setup our boto3 clients
|
|
324
|
-
s3 = boto3.client('s3')
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
def hello(event, context):
|
|
328
|
-
logger.info(f"Hello world: {event}")
|
|
329
|
-
|
|
330
|
-
response = s3.list_buckets()
|
|
331
|
-
logger.info(f"S3 Buckets: {response}")
|
|
332
|
-
|
|
333
|
-
```
|
|
334
|
-
|
|
335
|
-
### Node.JS
|
|
336
|
-
|
|
337
|
-
`handler.js`
|
|
338
|
-
```nodejs
|
|
339
|
-
'use strict';
|
|
340
|
-
const process = require("process");
|
|
341
|
-
const AWS = require("aws-sdk");
|
|
342
|
-
const s3 = new AWS.S3();
|
|
343
|
-
|
|
344
|
-
module.exports.hello = (event, context, callback) => {
|
|
345
|
-
|
|
346
|
-
console.log(`Received event: ${JSON.stringify(event)}`);
|
|
347
|
-
|
|
348
|
-
console.log(process.env.AWS_ACCESS_KEY_ID);
|
|
349
|
-
console.log(process.env.AWS_SECRET_ACCESS_KEY);
|
|
350
|
-
|
|
351
|
-
s3.listBuckets({}, function(err, data) {
|
|
352
|
-
console.log(`List buckets data: ${data} err: ${err}`);
|
|
353
|
-
});
|
|
354
|
-
};
|
|
355
|
-
```
|
|
356
|
-
|
|
357
|
-
## Contributors
|
|
358
|
-
|
|
359
|
-
* [simonobe](https://github.com/simonobe)
|
|
360
|
-
* [gcaliene](https://github.com/gcaliene)
|
|
361
|
-
* [MEGApixel23](https://github.com/MEGApixel23)
|
|
362
|
-
* [claydanford](https://github.com/claydanford)
|
|
1
|
+
# Serverless Batch
|
|
2
|
+
[](http://www.serverless.com)
|
|
3
|
+
|
|
4
|
+
A Serverless v1.x plugin that makes creating and running AWS Batch tasks as easy as creating a Serverless Lambda Function.
|
|
5
|
+
|
|
6
|
+
Tested with:
|
|
7
|
+
* Serverless >= v1.43
|
|
8
|
+
* Python 3.7
|
|
9
|
+
* Node.JS 10
|
|
10
|
+
|
|
11
|
+
**Disclaimer: This project has not yet been well tested and is not yet recommended for a production system**
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
First make sure than you have Docker installed and running
|
|
15
|
+
|
|
16
|
+
Then add the plugin to your serverless project:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
npm install serverless-aws-batch --save-dev
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Modify the `serverless.yml` file to include the plugin:
|
|
23
|
+
|
|
24
|
+
```yaml
|
|
25
|
+
plugins:
|
|
26
|
+
- serverless-aws-batch
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Next we need to define our [AWS Batch Compute Resource](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-batch-computeenvironment-computeresources.html)
|
|
30
|
+
|
|
31
|
+
```yaml
|
|
32
|
+
provider:
|
|
33
|
+
name: aws
|
|
34
|
+
region: us-east-2
|
|
35
|
+
runtime: python3.7
|
|
36
|
+
batch:
|
|
37
|
+
Type: [EC2 | SPOT] # Required
|
|
38
|
+
BidPercentage: <Integer> # Optional. Defaults to 100 if Type = SPOT (you always pay lowest market price)
|
|
39
|
+
SecurityGroupIds: # Required
|
|
40
|
+
- <Security Group ID>
|
|
41
|
+
Subnets: # Required
|
|
42
|
+
- <VPC Subnet ID>
|
|
43
|
+
InstanceTypes: # Optional
|
|
44
|
+
- <Batch-Supported-Instance-Type> # Default c5.large (cheapest)
|
|
45
|
+
MinvCpus: <Integer> # Optional. Default 0
|
|
46
|
+
MaxvCpus: <Integer> # Optional. Default 2
|
|
47
|
+
Tags: # Optional
|
|
48
|
+
<Key>: <Value> # Default "Name": "AWS Batch Instance - <service>"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
And then define our [AWS Batch Job Definition](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-batch-jobdefinition.html)
|
|
52
|
+
on the function definition
|
|
53
|
+
|
|
54
|
+
```yaml
|
|
55
|
+
functions:
|
|
56
|
+
hello:
|
|
57
|
+
handler: <handler definition>
|
|
58
|
+
batch:
|
|
59
|
+
ContainerProperties:
|
|
60
|
+
Memory: <Integer> # Optional. Default 2048
|
|
61
|
+
Vcpus: <Integer> # Optional. Default 1
|
|
62
|
+
Command: <Command to run in docker> # Optional. Defaults to "<handler> Ref::event"
|
|
63
|
+
JobRoleArn: <ARN> # Optional. Defaults to package.iamRoleStatements
|
|
64
|
+
Environment:
|
|
65
|
+
TEST_ENV_1: Test Value 1
|
|
66
|
+
RetryStrategy:
|
|
67
|
+
Attempts: <Integer> # Optional. Defaults to 1
|
|
68
|
+
Timeout:
|
|
69
|
+
AttemptDurationSeconds: <Integer> # Optional. Defaults to 300
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
And now you should be able to write your batch function like you would any other serverless lambda function:
|
|
73
|
+
|
|
74
|
+
```python3
|
|
75
|
+
import logging
|
|
76
|
+
|
|
77
|
+
# Setup our logger to work both locally and with both AWS CloudWatch
|
|
78
|
+
if len(logging.getLogger().handlers) > 0:
|
|
79
|
+
logging.getLogger().setLevel(logging.INFO)
|
|
80
|
+
else:
|
|
81
|
+
logging.basicConfig(level=logging.INFO)
|
|
82
|
+
logger = logging.getLogger()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def hello(event, context):
|
|
86
|
+
logger.info(f"Hello World: {event}")
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```yaml
|
|
90
|
+
functions:
|
|
91
|
+
hello:
|
|
92
|
+
handler: handler.hello
|
|
93
|
+
events:
|
|
94
|
+
- http:
|
|
95
|
+
path: hello
|
|
96
|
+
method: get
|
|
97
|
+
batch:
|
|
98
|
+
ContainerProperties:
|
|
99
|
+
Memory: 2048
|
|
100
|
+
Vcpus: 1
|
|
101
|
+
RetryStrategy:
|
|
102
|
+
Attempts: 1
|
|
103
|
+
Timeout:
|
|
104
|
+
AttemptDurationSeconds: 3600
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Implementation
|
|
108
|
+
|
|
109
|
+
What happens behind the scenes is that the Serverless Framework's ".zip" artifact gets installed into a
|
|
110
|
+
[lambci/lambda:\<env>](https://hub.docker.com/r/lambci/lambda/) docker image and uploaded to [ECR](https://aws.amazon.com/ecr/).
|
|
111
|
+
|
|
112
|
+
* _Note: Currently using copy of the docker images (https://cloud.docker.com/u/justinram11/repository/list) that unsets the
|
|
113
|
+
ACCESS_KEY_ID and SECRET_ACCESS_KEY environmental variables so that we can use the role attached to the EC2 instance._
|
|
114
|
+
|
|
115
|
+
A "schedule" lambda function is then created with the same name as the regular Serverless Framework's lambda function
|
|
116
|
+
that can be called in to pass the input event to the AWS Batch Job. The "schedule" lambda function can also be subscribed
|
|
117
|
+
to events the same way a regular Serverless Lambda Function is.
|
|
118
|
+
|
|
119
|
+

|
|
120
|
+
|
|
121
|
+
## Example
|
|
122
|
+
|
|
123
|
+
After deploying with `sls deploy`, navigate to the [AWS Lambda Console](https://us-east-2.console.aws.amazon.com/lambda/home?region=us-east-2#/functions/serverless-demo-dev-hello?tab=graph)
|
|
124
|
+
and create a new Test Event with any event value and click the "Test" button.
|
|
125
|
+
|
|
126
|
+
The lambda function will automatically create a new AWS Batch Job which should be visible on the [AWS Batch Console](https://us-east-2.console.aws.amazon.com/batch/home?region=us-east-2#/dashboard)
|
|
127
|
+
|
|
128
|
+
Logs are visible in CloudWatch under the [/aws/batch/job](https://us-east-2.console.aws.amazon.com/cloudwatch/home?region=us-east-2#logStream:group=/aws/batch/job;streamFilter=typeLogStreamPrefix) Log Group
|
|
129
|
+
|
|
130
|
+
`serverless.yml`
|
|
131
|
+
```yaml
|
|
132
|
+
service: serverless-demo
|
|
133
|
+
|
|
134
|
+
provider:
|
|
135
|
+
name: aws
|
|
136
|
+
region: us-east-2
|
|
137
|
+
runtime: python3.7 | nodejs12.x # TODO Select one
|
|
138
|
+
# Creates a SPOT Compute Environment using the VPC defined in our Resources
|
|
139
|
+
batch:
|
|
140
|
+
Type: SPOT
|
|
141
|
+
BidPercentage: 100
|
|
142
|
+
SecurityGroupIds:
|
|
143
|
+
- Ref: AllowAllSecurityGroup
|
|
144
|
+
Subnets:
|
|
145
|
+
- Ref: PublicSubnet
|
|
146
|
+
MinvCpus: 0
|
|
147
|
+
MaxvCpus: 2
|
|
148
|
+
# Allows the Batch Job (code written in handler.py) to list all of our S3 Buckets.
|
|
149
|
+
iamRoleStatements:
|
|
150
|
+
- Effect: "Allow"
|
|
151
|
+
Action:
|
|
152
|
+
- "s3:ListAllMyBuckets"
|
|
153
|
+
Resource: "*"
|
|
154
|
+
|
|
155
|
+
plugins:
|
|
156
|
+
# TODO Required if a python project
|
|
157
|
+
# - serverless-python-requirements
|
|
158
|
+
- serverless-aws-batch
|
|
159
|
+
|
|
160
|
+
package:
|
|
161
|
+
include:
|
|
162
|
+
- handler.py
|
|
163
|
+
exclude:
|
|
164
|
+
- .serverless/**
|
|
165
|
+
- node_modules/**
|
|
166
|
+
|
|
167
|
+
functions:
|
|
168
|
+
hello:
|
|
169
|
+
handler: handler.hello
|
|
170
|
+
events:
|
|
171
|
+
- http:
|
|
172
|
+
path: hello
|
|
173
|
+
method: get
|
|
174
|
+
# Creates a Batch Job with 2GB of memory and 1 vCPU.
|
|
175
|
+
batch:
|
|
176
|
+
ContainerProperties:
|
|
177
|
+
Memory: 2048
|
|
178
|
+
Vcpus: 1
|
|
179
|
+
RetryStrategy:
|
|
180
|
+
Attempts: 1
|
|
181
|
+
Timeout:
|
|
182
|
+
AttemptDurationSeconds: 3600
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
# WARNING: Should not be used in production (allows all traffic)
|
|
186
|
+
# VPC: "Batch VPC"
|
|
187
|
+
# - CIDR: 10.224.0.0/16
|
|
188
|
+
# - Subnet: "Batch <region>a Public Subnet"
|
|
189
|
+
# - CIDR: 10.224.0.0/20
|
|
190
|
+
# - Allows all incoming and outgoing traffic
|
|
191
|
+
# - Security Group: "Batch Allow All"
|
|
192
|
+
# - Allows all incoming and outgoing traffic
|
|
193
|
+
resources:
|
|
194
|
+
Resources:
|
|
195
|
+
|
|
196
|
+
VPC:
|
|
197
|
+
Type: AWS::EC2::VPC
|
|
198
|
+
Properties:
|
|
199
|
+
CidrBlock: 10.224.0.0/16
|
|
200
|
+
EnableDnsSupport: true
|
|
201
|
+
EnableDnsHostnames: true
|
|
202
|
+
Tags:
|
|
203
|
+
- Key: Name
|
|
204
|
+
Value: Batch VPC
|
|
205
|
+
|
|
206
|
+
InternetGateway:
|
|
207
|
+
Type: AWS::EC2::InternetGateway
|
|
208
|
+
Properties:
|
|
209
|
+
Tags:
|
|
210
|
+
- Key: Name
|
|
211
|
+
Value: Batch Internet Gateway
|
|
212
|
+
|
|
213
|
+
InternetGatewayAttachment:
|
|
214
|
+
Type: AWS::EC2::VPCGatewayAttachment
|
|
215
|
+
Properties:
|
|
216
|
+
InternetGatewayId: !Ref InternetGateway
|
|
217
|
+
VpcId: !Ref VPC
|
|
218
|
+
|
|
219
|
+
PublicSubnet:
|
|
220
|
+
Type: AWS::EC2::Subnet
|
|
221
|
+
Properties:
|
|
222
|
+
VpcId: !Ref VPC
|
|
223
|
+
AvailabilityZone: ${self:provider.region}a
|
|
224
|
+
CidrBlock: 10.224.0.0/20
|
|
225
|
+
MapPublicIpOnLaunch: true
|
|
226
|
+
Tags:
|
|
227
|
+
- Key: Name
|
|
228
|
+
Value: Batch ${self:provider.region}a Public Subnet
|
|
229
|
+
|
|
230
|
+
PublicRouteTable:
|
|
231
|
+
Type: AWS::EC2::RouteTable
|
|
232
|
+
Properties:
|
|
233
|
+
VpcId: !Ref VPC
|
|
234
|
+
Tags:
|
|
235
|
+
- Key: Name
|
|
236
|
+
Value: Batch Public Routes
|
|
237
|
+
|
|
238
|
+
DefaultPublicRoute:
|
|
239
|
+
Type: AWS::EC2::Route
|
|
240
|
+
DependsOn: InternetGatewayAttachment
|
|
241
|
+
Properties:
|
|
242
|
+
RouteTableId: !Ref PublicRouteTable
|
|
243
|
+
DestinationCidrBlock: 0.0.0.0/0
|
|
244
|
+
GatewayId: !Ref InternetGateway
|
|
245
|
+
|
|
246
|
+
PublicSubnetRouteTableAssociation:
|
|
247
|
+
Type: AWS::EC2::SubnetRouteTableAssociation
|
|
248
|
+
Properties:
|
|
249
|
+
RouteTableId: !Ref PublicRouteTable
|
|
250
|
+
SubnetId: !Ref PublicSubnet
|
|
251
|
+
|
|
252
|
+
PublicNetworkAcl:
|
|
253
|
+
Type: AWS::EC2::NetworkAcl
|
|
254
|
+
DependsOn: VPC
|
|
255
|
+
Properties:
|
|
256
|
+
VpcId: !Ref VPC
|
|
257
|
+
Tags:
|
|
258
|
+
- Key: Name
|
|
259
|
+
Value: AWS Batch Public ACL
|
|
260
|
+
|
|
261
|
+
InboundPublicNetworkAclAllowAll:
|
|
262
|
+
Type: AWS::EC2::NetworkAclEntry
|
|
263
|
+
Properties:
|
|
264
|
+
NetworkAclId: !Ref PublicNetworkAcl
|
|
265
|
+
RuleNumber: 100
|
|
266
|
+
Protocol: -1
|
|
267
|
+
RuleAction: allow
|
|
268
|
+
Egress: false
|
|
269
|
+
CidrBlock: 0.0.0.0/0
|
|
270
|
+
PortRange:
|
|
271
|
+
From: 0
|
|
272
|
+
To: 65535
|
|
273
|
+
|
|
274
|
+
OutboundPublicNetworkAclAllowAll:
|
|
275
|
+
Type: AWS::EC2::NetworkAclEntry
|
|
276
|
+
Properties:
|
|
277
|
+
NetworkAclId: !Ref PublicNetworkAcl
|
|
278
|
+
RuleNumber: 100
|
|
279
|
+
Protocol: -1
|
|
280
|
+
RuleAction: allow
|
|
281
|
+
Egress: true
|
|
282
|
+
CidrBlock: 0.0.0.0/0
|
|
283
|
+
PortRange:
|
|
284
|
+
From: 0
|
|
285
|
+
To: 65535
|
|
286
|
+
|
|
287
|
+
PublicSubnetNetworkAclAssociation1:
|
|
288
|
+
Type: AWS::EC2::SubnetNetworkAclAssociation
|
|
289
|
+
Properties:
|
|
290
|
+
SubnetId: !Ref PublicSubnet
|
|
291
|
+
NetworkAclId: !Ref PublicNetworkAcl
|
|
292
|
+
|
|
293
|
+
AllowAllSecurityGroup:
|
|
294
|
+
Type: AWS::EC2::SecurityGroup
|
|
295
|
+
Properties:
|
|
296
|
+
GroupName: Batch Allow All
|
|
297
|
+
GroupDescription: "Security group for batch instances that allows all traffic"
|
|
298
|
+
VpcId: !Ref VPC
|
|
299
|
+
SecurityGroupIngress:
|
|
300
|
+
- IpProtocol: "-1"
|
|
301
|
+
CidrIp: 0.0.0.0/0
|
|
302
|
+
SecurityGroupEgress:
|
|
303
|
+
- IpProtocol: "-1"
|
|
304
|
+
CidrIp: 0.0.0.0/0
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### Python
|
|
308
|
+
|
|
309
|
+
`handler.py`
|
|
310
|
+
```python3
|
|
311
|
+
import json
|
|
312
|
+
import time
|
|
313
|
+
import logging
|
|
314
|
+
import boto3
|
|
315
|
+
|
|
316
|
+
# Setup our logger to work with both AWS CloudWatch and locally
|
|
317
|
+
if len(logging.getLogger().handlers) > 0:
|
|
318
|
+
logging.getLogger().setLevel(logging.INFO)
|
|
319
|
+
else:
|
|
320
|
+
logging.basicConfig(level=logging.INFO)
|
|
321
|
+
logger = logging.getLogger()
|
|
322
|
+
|
|
323
|
+
# Setup our boto3 clients
|
|
324
|
+
s3 = boto3.client('s3')
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def hello(event, context):
|
|
328
|
+
logger.info(f"Hello world: {event}")
|
|
329
|
+
|
|
330
|
+
response = s3.list_buckets()
|
|
331
|
+
logger.info(f"S3 Buckets: {response}")
|
|
332
|
+
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
### Node.JS
|
|
336
|
+
|
|
337
|
+
`handler.js`
|
|
338
|
+
```nodejs
|
|
339
|
+
'use strict';
|
|
340
|
+
const process = require("process");
|
|
341
|
+
const AWS = require("aws-sdk");
|
|
342
|
+
const s3 = new AWS.S3();
|
|
343
|
+
|
|
344
|
+
module.exports.hello = (event, context, callback) => {
|
|
345
|
+
|
|
346
|
+
console.log(`Received event: ${JSON.stringify(event)}`);
|
|
347
|
+
|
|
348
|
+
console.log(process.env.AWS_ACCESS_KEY_ID);
|
|
349
|
+
console.log(process.env.AWS_SECRET_ACCESS_KEY);
|
|
350
|
+
|
|
351
|
+
s3.listBuckets({}, function(err, data) {
|
|
352
|
+
console.log(`List buckets data: ${data} err: ${err}`);
|
|
353
|
+
});
|
|
354
|
+
};
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
## Contributors
|
|
358
|
+
|
|
359
|
+
* [simonobe](https://github.com/simonobe)
|
|
360
|
+
* [gcaliene](https://github.com/gcaliene)
|
|
361
|
+
* [MEGApixel23](https://github.com/MEGApixel23)
|
|
362
|
+
* [claydanford](https://github.com/claydanford)
|