Configuring Notifications for Amazon EC2 Auto Scaling Lifecycle Hooks

Home » AWS Cheat Sheets » AWS Compute Services » Compute Related Notes » Configuring Notifications for Amazon EC2 Auto Scaling Lifecycle Hooks

Configuring Notifications for Amazon EC2 Auto Scaling Lifecycle Hooks

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

    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”
Tutorials dojo strip

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/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

Tutorials Dojo portal

Be Inspired and Mentored with Cloud Career Journeys!

Tutorials Dojo portal

Enroll Now – Our Azure Certification Exam Reviewers

azure reviewers tutorials dojo

Enroll Now – Our Google Cloud Certification Exam Reviewers

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

FREE AWS Exam Readiness Digital Courses

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

FREE Intro to Cloud Computing for Beginners

FREE AWS, Azure, GCP Practice Test Samplers

Recent Posts

Written by: Kenneth Samonte

Kenneth is an AWS Architect & Linux System Administrator. He's a Red Hat Certified System Administrator, AWS Certified Solutions Architect Professional, and a VMware Certified Professional. He's also a registered Electronics Engineer and Cisco Certified Network Associate (CCNA). He enjoys exploring cloud platforms and administering Linux systems. When he's not busy, you’ll find him online playing League of Legends.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?