image
Lifecycle Hook During Instance Termination Demo
Start course
Difficulty
Advanced
Duration
1h 27m
Students
64
Ratings
3/5
Description

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
Transcript

The architecture for a termination lifecycle hook is slightly different. Why? Because it's a bit tricky for the instance itself to know that it's being shut down. So, it's probably better to manage this from the outside to keep things simple. Let me show you what that will look like. I'm going to go down to Auto Scaling group. I have a Spot Instance Auto Scaling group here. Let's go to instance management. And as you can see, I don't have any lifecycle hooks here. We're going to try and create one. SavingData, and it's going to be for termination. We want to hold this, let's say, again for 300 seconds. And we wanted to continue at the end of those 300 seconds. Now, here's the interesting part. There's a link here that says: Learn how to receive lifecycle hooks notification. If we click on that, we'll get a link to the documentation and there's this note right here. By default, when you add a lifecycle hook in the console, Amazon EC2 Auto Scaling sends lifecycle events notification to Amazon EventBridge. Keep that in mind. Using EventBridge or a user data script is a recommended best practice. So, we're trying to keep things simple.

So, we're going to use EventBridge as a recommended best practice in this case. So, I'm going to click 'Create.' So, what happens is, let's say that we try to terminate an instance, it's going to go into this scenario here. In fact, let's go ahead and try that right now. Go back to Details. I'll click 'Edit' and set this to 2, meaning that we should be eliminating one of our instances. Let's see here. As we can see, we have an instance in Terminating:Wait state, and it will continue to be there for another five minutes or so. So, the question is, how do we trigger an event, let's say, a Lambda function or a SSM, a run document that we can use to preserve the data in the instance before it actually terminates. Let's go to EventBridge. We'll go to View rules and Create rule. So, ideally we would have a run document that we can send with SSM or a Lambda function, and we would call it with this information. It will be hook for termination for example. I'm going to leave everything as is and click 'Next.' And now, we can type lifecycle.

And there is an action here that says EC2 Instance-terminate Lifecycle Action. We get a sample of what that's going to look like. And it's going to include the InstanceId. This is very important because then we can use this information, like I said, to go to SSM and fire a run command that can send a document, perhaps to preserve data to S3 for example. Really all you need is the InstanceId in this case. You'll also need the InstanceId in case you are done with your action, and then you want to notify the Auto Scaling group that is okay to continue the instance termination. And with the setup, you can be sure that your data will persist after instance termination.

 

About the Author
Avatar
Carlos Rivas
Sr. AWS Content Creator
Students
836
Courses
15
Learning Paths
1

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