Configuring Notifications for Amazon EC2 Auto Scaling Lifecycle Hooks

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.

IT Certification Category (English)728x90

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

    1. Create your Lambda function and take note of the ARN. To create your Lambda function, see this link
    2. Go to AWS CloudWatch > Events > Rules and click Create rule
    3. Choose the following options:
      1. Event Pattern – since you want this rule to filter AWS events
      2. Service Name: Auto Scaling – to filter from Auto Scaling service
      3. Event type: Instance Launch and Terminate – since the lifecycle hook happens on scale-out and scale-in event
      4. 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”.

Amazon EC2 Auto Scaling lifecycle hooks1

Your rule should be like the screenshot below for the “Instance-terminate Lifecycle Action”.

Amazon EC2 Auto Scaling lifecycle hooks2

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:

Amazon EC2 Auto Scaling lifecycle hooks3

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. 

  1. 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. 
  2. Create an IAM Role that you will associate to the lifecycle hook.
      1. Go to IAM > Role > Create role
      2. Select AWS Service under the Select type of trusted entity.
      3. Click EC2 Auto Scaling from the list under the Choose a use case section.
      4. Choose EC2 Auto Scaling on the Select your use case section.
      5. Click Next: Permissions to the add permission to this role. The AutoScalingServiceRolePolicy should already be added. 
      6. Click Next: Tags to add tags to this role. 
      7. Click Next: Review to add a name to this role
      8. Click Create role.

Amazon EC2 Auto Scaling lifecycle hooks4

3. Get the ARN of this role. Let’s assume the ARN is 
 arn:aws:iam::123456789123:role/aws-service role/

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.



AWS Certifications are consistently among the top paying IT certifications in the world, considering that Amazon Web Services is the leading cloud services platform with almost 50% market share! Earn over $150,000 per year with an AWS certification!

Subscribe to our newsletter and notifications for more helpful AWS cheat sheets and study guides like this and answer as many AWS practice exams as you can.🙂

Pass your AWS Certifications on your First Try with the Tutorials Dojo Portal

Tutorials Dojo portal

Our Bestselling AWS Certified Solutions Architect Associate Practice Exams

AWS Certified Solutions Architect Associate Practice Exams

Enroll Now – Our AWS Practice Exams with 95% Passing Rate

AWS Practice Exams Tutorials Dojo

Tutorials Dojo Study Guide and Cheat Sheets eBooks

Tutorials Dojo Study Guide and Cheat Sheets-2

FREE AWS eBook for Beginners

AWS eBook Tutorials Dojo

FREE AWS Practice Test Samplers

Browse Other Courses

Generic Category (English)300x250

Recent Posts