Back to results for “Lambda & Serverless”
AWS › Auto Scaling Groups

How do lifecycle hooks in Auto Scaling Groups let you customize instance launch and termination behavior?

Advanced

Answer

Lifecycle hooks let you pause an instance in a wait state during either the launch or termination process, giving you time to perform custom actions, such as running configuration scripts before an instance becomes fully active, or gracefully draining active connections and backing up logs before an instance is terminated, before the Auto Scaling Group is allowed to proceed with completing that lifecycle transition.
aws autoscaling put-lifecycle-hook --lifecycle-hook-name my-hook --auto-scaling-group-name my-asg --lifecycle-transition autoscaling:EC2_INSTANCE_TERMINATING --heartbeat-timeout 300
Real-world example A logging service uses a termination lifecycle hook to ensure that any pending log files on an instance are safely uploaded to S3 before that instance is fully terminated by the Auto Scaling Group, preventing any log data loss.

Common follow-ups: What happens if a lifecycle hook's custom action never completes?;How do lifecycle hooks integrate with Lambda or SNS notifications?