0
votes

I have an autoscaling group created by CloudFormation. When a scale out or scale in event occurs, I have configured an SNS topic to trigger a lambda function. Everything works as expected, except when I delete my CloudFormation stack.

When I delete my CloudFormation stack (I use short-lived stacks for integration testing), the autoscaling group is deleted and the instances enter the Terminating:Wait stage as expected. But, the autoscaling:EC2_INSTANCE_TERMINATING lifecycle hook is never called (neither the Lambda monitoring nor the CloudWatch logs show any evidence of the lifecycle hook getting called). The autoscaling group appears to wait for the heartbeat timeout to expire, then deletes the instances and the autoscaling group.

Is there a way I can have the autoscaling:EC2_INSTANCE_TERMINATING lifecycle hook called when the EC2 instances are terminated because the ASG is deleted?

1

1 Answers

0
votes

I figured this out. In my case I had a AWS::Lambda::Permission resource which granted SNS permission to invoke my lambda function. The permission was being deleted before the Autoscaling group so the SNS topic did not have permission to invoke my lambda function when the message arrived at the SNS topic.

Adding a DependsOn attribute to my ASG so it depends on the permission object solved this.