-
Notifications
You must be signed in to change notification settings - Fork 319
/
sqs-ec2-spot-asg.yaml
493 lines (486 loc) · 12 KB
/
sqs-ec2-spot-asg.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
---
AWSTemplateFormatVersion: 2010-09-09
Description: Example of EC2 Auto Scaling group using EC2 Spot Instances, automatically scaling
based on SQS queue depth
Mappings:
CidrMappings:
vpc:
CIDR: 10.0.0.0/16
amazonLinuxAmi:
ap-northeast-1:
AMI: ami-0c3fd0f5d33134a76
ap-northeast-2:
AMI: ami-095ca789e0549777d
ap-northeast-3:
AMI: ami-0ee933a7f81beb045
ap-south-1:
AMI: ami-0d2692b6acea72ee6
ap-southeast-1:
AMI: ami-01f7527546b557442
ap-southeast-2:
AMI: ami-0dc96254d5535925f
ca-central-1:
AMI: ami-0d4ae09ec9361d8ac
eu-central-1:
AMI: ami-0cc293023f983ed53
eu-north-1:
AMI: ami-3f36be41
eu-west-1:
AMI: ami-0bbc25e23a7640b9b
eu-west-2:
AMI: ami-0d8e27447ec2c8410
eu-west-3:
AMI: ami-0adcddd3324248c4c
sa-east-1:
AMI: ami-058943e7d9b9cabfb
us-east-1:
AMI: ami-0b898040803850657
us-east-2:
AMI: ami-0d8f6eb4f641ef691
us-west-1:
AMI: ami-056ee704806822732
us-west-2:
AMI: ami-082b5a644766e0e6f
Metadata:
Author:
Description: Ran Sheinberg <ranshein@amazon.com>, Chad Schmutzer <schmutze@amazon.com>
License:
Description: 'Copyright 2019 Amazon.com, Inc. and its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under the License.'
Outputs:
autoScalingGroup:
Description: The autoScalingGroup
Value:
Ref: autoScalingGroup
awsRegionId:
Description: The AWS Region ID your template was launched in
Value:
Ref: AWS::Region
instanceProfile:
Description: Instance profile ARN
Value:
Fn::GetAtt:
- instanceProfile
- Arn
publicSubnet1:
Description: Public subnet 1
Value:
Ref: publicSubnet1
publicSubnet2:
Description: Public subnet 2
Value:
Ref: publicSubnet2
s3BucketId:
Description: The S3 bucket ID
Value:
Ref: s3Bucket
sqsQueueArn:
Description: The SQS queue ARN
Value:
Fn::GetAtt:
- sqsQueue
- Arn
vpc:
Description: The VPC
Value:
Ref: vpc
Parameters:
desiredCapacity:
Default: 2
Description: Desired capacity
Type: Number
maxSize:
Default: 12
Description: Maximum capacity
Type: Number
minSize:
Default: 2
Description: Mininum capacity
Type: Number
Resources:
attachGateway:
DependsOn:
- vpc
- internetGateway
Properties:
InternetGatewayId:
Ref: internetGateway
VpcId:
Ref: vpc
Type: AWS::EC2::VPCGatewayAttachment
autoScalingGroup:
DependsOn:
- launchTemplate
- publicSubnet1
- publicSubnet2
- autoScalingServiceLinkedRole
Properties:
DesiredCapacity:
Ref: desiredCapacity
HealthCheckType: EC2
MaxSize:
Ref: maxSize
MinSize:
Ref: minSize
MixedInstancesPolicy:
InstancesDistribution:
OnDemandBaseCapacity: 0
OnDemandPercentageAboveBaseCapacity: 0
SpotAllocationStrategy: capacity-optimized
LaunchTemplate:
LaunchTemplateSpecification:
LaunchTemplateId:
Ref: launchTemplate
Version: 1
Overrides:
- InstanceType: t3.large
- InstanceType: c4.large
- InstanceType: c5.large
- InstanceType: m4.large
- InstanceType: m5.large
- InstanceType: r4.large
- InstanceType: r5.large
VPCZoneIdentifier:
- Ref: publicSubnet1
- Ref: publicSubnet2
Type: AWS::AutoScaling::AutoScalingGroup
autoScalingPolicy:
DependsOn:
- autoScalingGroup
- sqsQueue
Properties:
AutoScalingGroupName:
Ref: autoScalingGroup
PolicyType: TargetTrackingScaling
TargetTrackingConfiguration:
CustomizedMetricSpecification:
Dimensions:
- Name: QueueName
Value:
Fn::GetAtt:
- sqsQueue
- QueueName
MetricName: ApproximateNumberOfMessagesVisible
Namespace: AWS/SQS
Statistic: Sum
Unit: Count
TargetValue: 2
Type: AWS::AutoScaling::ScalingPolicy
autoScalingServiceLinkedRole:
Properties:
AWSServiceName: autoscaling.amazonaws.com
Description: Default Service-Linked Role enables access to AWS Services and Resources
used or managed by Auto Scaling
Type: AWS::IAM::ServiceLinkedRole
cloudWatchLogsGroup:
Properties:
RetentionInDays: 7
Type: AWS::Logs::LogGroup
ec2FleetServiceLinkedRole:
Properties:
AWSServiceName: ec2fleet.amazonaws.com
Description: Default EC2 Fleet Service Linked Role
Type: AWS::IAM::ServiceLinkedRole
instanceProfile:
DependsOn:
- instanceRole
Properties:
Path: /
Roles:
- Ref: instanceRole
Type: AWS::IAM::InstanceProfile
instanceRole:
DependsOn:
- sqsQueue
- s3Bucket
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Version: 2012-10-17
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM
Path: /
Policies:
- PolicyDocument:
Statement:
- Action: sqs:*
Effect: Allow
Resource:
Fn::GetAtt:
- sqsQueue
- Arn
Version: 2012-10-17
PolicyName:
Fn::Join:
- '-'
- - Ref: AWS::StackName
- sqsQueuePolicy
- PolicyDocument:
Statement:
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- logs:DescribeLogStreams
Effect: Allow
Resource: arn:aws:logs:*:*:*
Version: 2012-10-17
PolicyName:
Fn::Join:
- '-'
- - Ref: AWS::StackName
- cloudWatchLogsPolicy
- PolicyDocument:
Statement:
- Action: s3:ListBucket
Effect: Allow
Resource:
- Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: s3Bucket
Version: '2012-10-17'
PolicyName:
Fn::Join:
- '-'
- - Ref: AWS::StackName
- s3BucketListPolicy
- PolicyDocument:
Statement:
- Action: ec2:DescribeTags
Effect: Allow
Resource: '*'
Version: '2012-10-17'
PolicyName:
Fn::Join:
- '-'
- - Ref: AWS::StackName
- ec2DescribeTagsPolicy
- PolicyDocument:
Statement:
- Action:
- s3:PutObject*
- s3:GetObject
- s3:DeleteObject
Effect: Allow
Resource:
- Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: s3Bucket
- /*
Version: '2012-10-17'
PolicyName:
Fn::Join:
- '-'
- - Ref: AWS::StackName
- s3BucketReadWritePolicy
- PolicyDocument:
Statement:
- Action:
- autoscaling:SetInstanceProtection
- autoscaling:SetInstanceHealth
Effect: Allow
Resource: arn:aws:autoscaling:*:*:autoScalingGroup:*:autoScalingGroupName/*
Version: '2012-10-17'
PolicyName:
Fn::Join:
- '-'
- - Ref: AWS::StackName
- autoScalingGroupPolicy
Type: AWS::IAM::Role
internetGateway:
DependsOn:
- vpc
Type: AWS::EC2::InternetGateway
launchTemplate:
DependsOn:
- instanceProfile
- s3Bucket
- sqsQueue
- cloudWatchLogsGroup
Properties:
LaunchTemplateData:
IamInstanceProfile:
Arn:
Fn::GetAtt:
- instanceProfile
- Arn
ImageId:
Fn::FindInMap:
- amazonLinuxAmi
- Ref: AWS::Region
- AMI
TagSpecifications:
- ResourceType: instance
Tags:
- Key: Name
Value:
Ref: AWS::StackName
UserData:
Fn::Base64:
Fn::Sub: '#!/bin/bash -xe
yum -y install git
cd /root && git clone https://github.com/awslabs/ec2-spot-labs.git
REGION=${AWS::Region} S3BUCKET=${s3Bucket} SQSQUEUE=${sqsQueue} CLOUDWATCHLOGSGROUP=${cloudWatchLogsGroup}
bash /root/ec2-spot-labs/sqs-ec2-spot-asg/user-data.sh
'
Type: AWS::EC2::LaunchTemplate
publicRoute:
DependsOn:
- publicRouteTable
- internetGateway
- attachGateway
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Ref: internetGateway
RouteTableId:
Ref: publicRouteTable
Type: AWS::EC2::Route
publicRouteTable:
DependsOn:
- vpc
- attachGateway
Properties:
Tags:
- Key: Name
Value: Public Route Table
VpcId:
Ref: vpc
Type: AWS::EC2::RouteTable
publicSubnet1:
DependsOn:
- attachGateway
Properties:
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs:
Ref: AWS::Region
CidrBlock:
Fn::Select:
- 0
- Fn::Cidr:
- Fn::FindInMap:
- CidrMappings
- vpc
- CIDR
- 256
- 8
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value:
Ref: AWS::StackName
VpcId:
Ref: vpc
Type: AWS::EC2::Subnet
publicSubnet1RouteTableAssociation:
DependsOn:
- publicRouteTable
- publicSubnet1
- attachGateway
Properties:
RouteTableId:
Ref: publicRouteTable
SubnetId:
Ref: publicSubnet1
Type: AWS::EC2::SubnetRouteTableAssociation
publicSubnet2:
DependsOn:
- attachGateway
Properties:
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs:
Ref: AWS::Region
CidrBlock:
Fn::Select:
- 1
- Fn::Cidr:
- Fn::FindInMap:
- CidrMappings
- vpc
- CIDR
- 256
- 8
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value:
Ref: AWS::StackName
VpcId:
Ref: vpc
Type: AWS::EC2::Subnet
publicSubnet2RouteTableAssociation:
DependsOn:
- publicRouteTable
- publicSubnet2
- attachGateway
Properties:
RouteTableId:
Ref: publicRouteTable
SubnetId:
Ref: publicSubnet2
Type: AWS::EC2::SubnetRouteTableAssociation
s3Bucket:
DependsOn:
- sqsQueue
- sqsQueuePolicy
Properties:
NotificationConfiguration:
QueueConfigurations:
- Event: s3:ObjectCreated:*
Queue:
Fn::GetAtt:
- sqsQueue
- Arn
Type: AWS::S3::Bucket
sqsQueue:
Properties:
VisibilityTimeout: 900
Type: AWS::SQS::Queue
sqsQueuePolicy:
DependsOn:
- sqsQueue
Properties:
PolicyDocument:
Statement:
- Action:
- sqs:SendMessage
Effect: Allow
Principal:
Service: s3.amazonaws.com
Resource: '*'
Version: 2012-10-17
Queues:
- Ref: sqsQueue
Type: AWS::SQS::QueuePolicy
vpc:
Properties:
CidrBlock:
Fn::FindInMap:
- CidrMappings
- vpc
- CIDR
EnableDnsHostnames: true
EnableDnsSupport: true
Tags:
- Key: Name
Value:
Ref: AWS::StackName
Type: AWS::EC2::VPC
...