1. Home
  2. Training Library
  3. Designing Compute solutions in AWS - Level 3

Brief overview of auto scaling and lifecycle of an EC2 instances created by ASG

Written by Carlos Rivas

Auto Scaling Groups are a tool to manage supply and demand of infrastructure, in this case EC2 instances that  your application can leverage to match the expectation of your users.

When more compute units are required based on a certain metric, such as CPU Usage, number of web visitors or total number of orders in queue you can make an informed decision of whether to scale your auto scaling group in or out.

Scale In means that you will eliminate unused instances and Scale Out means that you will add additional instances to keep up with demand.

When an instance is launched during a Scale Out event it will start with a State of ‘Pending’. This allows time to run any bootstrapping scripts that you may have specified in your User Data section of the Launch Template.

Once this is complete, the instance will immediately go into ‘In Service’ state.

On the other hand, when an instance is terminated by a Scale In event or because it has failed to respond to Health checks , it will be moved to a state called ‘Terminating’ until it’s actually terminated. 

There are two problems here.

Number one:  When launching an instance, say, for example, you are running a JBoss java application server. Just because your User Data script completed a “Server Start” command does not mean that your application is ready to receive requests. You may need additional time to initialize custom resources, process configuration files, connect to back-end databases and more. All this can take many seconds, sometimes minutes to complete.

During this time your Auto Scaling Group already switched your instance to ‘In Service’ status and because of this, it may already be getting pings from a health-check coming from a Load Balancer, for example.  These checks will likely fail because your application is still loading.

We need a way to delay telling our Auto Scaling group that our instance that just launched is not ready yet and will need additional time to prepare to receive requests.

Problem number two:  One of your instances has been targeted for termination during a Scale In event, however, your instance is still in the middle of processing a request and in addition, it has valuable logs that can be used to troubleshoot potential problems and improve performance in the future. Both the in-progress request and the logs will be lost if the instance is suddenly terminated.

We need a way to hold the termination process long enough for us to gracefully shutdown our service and move our logs to permanent storage, such as Amazon S3 and only then allow the instance to be terminated.

Lifecycle hooks can help solve both of these problems.

An ‘Instance Launching’ Lifecycle hook prevents the instance from becoming ‘in service’ and instead puts it in ‘Pending:Wait’ state, during this event you can ensure that the application loads and gets to a proper stable state. Once the life cycle hook ends, the instance will go into ‘Pending:Proceed’ state and the ASG will take over once again and attempt to move it to ‘In Service’

In a similar fashion, an ‘instance terminating’ lifecycle hook will put your instance in ‘Terminating:Wait’ state, during which you can do your final clean up tasks as needed, such as preserving copies of logs by moving them to S3, for example. And once you are done or the preset timer expires, the instance will switch to ‘Terminating:Proceed’ state and the ASG will take over and terminate the instance.

1h 20m

This course covers the core learning objective to meet the requirements of the 'Designing Compute solutions in AWS - Level 3' skill

Learning Objectives:

  • Evaluate and enforce secure communications when using AWS elastic load balancers using HTTPS/SSL listeners
  • Evaluate when to use a serverless architecture compared to Amazon EC2 based upon workload performance optimization
  • Evaluate how to implement fully managed container orchestration services to deploy, manage, and scale containerized applications
About the Author
Carlos Rivas
Sr. AWS Content Creator

Software Development has been my craft for over 2 decades. In recent years, I was introduced to the world of "Infrastructure as Code" and Cloud Computing.
I loved it! -- it re-sparked my interest in staying on the cutting edge of technology.

Colleagues regard me as a mentor and leader in my areas of expertise and also as the person to call when production servers crash and we need the App back online quickly.

My primary skills are:
★ Software Development ( Java, PHP, Python and others )
★ Cloud Computing Design and Implementation
★ DevOps: Continuous Delivery and Integration