Skip to content

Commit 8ae7f15

Browse files
committed
Add a spot interruption rule and automation that terminates the instance
1 parent 1352c0d commit 8ae7f15

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

templates/aws-stack.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ Resources:
11381138
Resource:
11391139
- !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/${BootHookAutomation}:$DEFAULT
11401140
- !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/${ShutdownHookAutomation}:$DEFAULT
1141+
- !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/${SpotInteruptionAutomation}:$DEFAULT
11411142
- Effect: Allow
11421143
Action: iam:PassRole
11431144
Resource: !GetAtt AutomationRole.Arn
@@ -1341,3 +1342,45 @@ Resources:
13411342
InstanceId: "{{ InstanceId }}"
13421343
LifecycleActionResult: "CONTINUE"
13431344
LifecycleHookName: "{{ LifecycleHook }}"
1345+
1346+
SpotInterruptionRule:
1347+
Type: AWS::Events::Rule
1348+
Properties:
1349+
Description: !Sub Run the spot interruption AWS SSM Automation for ${AgentAutoScaleGroup}
1350+
EventPattern:
1351+
source:
1352+
- aws.ec2
1353+
detail-type:
1354+
- EC2 Spot Instance Interruption Warning
1355+
Targets:
1356+
- Arn: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/${SpotInteruptionAutomation}:$DEFAULT
1357+
RoleArn: !GetAtt EventBridgeRuleRole.Arn
1358+
Id: TargetSsmAutomation
1359+
InputTransformer:
1360+
InputPathsMap:
1361+
instanceid: "$.detail.instance-id"
1362+
InputTemplate: "{\"InstanceId\":[<instanceid>]}"
1363+
1364+
SpotInteruptionAutomation:
1365+
Type: AWS::SSM::Document
1366+
Properties:
1367+
DocumentType: Automation
1368+
Content:
1369+
schemaVersion: "0.3"
1370+
assumeRole: !GetAtt AutomationRole.Arn
1371+
description: Terminate the instance in the Auto Scaling group, making it go through the shutdown hook.
1372+
parameters:
1373+
InstanceId:
1374+
type: String
1375+
AutoScalingGroupName:
1376+
type: String
1377+
default: !Ref AgentAutoScaleGroup
1378+
mainSteps:
1379+
- name: TerminateInstanceInAutoScalingGroup
1380+
action: aws:executeAwsApi
1381+
inputs:
1382+
Service: autoscaling
1383+
Api: TerminateInstanceInAutoScalingGroup
1384+
AutoScalingGroupName: "{{ AutoScalingGroupName }}"
1385+
InstanceId: "{{ InstanceId }}"
1386+
ShouldDecrementDesiredCapacity: false

0 commit comments

Comments
 (0)