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.
Here is a solution using Lifecycle Hooks, Automation and Run Command, based on this article:
The permissions required to deploy these are
There might be more; these are the ones I had to add to our existing deployment policy. They may also not all be required, but I was fed up redeploying and adding them piecemeal so I added some of the Rule ones as an educated guess.