forked from asaphe/cf-nested-stack-spot-fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2-spot-fleet.json
More file actions
262 lines (262 loc) · 8.86 KB
/
ec2-spot-fleet.json
File metadata and controls
262 lines (262 loc) · 8.86 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
250
251
252
253
254
255
256
257
258
259
260
261
262
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "EC2 Spot Fleet",
"Mappings": {
"amiRegion": {
"us-east-1": { "AMI": "ami-0dbb717f493016a1a" },
"us-east-2": { "AMI": "ami-0d6808451e868a339" },
"us-west-1": { "AMI": "ami-09f2f73141c83d4fe" },
"us-west-2": { "AMI": "ami-008d8ed4bd7dc2485" }
}
},
"Parameters": {
"spotFleetS3BucketName": {
"Type": "String",
"Description": "AWS S3 Bucket that will be accessed by the Spot Fleet"
},
"spotFleetUserData": {
"Type": "String",
"Description": "executes on the Spot Fleet"
},
"spotFleetKeyPairName": {
"Type": "AWS::EC2::KeyPair::KeyName",
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the EC2 Instances"
},
"spotFleetInstanceType": {
"Type": "String",
"AllowedValues": ["p3.2xlarge", "p2.8xlarge"],
"Description": "EC2 Instance type to use"
},
"spotFleetSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup::Id",
"Description": "Security Group ID"
},
"spotFleetMaximumCapacity": {
"Type": "Number",
"Description": "The maximum number of units to request for the Spot Fleet"
},
"spotFleetTargetCapacity": {
"Type": "Number",
"Description": "The number of units to request for the Spot Fleet"
},
"spotFleetPrice": {
"Type": "Number",
"Description": "The maximum price per unit hour that you are willing to pay for a Spot Instance. The default is the On-Demand price"
},
"spotFleetAllocationStrategy": {
"Type": "String",
"Description": "Indicates how to allocate the target Spot Instance capacity across the Spot Instance pools specified by the Spot Fleet request"
},
"spotFleetSubnets": {
"Type": "String",
"Description": "Subnetwork/s ID"
},
"spotFleetRequestType": {
"Type": "String",
"Description": "One of [instant | maintain | request]"
}
},
"Outputs": {
"spotFleetRoleARN": { "Value": { "Fn::GetAtt": ["spotFleetRole", "Arn"]}},
"spotFleetRequestId": { "Value": { "Ref": "spotFleet" }}
},
"Resources": {
"spotFleetRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetTaggingRole" ],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": ["spotfleet.amazonaws.com"] },
"Action": ["sts:AssumeRole"]
}
]
}
}
},
"spotFleetAutoscaleRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": ["arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetAutoscaleRole"],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{ "Action": ["sts:AssumeRole"],
"Effect": "Allow",
"Principal": { "Service": ["application-autoscaling.amazonaws.com"] }
}
]
}
}
},
"spotFleetInstanceRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [ "sts:AssumeRole" ],
"Effect": "Allow",
"Principal": { "Service": ["ec2.amazonaws.com"] }
}
]
},
"Policies": [
{
"PolicyName": { "Fn::Join": ["-", [ { "Ref": "AWS::StackName" }, "cloudWatchLogsPolicy" ] ] },
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
],
"Effect": "Allow",
"Resource": "arn:aws:logs:*:*:*"
}
]
}
},
{
"PolicyName": { "Fn::Join": ["-", [{ "Ref": "AWS::StackName" }, "s3BucketListPolicy"] ]},
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "s3:ListBucket",
"Effect": "Allow",
"Resource": [{ "Fn::Join": ["", ["arn:aws:s3:::", { "Ref": "spotFleetS3BucketName" }, "/*"] ]}]
}
]
}
},
{
"PolicyName": { "Fn::Join": ["-", [{ "Ref": "AWS::StackName" }, "s3BucketReadWritePolicy"] ]},
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:PutObject*",
"s3:GetObject",
"s3:DeleteObject"
],
"Effect": "Allow",
"Resource": [ { "Fn::Join": ["", ["arn:aws:s3:::", { "Ref": "spotFleetS3BucketName" }, "/*"] ]}]
}
]
}
}
]
}
},
"spotFleetInstanceProfile": {
"DependsOn": ["spotFleetInstanceRole"],
"Type": "AWS::IAM::InstanceProfile",
"Properties": {
"Path": "/",
"Roles": [ { "Ref": "spotFleetInstanceRole" }]
}
},
"spotFleetScalingPolicyDown": {
"Type" : "AWS::ApplicationAutoScaling::ScalingPolicy",
"Properties" : {
"PolicyName": "spotFleetScalingPolicyScaleDown",
"PolicyType": "StepScaling",
"ScalingTargetId": { "Ref": "spotFleetScalableTarget" },
"StepScalingPolicyConfiguration": {
"AdjustmentType": "ChangeInCapacity",
"Cooldown": 300,
"MetricAggregationType": "Average",
"StepAdjustments": [
{
"MetricIntervalUpperBound": 0,
"ScalingAdjustment": -1
}
]
}
}
},
"spotFleetScalingPolicyUp": {
"Type" : "AWS::ApplicationAutoScaling::ScalingPolicy",
"Properties" : {
"PolicyName": "spotFleetScalingPolicyUp",
"PolicyType": "StepScaling",
"ScalingTargetId": { "Ref": "spotFleetScalableTarget" },
"StepScalingPolicyConfiguration": {
"AdjustmentType": "ChangeInCapacity",
"Cooldown": 300,
"MetricAggregationType": "Average",
"StepAdjustments": [
{
"MetricIntervalUpperBound": 0,
"ScalingAdjustment": 1
}
]
}
}
},
"spotFleetScalableTarget": {
"DependsOn": ["spotFleet", "spotFleetAutoscaleRole"],
"Type" : "AWS::ApplicationAutoScaling::ScalableTarget",
"Properties" : {
"MaxCapacity": { "Ref": "spotFleetMaximumCapacity" },
"MinCapacity": { "Ref": "spotFleetTargetCapacity" },
"ResourceId": { "Fn::Join": ["/", ["spot-fleet-request", { "Ref": "spotFleet" }] ]},
"RoleARN": { "Fn::GetAtt": ["spotFleetAutoscaleRole", "Arn"] },
"ScalableDimension": "ec2:spot-fleet-request:TargetCapacity",
"ServiceNamespace": "ec2"
}
},
"spotFleet": {
"Type": "AWS::EC2::SpotFleet",
"Properties": {
"SpotFleetRequestConfigData": {
"IamFleetRole": { "Fn::GetAtt": ["spotFleetRole", "Arn"] },
"AllocationStrategy": { "Ref": "spotFleetAllocationStrategy" },
"SpotPrice": { "Ref": "spotFleetPrice" },
"TargetCapacity": { "Ref": "spotFleetTargetCapacity" },
"TerminateInstancesWithExpiration": true,
"ReplaceUnhealthyInstances": true,
"Type": { "Ref": "spotFleetRequestType" },
"LaunchSpecifications": [
{
"Monitoring": { "Enabled": true },
"ImageId": {
"Fn::FindInMap": [
"amiRegion",
{ "Ref": "AWS::Region" },
"AMI"
]
},
"IamInstanceProfile": {
"Arn": { "Fn::GetAtt": ["spotFleetInstanceProfile", "Arn"] }
},
"InstanceType": { "Ref": "spotFleetInstanceType" },
"KeyName": { "Ref": "spotFleetKeyPairName" },
"SecurityGroups": [{ "GroupId": { "Ref": "spotFleetSecurityGroup" } }],
"SubnetId": { "Ref": "spotFleetSubnets" },
"EbsOptimized": false,
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [ { "Ref": "spotFleetUserData" } ] ] } },
"TagSpecifications": [
{
"ResourceType": "instance",
"Tags": [
{ "Key": "Name", "Value": { "Fn::Join": ["", [ { "Ref": "AWS::StackName" }, "-instance"] ] } }
]
}
]
}
]
}
}
}
}
}