You use CloudFormation to launch an Auto Scaling group.
In the CloudFormation stack, you create a lifecycle hook in the ASG that puts a new instance in the Wait state while the custom script of the user data is running.
The script installs the web application, initializes customer data and performs a health check of the instance.
After the health check has passed, how would you put the instance back to the InService state in the Auto Scaling group?
Click on the arrows to vote for the correct answer
A. B. C. D.Correct Answer - B.
Option A is incorrect because the record-lifecycle-action-heartbeat command is used to restart the timeout period and keep the instance in the Wait state.
It cannot complete the lifecycle period for an instance in the Pending:Wait state.
Option B is CORRECT because in the script, the complete-lifecycle-action command should be used to finish the lifecycle action before the timeout period ends.
The following is an example:
"aws autoscaling complete-lifecycle-action --lifecycle-hook-name my-lifecycle-hook --auto-scaling-group-name my-auto-scaling-group --lifecycle-action-result CONTINUE --lifecycle-action-token bcd2f1b8-9a78-44d3-8a7a-4dd07d7cf635"
Option C is incorrect because the put-lifecycle-hook CLI command creates or updates a lifecycle hook for a specified Auto Scaling group.
It is unsuitable to be used in this scenario.
Option D is incorrect because the AWS CLI command attach-instances attaches EC2 instances to the ASG.
It cannot finish the custom actions in a lifecycle hook.
Reference:
https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html, https://docs.aws.amazon.com/cli/latest/reference/autoscaling/complete-lifecycle-action.html.Sure, I'd be happy to provide a detailed explanation of the correct answer.
The correct answer is B: In the script, use the AWS CLI command complete-lifecycle-action to finish the lifecycle hook customer actions.
Let's break down why this is the correct answer and why the other options are incorrect:
Option A: Use the record-lifecycle-action-heartbeat CLI command or the RecordLifecycleActionHeartbeat operation in the script. The record-lifecycle-action-heartbeat
command is used to signal to Amazon EC2 Auto Scaling that your instance is still in the process of completing a lifecycle action (such as a custom script) and to extend the timeout period specified when you created the lifecycle hook. This command is useful if the instance is still running the script and you need to avoid the instance from timing out, but it doesn't complete the lifecycle action or move the instance out of the Wait state. Therefore, Option A is not the correct answer.
Option C: Use the AWS CLI command put-lifecycle-hook to complete the lifecycle actions and put the instance back to the normal process of the Auto Scaling group. The put-lifecycle-hook
command is used to create a lifecycle hook, update a lifecycle hook, or delete a lifecycle hook for an Auto Scaling group. This command is not used to complete the lifecycle action and move the instance out of the Wait state. Therefore, Option C is not the correct answer.
Option D: In the script, use the AWS CLI command attach-instances to attach the instance to the ASG, which completes the lifecycle action and modifies the instance state from Wait to InService. The attach-instances
command is used to attach one or more EC2 instances to an Auto Scaling group. This command doesn't complete the lifecycle action or move the instance out of the Wait state. Therefore, Option D is not the correct answer.
Option B: In the script, use the AWS CLI command complete-lifecycle-action to finish the lifecycle hook customer actions. The complete-lifecycle-action
command is used to complete the lifecycle action and move the instance out of the Wait state. When a lifecycle hook is in the Wait state, you can complete the lifecycle action using this command. The command takes the name of the Auto Scaling group, the instance ID, the lifecycle action token, and specifies whether the instance should continue to run (with CONTINUE) or be terminated (with ABANDON). In this case, since the instance passed the health check and the web application is installed and initialized, we want to continue the instance and move it back to the InService state. Therefore, Option B is the correct answer.
To summarize, when using CloudFormation to launch an Auto Scaling group with a lifecycle hook that puts the instance in the Wait state while a custom script is running, the correct way to move the instance back to the InService state after the script has completed is to use the complete-lifecycle-action
command in the script.