Reputation: 396
We have nodejs running environment in AWS EC2 with auto scale.
When load is increased/decreased auto scale get triggered. Here we want to trigger/capture an event in nodejs before AWS instance get terminate (because of scale down) to execute some code in nodejs.
Upvotes: 0
Views: 548
Reputation: 13640
You will want to add a Auto Scaling Lifecycle Hook to the autoscaling group. By adding an EC2_INSTANCE_TERMINATING
hook, your instance moves from from the Terminating
state to the Terminating:Wait
state. After you complete the lifecycle action, the instances enter the Terminating:Proceed
state. When the instances are fully terminated, they enter the Terminated
state.
During the Terminating:Wait
state you can run the NodeJS process.
There are different ways to notify the instance that is has entered the Terminating:Wait
state. There is a good discussion of those options here:
How to detect state of aws instance from inside of itself?
Upvotes: 1