ASG isntances on ECS cluster graceful shutdown example.
Follow this reference link
-
Create Lifecycle hooks on ASG
- Lifecycle transition:
instance terminate
- Heartbeat timeout:
600
(You want change) - Default result:
CONTINUE
(You want change) - Notification metadata:
{ "clusterName": "ecsCluster" }
- Lifecycle transition:
-
Create EventBridge Rules
- Build event pattern
- Event source:
AWS events or EventBridge partner events
- Event pattern
- Event source:
AWS services
- AWS service:
Auto Scaling
- Event type:
Instance Launch and Terminate
, - Specific instance event(s):
EC2 Instance-launch Lifecycle Action
- (Optional) Choose specific group name
- Event source:
- Event source:
- custom event pattern
{ "source": ["aws.autoscaling"], "detail-type": ["EC2 Instance-terminate Lifecycle Action"], "detail": { "AutoScalingGroupName": ["ecs-cluster"] } }
- Build event pattern
-
Create iam role & policy used by Lambda
- attach
AWSLambdaBasicExecutionRole
to Role - attach below policy to Role
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "autoscaling:CompleteLifecycleAction" ], "Resource": [ "arn:aws:autoscaling:${region}:${accountId}:autoScalingGroup:*:autoScalingGroupName/*" ] }, { "Effect": "Allow", "Action": [ "ecs:ListContainerInstances" ], "Resource": [ "arn:aws:ecs:${region}:${accountId}:cluster/*" ] }, { "Effect": "Allow", "Action": [ "ecs:UpdateContainerInstancesState" ], "Resource": [ "arn:aws:ecs:${region}:${accountId}:container-instance/*/*" ] } ] }
- attach
-
Create using Lambda
- create lambda role above
-
Add ecs instance attributes for containerInstance filter, add userdata ASG launch template
echo ECS_INSTANCE_ATTRIBUTES={\"instanceId\":\"$(curl -s http://169.254.169.254/latest/meta-data/instance-id)\"} >> /etc/ecs/ecs.config