Ref: https://learn.cantrill.io/courses/1820301/lectures/41301445
EC2 ASG - Lifecycle Hooks
Diagram: https://github.com/acantril/aws-sa-associate-saac03/blob/main/1500-HA_and_SCALING/00_LEARNINGAIDS/ASG-LifecycleHooks-1.png
- Normal EC2 instance launch/termination behavior in ASGs:
- Launch: instance moves to
Pending
state and then to InService
state
- Termination: instance moves to
Terminating
state and then to Terminated
state
- ❗ Without ASG lifecycle hooks, processes automatically carried out by the ASG, and NOTHING can be done during those transitions
- 🔧 ASG lifecycle hooks allow taking custom actions on instances during ASG actions
- …i.e. instance launch/termination transitions
- ❗ With lifecycle hooks in place, instances are PAUSED within the launch/termination flow
- Flow resumes when one of these 2 happen:
- A (configurable) timeout expires → then either continue or abandon the ASG action
- Default timeout is 3600s (one hour)
CompleteLifecycleAction
operation is executed
- Execute once you have performed whatever activity you wanted to perform
- Alternatively, execute
complete-lifecycle-action
command (e.g. in AWS CLI)
- Examples of custom actions to take:
- Load/index data before bringing a new instance to service
- Backup data or logs, or tidy up an instance before terminating it
- Can be integrated with Amazon EventBridge & SNS Notifications
- Allows for event-driven processing based on launch/termination of ASG instances