AWS – Auto Scaling Lifecycle Hooks

Auto Scaling lifecycle hooks enable you to perform custom actions as Auto Scaling launches or terminates instances. For example, you could install or configure software on newly launched instances, or download log files from an instance before it terminates.

  • Create a lifecycle hooks to perform an action on scale out
$ aws autoscaling put-lifecycle-hook —lifecycle-hook-name my-hook --auto-scaling-group-name my-asg --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING
  • Create a lifecycle hooks to perform an action on scale in
$ aws autoscaling put-lifecycle-hook —lifecycle-hook-name my-hook --auto-scaling-group-name my-asg --lifecycle-transition autoscaling:EC2_INSTANCE_TERMINATING
  • If you need more time than 1 hour send a heartbeat to keep the current state of the instance.
$ aws autoscaling record-lifecycle-action-heartbeat --instance-id i-1a2b3c4d5e6f7g --lifecycle-hook-name my-launch-hook --auto-scaling-group-name my-asg
  • Put the instance on InService state
$ aws autoscaling complete-lifecycle-action —lifecycle-action-result CONTINUE —instance-id i-1a2b3c4d5e6f7g —lifecycle-hook-name my-hook —auto-scaling-group-name my-asg
  • Put the instance on terminate state
$ aws autoscaling complete-lifecycle-action —lifecycle-action-result TERMINATE —instance-id i-1a2b3c4d5e6f7g —lifecycle-hook-name my-hook --auto-scaling-group-name my-asg
You can read more about Auto Scaling Lifecycle Hooks here.