Atul Bhosale
Atul Bhosale

Reputation: 396

How to trigger/capture an event in nodejs before AWS EC2 instance get terminate

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

Answers (1)

Rodrigo Murillo
Rodrigo Murillo

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?

See Auto Scaling Lifecycle

Upvotes: 1

Related Questions