Skip to content

Commit aa1deb9

Browse files
committed
Add EventBridge rules to route ASG lifecycle events to the SSM automations
1 parent 42d6860 commit aa1deb9

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

templates/aws-stack.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,29 @@ Resources:
11191119
ScaleOutForWaitingJobs: !Ref ScaleOutForWaitingJobs
11201120
DisableScaleIn: "false"
11211121

1122+
EventBridgeRuleRole:
1123+
Type: AWS::IAM::Role
1124+
Properties:
1125+
AssumeRolePolicyDocument:
1126+
Statement:
1127+
- Effect: Allow
1128+
Principal:
1129+
Service: events.amazonaws.com
1130+
Action: sts:AssumeRole
1131+
Policies:
1132+
- PolicyName: StartSsmAutomation
1133+
PolicyDocument:
1134+
Version: '2012-10-17'
1135+
Statement:
1136+
- Effect: Allow
1137+
Action: ssm:StartAutomationExecution
1138+
Resource:
1139+
- !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/${BootHookAutomation}:$DEFAULT
1140+
- !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/${ShutdownHookAutomation}:$DEFAULT
1141+
- Effect: Allow
1142+
Action: iam:PassRole
1143+
Resource: !GetAtt AutomationRole.Arn
1144+
11221145
AutomationRole:
11231146
Type: AWS::IAM::Role
11241147
Properties:
@@ -1157,6 +1180,26 @@ Resources:
11571180
# them
11581181
HeartbeatTimeout: 300
11591182

1183+
BootHookRule:
1184+
Type: AWS::Events::Rule
1185+
Properties:
1186+
Description: !Sub Run the boot time AWS SSM Automation for ${BootHook}
1187+
EventPattern:
1188+
source:
1189+
- aws.autoscaling
1190+
detail-type:
1191+
- "EC2 Instance-launch Lifecycle Action"
1192+
detail:
1193+
AutoScalingGroupName: !Ref AgentAutoScaleGroup
1194+
Targets:
1195+
- Arn: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/${BootHookAutomation}:$DEFAULT
1196+
RoleArn: !GetAtt EventBridgeRuleRole.Arn
1197+
Id: TargetSsmAutomation
1198+
InputTransformer:
1199+
InputPathsMap:
1200+
instanceid: "$.detail.EC2InstanceId"
1201+
InputTemplate: "{\"InstanceId\":[<instanceid>]}"
1202+
11601203
BootHookAutomation:
11611204
Type: AWS::SSM::Document
11621205
Properties:
@@ -1205,6 +1248,26 @@ Resources:
12051248
HeartbeatTimeout: 3600
12061249
DefaultResult: CONTINUE
12071250

1251+
ShutdownHookRule:
1252+
Type: AWS::Events::Rule
1253+
Properties:
1254+
Description: !Sub Run the shutdown time AWS SSM Automation for ${ShutdownHook}
1255+
EventPattern:
1256+
source:
1257+
- aws.autoscaling
1258+
detail-type:
1259+
- "EC2 Instance-terminate Lifecycle Action"
1260+
detail:
1261+
AutoScalingGroupName: !Ref AgentAutoScaleGroup
1262+
Targets:
1263+
- Arn: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:automation-definition/${BootHookAutomation}:$DEFAULT
1264+
RoleArn: !GetAtt EventBridgeRuleRole.Arn
1265+
Id: TargetSsmAutomation
1266+
InputTransformer:
1267+
InputPathsMap:
1268+
instanceid: "$.detail.EC2InstanceId"
1269+
InputTemplate: "{\"InstanceId\":[<instanceid>]}"
1270+
12081271
ShutdownHookAutomation:
12091272
Type: AWS::SSM::Document
12101273
Properties:

0 commit comments

Comments
 (0)