-
Notifications
You must be signed in to change notification settings - Fork 319
/
ec2-spot-interruption-notice-cloudwatch-events.yaml
356 lines (353 loc) · 9.21 KB
/
ec2-spot-interruption-notice-cloudwatch-events.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
---
AWSTemplateFormatVersion: 2010-09-09
Description: Example of monitoring for EC2 Spot Instance Interruption Notices in CloudWatch
Events
Mappings:
CidrMappings:
public-subnet-1:
CIDR: 10.0.1.0/24
public-subnet-2:
CIDR: 10.0.2.0/24
vpc:
CIDR: 10.0.0.0/16
Metadata:
Author:
Description: Chad Schmutzer <schmutze@amazon.com>
License:
Description: 'Copyright 2017 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:
awsRegionId:
Description: The AWS Region ID your template was launched in
Value:
Ref: AWS::Region
loadBalancer:
Description: Application load balancer
Value:
Ref: loadBalancer
loadBalancerTargetGroup:
Description: Load balancer target group ARN
Value:
Ref: loadBalancerTargetGroup
publicSubnet1:
Description: Public subnet 1
Value:
Ref: publicSubnet1
publicSubnet2:
Description: Public subnet 2
Value:
Ref: publicSubnet2
snsTopic:
Description: SNS topic ARN
Value:
Ref: snsTopic
spotFleetRole:
Description: Spot Fleet role
Value:
Fn::GetAtt:
- spotFleetRole
- Arn
Resources:
attachGateway:
DependsOn:
- vpc
- internetGateway
Properties:
InternetGatewayId:
Ref: internetGateway
VpcId:
Ref: vpc
Type: AWS::EC2::VPCGatewayAttachment
eventRule:
DependsOn:
- snsTopic
Properties:
Description: Events rule for EC2 Spot Instance Interruption Notices
EventPattern:
detail-type:
- EC2 Spot Instance Interruption Warning
source:
- aws.ec2
State: ENABLED
Targets:
- Arn:
Ref: snsTopic
Id:
Fn::GetAtt:
- snsTopic
- TopicName
- Arn:
Fn::GetAtt:
- lambdaFunction
- Arn
Id:
Ref: lambdaFunction
Type: AWS::Events::Rule
internetGateway:
DependsOn:
- vpc
Type: AWS::EC2::InternetGateway
lambdaFunction:
DependsOn:
- lambdaFunctionRole
Properties:
Code:
ZipFile: "import boto3\ndef handler(event, context):\n instanceId = event['detail']['instance-id']\n\
\ instanceAction = event['detail']['instance-action']\n try:\n ec2client = boto3.client('ec2')\n\
\ describeTags = ec2client.describe_tags(Filters=[{'Name': 'resource-id','Values':[instanceId]},{'Name':'key','Values':['loadBalancerTargetGroup']}])\n\
\ except:\n print(\"No action being taken. Unable to describe tags for instance\
\ id:\", instanceId)\n return\n try:\n elbv2client = boto3.client('elbv2')\n\
\ deregisterTargets = elbv2client.deregister_targets(TargetGroupArn=describeTags['Tags'][0]['Value'],Targets=[{'Id':instanceId}])\n\
\ except:\n print(\"No action being taken. Unable to deregister targets for instance\
\ id:\", instanceId)\n return\n print(\"Spot Instance interruption notice detected.\
\ Detaching instance from target:\")\n print(instanceId, describeTags['Tags'][0]['Value'],\
\ deregisterTargets, sep=\",\")\n return\n"
Handler: index.handler
Role:
Fn::GetAtt:
- lambdaFunctionRole
- Arn
Runtime: python3.6
Type: AWS::Lambda::Function
lambdaFunctionPermission:
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- lambdaFunction
- Arn
Principal: events.amazonaws.com
SourceArn:
Fn::GetAtt:
- eventRule
- Arn
Type: AWS::Lambda::Permission
lambdaFunctionRole:
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Version: 2012-10-17
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Path: /
Policies:
- PolicyDocument:
Statement:
- Action: elasticloadbalancing:DeregisterTargets
Effect: Allow
Resource: '*'
- Action: ec2:DescribeTags
Effect: Allow
Resource: '*'
Version: '2012-10-17'
PolicyName:
Fn::Join:
- '-'
- - Ref: AWS::StackName
- lambdaFunctionRole
Type: AWS::IAM::Role
loadBalancer:
DependsOn:
- vpc
- loadBalancerSecurityGroup
Properties:
Scheme: internet-facing
SecurityGroups:
- Ref: loadBalancerSecurityGroup
- Fn::GetAtt:
- vpc
- DefaultSecurityGroup
Subnets:
- Ref: publicSubnet1
- Ref: publicSubnet2
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
loadBalancerListener:
DependsOn:
- loadBalancer
- loadBalancerTargetGroup
Properties:
DefaultActions:
- TargetGroupArn:
Ref: loadBalancerTargetGroup
Type: forward
LoadBalancerArn:
Ref: loadBalancer
Port: 80
Protocol: HTTP
Type: AWS::ElasticLoadBalancingV2::Listener
loadBalancerSecurityGroup:
DependsOn:
- vpc
Properties:
GroupDescription: Allow http to 0.0.0.0/0
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: tcp
ToPort: 80
VpcId:
Ref: vpc
Type: AWS::EC2::SecurityGroup
loadBalancerTargetGroup:
DependsOn:
- vpc
Properties:
HealthCheckIntervalSeconds: 5
HealthCheckPath: /
HealthCheckTimeoutSeconds: 2
Port: 80
Protocol: HTTP
TargetGroupAttributes:
- Key: deregistration_delay.timeout_seconds
Value: 120
UnhealthyThresholdCount: 2
VpcId:
Ref: vpc
Type: AWS::ElasticLoadBalancingV2::TargetGroup
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::FindInMap:
- CidrMappings
- public-subnet-1
- CIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: Public Subnet 1
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::FindInMap:
- CidrMappings
- public-subnet-2
- CIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: Public Subnet 2
VpcId:
Ref: vpc
Type: AWS::EC2::Subnet
publicSubnet2RouteTableAssociation:
DependsOn:
- publicRouteTable
- publicSubnet2
- attachGateway
Properties:
RouteTableId:
Ref: publicRouteTable
SubnetId:
Ref: publicSubnet2
Type: AWS::EC2::SubnetRouteTableAssociation
snsTopic:
Properties:
DisplayName: SNS Topic for EC2 Spot Instance Interruption Notices
Type: AWS::SNS::Topic
snsTopicPolicy:
DependsOn:
- snsTopic
Properties:
PolicyDocument:
Id:
Fn::GetAtt:
- snsTopic
- TopicName
Statement:
- Action: sns:Publish
Effect: Allow
Principal:
Service:
- events.amazonaws.com
Resource:
Ref: snsTopic
Version: '2012-10-17'
Topics:
- Ref: snsTopic
Type: AWS::SNS::TopicPolicy
spotFleetRole:
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- spotfleet.amazonaws.com
Version: 2012-10-17
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetTaggingRole
Path: /
Type: AWS::IAM::Role
vpc:
Properties:
CidrBlock:
Fn::FindInMap:
- CidrMappings
- vpc
- CIDR
EnableDnsHostnames: true
EnableDnsSupport: true
Type: AWS::EC2::VPC
...