To fix the problem of EC2 instance termination affecting our ECS services, we now need to create an EC2 Auto Scaling life cycle hook that will notify us that an EC2 instance is about to be terminated. Referring back to the first diagram, this requires several resources:
- The actual life cycle hook
- A life cycle hook role that grants the EC2 Auto Scaling group permission to publish life cycle hook notifications to an SNS topic
- An SNS topic where life cycle hooks can be published and subscribed to
The following example demonstrates creating the life cycle hook, life cycle hook role, and SNS topic:
...
...
Resources:
...
...
LifecycleHook:
Type: AWS::AutoScaling::LifecycleHook
Properties:
RoleARN: !Sub ${LifecycleHookRole.Arn}
AutoScalingGroupName: !Ref ApplicationAutoscaling
DefaultResult: CONTINUE
HeartbeatTimeout...