Last updated on June 3, 2023
Auto Scaling Lifecycle Hooks
In the previous post, we talked about Auto Scaling Lifecycle Hooks and how to configure it on your Auto Scaling group. You can view the article here. It’s better to read it first before proceeding with this section.
In a nutshell, Auto Scaling Lifecycle Hooks allow you to put an instance in the “wait” state while the Auto Scaling group responds to a scale-out or scale-in event. While the instance is in the “wait” state, you perform actions such as installing the application before allowing traffic to the instance or sending out the application logs to S3 before terminating the instance.
You can also configure notifications when the instance enters the wait state. In this post, we’ll show you how to configure the notification for the lifecycle hooks, which you can route to a Lambda function to perform a custom action or to receive an email notification from SNS so that you can perform a custom action.
Configuring Notifications for Lifecycle Hooks
When a lifecycle hook occurs on an Auto Scaling group, it sends event logs to AWS CloudWatch Events, which in turn can be used to set up a rule and target to invoke a Lambda function.
The following step assumes that you have configured your Auto Scaling Lifecycle hook on the AWS Console. If you don’t know how to do that yet, refer to our previous post.
Route Notifications to Lambda using CloudWatch Events
-
- Create your Lambda function and take note of the ARN. To create your Lambda function, see this link.
- Go to AWS CloudWatch > Events > Rules and click Create rule.
- Choose the following options:
- Event Pattern – since you want this rule to filter AWS events
- Service Name: Auto Scaling – to filter from Auto Scaling service
- Event type: Instance Launch and Terminate – since the lifecycle hook happens on scale-out and scale-in event
- Specific Instance events – Select this and you can choose whether you want this rule to trigger for the “Instance-launch Lifecycle Action” or the “Instance-terminate Lifecycle Action”
Your rule should be like the screenshot below for the “Instance-launch Lifecycle Action”.
Your rule should be like the screenshot below for the “Instance-terminate Lifecycle Action”.
4. Click on “Add target” on the right side of the page to add a target for this Rule.
5. Select “Lambda function” as target and select your Lambda function on the “Function” field. You can also add other targets here if you need to. Here’s a screenshot for reference:
6. Click “Configure details” to proceed to the next step.
7. Add a name to your rule and add a description. You want to make sure the “State Enabled” is checked. Click Create rule to finally create your CloudWatch Events rule.
That’s it, the CloudWatch permission to trigger the Lambda function is automatically taken care of. Now, when the Auto Scaling group scales-out or scales-in with a lifecycle hook, the Lambda function is triggered.
Receive Notification using Amazon SNS
To receive lifecycle hook notifications with Amazon SNS, you need to use the AWS CLI to add a lifecycle hook. Configuring the notification on the AWS Console is not supported at this time. The key point here is that you need an SNS topic and an IAM role to allow publishing to that topic.
- Create your SNS topic. Let’s assume the SNS topic ARN is arn:aws:sns:ap-northeast-1:1234457689123:test-topic. Make sure that your email is subscribed to this topic.
- Create an IAM Role that you will associate to the lifecycle hook.
-
- Go to IAM > Role > Create role
- Select AWS Service under the Select type of trusted entity.
- Click EC2 Auto Scaling from the list under the Choose a use case section.
- Choose EC2 Auto Scaling on the Select your use case section.
- Click Next: Permissions to the add permission to this role. The AutoScalingServiceRolePolicy should already be added.
- Click Next: Tags to add tags to this role.
- Click Next: Review to add a name to this role
- Click Create role.
-
3. Get the ARN of this role. Let’s assume the ARN is
arn:aws:iam::123456789123:role/aws-service role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling_test
4. Now we need to add a lifecycle hook and a notification to your Auto Scaling group. Change the values inside the brackets for the correct values.
For the scale-out action lifecycle hook, use the following put-lifecycle-hook command.
aws autoscaling put-lifecycle-hook --lifecycle-hook-name [lifecycle hook name] --auto-scaling-group-name [auto scaling group name] --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING --notification-target-arn [put sns topic arn here] --role-arn [put iam role arn here]
For the scale-in action lifecycle hook, use the following put-lifecycle-hook command.
aws autoscaling put-lifecycle-hook --lifecycle-hook-name [lifecycle hook name] --auto-scaling-group-name [auto scaling group name] --lifecycle-transition autoscaling:EC2_INSTANCE_TERMINATING --notification-target-arn [put sns topic arn here] --role-arn [put iam role arn here]
Once configured, the SNS topic receives a test notification with the following key-value pair:
"Event": "autoscaling:TEST_NOTIFICATION"
That’s it! Your Auto Scaling lifecycle hook is configured with an SNS notification that will send out an email to you once the scale-out or scale-in event lifecycle hook puts the instance on the “wait” state.
Sources:
https://docs.aws.amazon.com/autoscaling/ec2/userguide/configuring-lifecycle-hook-notifications.html
https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html#adding-lifecycle-hooks-aws-cli
https://docs.aws.amazon.com/autoscaling/ec2/userguide/cloud-watch-events.html#create-lambda-function