-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbatch-example.yml
More file actions
249 lines (248 loc) · 7.35 KB
/
batch-example.yml
File metadata and controls
249 lines (248 loc) · 7.35 KB
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
AWSTemplateFormatVersion: '2010-09-09'
Description: Creates a POC AWS Batch environment, includes IAM resources, S3, Lambda, and Batch.
Parameters:
Release:
Type: String
Description: 'Release Identifier'
Default: 'develop'
CloudToolsBucket:
Type: String
Description: 'S3 Bucket where CFN and Lambda archives are stored.'
Environment:
Type: String
Description: 'Environment Name'
Default: Dev
QueueName:
Type: String
Description: 'Name of Queue to be created'
Default: MyQueue
VPCCidr:
Type: String
Description: 'Cidr Block of the VPC, allows for ssh access internally.'
Default: '10.0.0.0/8'
MinLength: "9"
MaxLength: "18"
AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
ConstraintDescription: "Must be valid CIDR notation (i.e. x.x.x.x/x)."
VPC:
Type: AWS::EC2::VPC::Id
Description: 'VPC ID to boot compute into'
Subnets:
Type: List<AWS::EC2::Subnet::Id>
Description: 'List of Subnets to boot into'
AMI:
Description: ECS Optimized AMI ID
Type: AWS::SSM::Parameter::Value<String>
Default: /aws/service/ecs/optimized-ami/amazon-linux/recommended/image_id
Ec2KeyPair:
Type: AWS::EC2::KeyPair::KeyName
Description: 'Ec2KeyPair to use for Compute Environment'
StartImageName:
Type: String
Description: 'Name and tag of Start Container Image'
Default: 'batchpocstart:latest'
ProcessImageName:
Type: String
Description: 'Name and tag of Process Container Image'
Default: 'batchpocprocess:latest'
Resources:
LambdaTriggerIAMRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: 'Allow'
Principal:
Service:
- 'lambda.amazonaws.com'
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
Policies:
-
PolicyName: 'SubmitBatch'
PolicyDocument:
Version: '2012-10-17'
Statement:
-
Effect: 'Allow'
Action:
- 'batch:SubmitJob'
Resource: '*'
BatchContainerIAMRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: 'Allow'
Principal:
Service:
- 'ecs-tasks.amazonaws.com'
Action:
- 'sts:AssumeRole'
Policies:
-
PolicyName: 'SubmitBatch'
PolicyDocument:
Version: '2012-10-17'
Statement:
-
Effect: 'Allow'
Action:
- 'batch:SubmitJob'
Resource: '*'
BatchServiceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: 'Allow'
Principal:
Service:
- 'batch.amazonaws.com'
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole'
BatchInstanceIAMRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: 'Allow'
Principal:
Service:
- 'ec2.amazonaws.com'
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role'
BatchInstanceProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Path: /
Roles:
- !Ref BatchInstanceIAMRole
InstanceProfileName: !Join [ "", [ "BatchIAM", !Ref Environment ] ]
TriggerFunction:
Type: 'AWS::Lambda::Function'
Properties:
Code:
S3Bucket: !Ref CloudToolsBucket
S3Key: !Join [ "/", [ !Ref Release, "lambda/trigger.zip" ] ]
Description: "Triggers AWS Batch when invoked"
Environment:
Variables:
JobQueue: !Ref QueueName
JobDefinition: Start
FunctionName: !Join ["",["BatchTrigger",!Ref Environment]]
Handler: lambda_function.lambda_handler
MemorySize: 128
Role: !GetAtt [ LambdaTriggerIAMRole, Arn ]
Runtime: python3.6
Timeout: 60
S3LambdaEvent:
Type: 'AWS::Lambda::Permission'
Properties:
FunctionName: !GetAtt [ TriggerFunction, Arn ]
SourceArn: !Join [ '', [ 'arn:aws:s3:::', "batch-poc-", !Ref 'AWS::AccountId','-', !Ref 'AWS::Region']]
Action: 'lambda:InvokeFunction'
Principal: s3.amazonaws.com
SourceAccount: !Ref 'AWS::AccountId'
TriggeredBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: !Join [ "-", [ "batch-poc", !Ref 'AWS::AccountId', !Ref 'AWS::Region' ] ]
NotificationConfiguration:
LambdaConfigurations:
- Event: s3:ObjectCreated:*
Function: !GetAtt [ TriggerFunction, Arn ]
DependsOn: S3LambdaEvent
BatchSecGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: BatchGroup
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
IpProtocol: '-1'
SecurityGroupIngress:
- CidrIp: !Ref VPCCidr
IpProtocol: tcp
FromPort: '22'
ToPort: '22'
VpcId: !Ref VPC
BatchCompute:
Type: "AWS::Batch::ComputeEnvironment"
Properties:
Type: Managed
ServiceRole: !Ref BatchServiceRole
ComputeEnvironmentName: !Join [ "", ["BatchPOC-", !Ref Environment]]
ComputeResources:
MaxvCpus: 24
SecurityGroupIds:
- !Ref BatchSecGroup
Subnets: !Ref Subnets
Type: Ec2
MinvCpus: 0
ImageId: !Ref AMI
InstanceRole: !Ref BatchInstanceProfile
InstanceTypes:
- optimal
Ec2KeyPair: !Ref Ec2KeyPair
Tags: { "Name": "Batch POC" }
DesiredvCpus: 0
State: ENABLED
Queue:
Type: "AWS::Batch::JobQueue"
Properties:
ComputeEnvironmentOrder:
- ComputeEnvironment: !Join [ "", ["BatchPOC-", !Ref Environment]]
Order: 1
Priority: 1
State: "ENABLED"
JobQueueName: !Ref QueueName
DependsOn: BatchCompute
StartJob:
Type: "AWS::Batch::JobDefinition"
Properties:
Type: Container
ContainerProperties:
Command:
- schedule
- -r
- !Ref 'AWS::Region'
- -q
- !Ref QueueName
- -j
- Process
Memory: 128
JobRoleArn: !Ref BatchContainerIAMRole
Vcpus: 1
Image: !Join ['', [!Ref 'AWS::AccountId','.dkr.ecr.', !Ref 'AWS::Region', '.amazonaws.com/', !Ref StartImageName ] ]
JobDefinitionName: Start
RetryStrategy:
Attempts: 1
ProcessJob:
Type: "AWS::Batch::JobDefinition"
Properties:
Type: Container
ContainerProperties:
Command:
- echo
- "hello world"
Memory: 128
JobRoleArn: !Ref BatchContainerIAMRole
Vcpus: 1
Image: !Join ['', [!Ref 'AWS::AccountId','.dkr.ecr.', !Ref 'AWS::Region', '.amazonaws.com/', !Ref ProcessImageName ] ]
JobDefinitionName: Process
RetryStrategy:
Attempts: 4