@@ -1138,6 +1138,7 @@ Resources:
1138
1138
Resource :
1139
1139
- !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/${BootHookAutomation}:$DEFAULT
1140
1140
- !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
1141
1142
- Effect : Allow
1142
1143
Action : iam:PassRole
1143
1144
Resource : !GetAtt AutomationRole.Arn
@@ -1341,3 +1342,45 @@ Resources:
1341
1342
InstanceId : " {{ InstanceId }}"
1342
1343
LifecycleActionResult : " CONTINUE"
1343
1344
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