Auto Scaling Lifecycle
- Instances launched through the Auto Scaling group have a different lifecycle than that of other EC2 instances
- Auto Scaling lifecycle starts when the Auto Scaling group launches an instance and puts it into service.
- Auto Scaling lifecycle ends when the instance is terminated either by the user, or the Auto Scaling group takes it out of service and terminates it
- AWS charges for the instances as soon as they are launched, including the time it is not in
Auto Scaling Lifecycle Transition
Auto Scaling Lifecycle Hooks
- Auto Scaling Lifecycle hooks enable performing custom actions by pausing instances as an Auto Scaling group launches or terminates them
- Each Auto Scaling group can have multiple lifecycle hooks. However, there is a limit on the number of hooks per Auto Scaling group
- Auto Scaling scale out event flow
- Instances start in the Pending state
- If an
autoscaling:EC2_INSTANCE_LAUNCHINGlifecycle hook is added, the state is moved to Pending:Wait
- After the lifecycle action is completed, instances enter to Pending:Proceed
- When the instances are fully configured, they are attached to the Auto Scaling group and moved to the InService state
- Auto Scaling scale in event flow
- Instances are detached from the Auto Scaling group and enter the Terminating state.
- If an
autoscaling:EC2_INSTANCE_TERMINATINGlifecycle hook is added, the state is moved to Terminating:Wait
- After the lifecycle action is completed, the instances enter the Terminating:Proceed state.
- When the instances are fully terminated, they enter the Terminated state.
- During the scale out and scale in events, instances are put into a wait state (Pending:Wait or Terminating:Wait) and are paused until either a continue action happens or the timeout period ends.
- By default, the instance remains in a wait state for one hour, which can be extended by restarting the timeout period by recording a heartbeat.
- If the task finishes before the timeout period ends, the lifecycle action can be marked completed and it continues the launch or termination process.
- After the wait period, the Auto Scaling group continues the launch or terminate process (Pending:Proceed or Terminating:Proceed)
- CloudWatch Events target to invoke a Lambda function when a lifecycle action occurs. Event contains information about the instance that is launching or terminating and a token that can be used to control the lifecycle action.
- Notification target (CloudWatch events, SNS, SQS) for the lifecycle hook which receives the message from EC2 Auto Scaling.The message contains information about the instance that is launching or terminating and a token that can be used to control the lifecycle action
- Create a script that runs on the instance as the instance starts. The script can control the lifecycle action using the ID of the instance on which it runs.Custom action can be implemented using
Auto Scaling Lifecycle Hooks Considerations
- Keeping Instances in a Wait State
- Instances remain in a wait state for a finite period of time.
- Default is 1 hour (3600 seconds) with the max being 48 hours or 100 times the heartbeat timeout, whichever is smaller.
- Time can be adjusted using
- complete-lifecycle-action (CompleteLifecycleAction) command to continue to the next state if finishes before the timeout period end
- put-lifecycle-hook command, the –heartbeat-timeout parameter to set the heartbeat timeout for the lifecycle hook during its creation
- Restart the timeout period by recording a heartbeat, using the record-lifecycle-action-heartbeat (RecordLifecycleActionHeartbeat) command
- Cooldowns and Custom Actions
- Cooldown period helps ensure that the Auto Scaling group does not launch or terminate more instances than needed
- Cooldown period starts when the instance enters the InService state. Any suspended scaling actions resume after cooldown period expires
- Health Check Grace Period
- Health check grace period does not start until the lifecycle hook completes and the instance enters the InService state
- Lifecycle Action Result
- Result of the lifecycle hook is either ABANDON or CONTINUE
- If the instance is launching,
- CONTINUE indicates a successful action, and the instance can be put into service.
- ABANDON indicates the custom actions were unsuccessful, and that the instance can be terminated.
- If the instance is terminating,
- ABANDON and CONTINUE allow the instance to terminate.
- However, ABANDON stops any remaining actions from other lifecycle hooks, while CONTINUE allows them to complete
- Spot Instances
- Lifecycle hooks can be used with Spot Instances. However, a lifecycle hook does not prevent an instance from terminating due to a change in the Spot Price, which can happen at any time
Enter and Exit Standby
- Instance in an InService state can be moved toStandby state.
- Standby state enables you to remove the instance from service, troubleshoot or make changes to it, and then put it back into service.
- Instances in a Standby state continue to be managed by the Auto Scaling group. However, they are not an active part of the application until they are put back into service.
AWS Certification Exam Practice Questions
- Questions are collected from Internet and the answers are marked as per my knowledge and understanding (which might differ with yours).
- AWS services are updated everyday and both the answers and questions might be outdated soon, so research accordingly.
- AWS exam questions are not updated to keep up the pace with AWS updates, so even if the underlying feature has changed the question might not be updated
- Open to further feedback, discussion and correction.
- Your application is running on EC2 in an Auto Scaling group. Bootstrapping is taking 20 minutes to complete. You find out that instances are shown as InService although the bootstrapping has not completed. How can you make sure that new instances are not added until the bootstrapping has finished. Choose the correct answer:
- Create a CloudWatch alarm with an SNS topic to send alarms to your DevOps engineer.
- Create a lifecycle hook to keep the instance in pending:wait state until the bootstrapping has finished and then put the instance in pending:proceed state.
- Increase the number of instances in your Auto Scaling group.
- Create a lifecycle hook to keep the instance in standby state until the bootstrapping has finished and then put the instance in pending:proceed state.
- When a scale out event occurs, the Auto Scaling group launches the required number of EC2 instances using its assigned launch configuration. What instance state do these instances start in? Choose the correct answer:
- With AWS Auto Scaling, once we apply a hook and the action is complete or the default wait state timeout runs out, the state changes to what, depending on which hook we have applied and what the instance is doing? Select two. Choose the 2 correct answers:
- For AWS Auto Scaling, what is the first transition state an existing instance enters after leaving steady state in Standby mode?
- Pending (You can put any instance that is in an InService state into a Standby state. This enables you to remove the instance from service, troubleshoot or make changes to it, and then put it back into service. Instances in a Standby state continue to be managed by the Auto Scaling group. However, they are not an active part of your application until you put them back into service. Refer link)
- For AWS Auto Scaling, what is the first transition state an instance enters after leaving steady state when scaling in due to health check failure or decreased load?
- Terminating (When Auto Scaling responds to a scale in event, it terminates one or more instances. These instances are detached from the Auto Scaling group and enter the Terminating state. Refer link)