Lifecycle Hook During Instance Launch Demo
Start course
1h 27m

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

Setting up a lifecycle hook for your auto scaling group is pretty straightforward. First, let me show you the current setup that we have and then we'll modify it to include a lifecycle hook. We'll go to EC2. As you can see, I already have two instances running here. We'll go all the way down to all the scaling groups and select this one right here that I have for this demonstration, the ASG hooks demo. I got a instance management, so you can see I already have two in service EC2 here, but no lifecycle hooks at the moment. Let's go ahead and create one. Create a lifecycle hook. Let's call it, I'm going to call it loadingProgram, and it's going to be at launch. Now, I expect this to need no more than five minutes, I'll type in 300 seconds. This is a worst-case scenario if you will. Now, right here, I'm gonna select continue. What this means is after 300 seconds, we're going to assume that the application is ready to go, so it will put the instance in service. I'll click 'Create' and we get confirmation that the lifecycle hook has been created. Now, how do we test this? I'll go to the details again, I'll click 'Edit', and I already told you I have two instances running. Let's go ahead and deploy a third one. This value is 3, I'll click update. Now, if we wait, we should be able to see a new instance coming online. Go to the EC2 Dashboard, select instances running, and let's see what happens. Sure enough, we see number three here initializing. Let's go back to the Auto Scaling Group. I will go to instance management, and sure enough we have the third one, but is in a Pending Wait state. And because we set this to 300 seconds, it's just going to sit there for 300 seconds. What if we finish,

let's say, in three minutes, 180 seconds? How do we notify the auto scaling group that we're done and this instance is ready to go to in service? Let me show you. Click on the instance here, it will bring me back to the EC2 console. I'm going to click 'Connect'. As you can see, I have Session Manager enabled in this case. So, let's go ahead and take advantage of this. Let me go ahead and paste a command in here. This command goes ahead and collects the metadata, specifically the instance-id. Let me show you echo instanceid. We're going to need the instanceid to tell the auto scaling group that this instance is ready to go. Now, we needed another command. Let me paste that over here. Alright, here we go, aws auto scaling complete-lifecycle hook. And what we're trying to do here is notify that the lifecycle-hook called loadingProgram that belongs to this auto scaling group ASG_Hooks_Demo, we want to send the instance-id and the action-result to be CONTINUE, meaning that we're done here.

Let's go ahead and hit 'Enter'. We don't get a response and that's a good thing. If we go back to the auto scaling group, we should be able to see this go from Pending Wait now to InService. Let's go ahead and refresh, and sure enough now our instance is in service. As you can see we didn't have to wait the full five minutes, and now the instance is ready to go. So, if you're wondering what would be a use case for this, I can immediately think of WebLogic, WebSphere, and JBoss. All three are Java-based application servers. It takes considerable time, sometimes minutes to load a Java virtual environment, then the application server itself, and finally, the actual application you're trying to run. So, instead of trying to estimate how long it will take to load, just set the lifecycle hook to a reasonable maximum and add a complete action command to your application launch verification script. I'm sure there are many more use cases, but those mostly will follow what we just described here.

About the Author
Carlos Rivas
Sr. AWS Content Creator
Learning Paths

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