I have to take certain actions during AWS autoscaling scale-in event.The ec2 instance should be able to save some logs and reports to S3 bucket. This can take anywhere between 5 to 15 mins.
I already have a script that gets called on termination:
ln -s /etc/ec2-termination /etc/rc0.d/S01ec2-termination
However the script ends abruptly within 5 mins. I am looking at leveraging AWS LifeCycle hooks to extend the EC2 lifetime. The documentation is not clear on invoking a script in a way similar to user-data script.
There are ways of using AWS lambda or SNS to receive notification. This can be potentially used to inform the ec2.
But, I would like to know if there is a simpler solution to this problem. Is there a way to register a script with Lifecycle hooks that gets called on a scale-in event.
Instance termination After you terminate an instance, it remains visible in the console for a short while, and then the entry is automatically deleted. You cannot delete the terminated instance entry yourself.
The ENI (Elastic Network Interface) is detached – x An ENI (Elastic Network Interface) is never detached when an instance is Stopped. The underlying host for the instance is changed -/ – In most cases, the instance is migrated to a new underlying host computer when it's started.
No.
The fact that the instance is being terminated is managed within the AWS infrastructure. Auto Scaling does not have the ability to reach "into" the EC2 instance to trigger anything.
Instead, you would need to write some code on the instance that checks whether the instance is in the termination state and then takes appropriate action.
An example might be:
(Be careful that the script doesn't trigger again during the shutdown process otherwise it might try performing the shutdown process every 15 seconds!)
Alternatively, store the shutdown information in the Systems Manager Parameter Store or a database, but using Tags seems nicely scalable!
Updated version:
Thanks to raevilman for the idea:
Much simpler!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With