Reuse EBS volume with AWS autoscaling

amazon ec2amazon-ebsamazon-web-services

I am trying to setup auto-healing for my EC2 instance. So whenever my instance goes down or is unreachable, it should auto reboot or launch a new instance preferrably in a different AZ in the same AWS region.

I looked into setting up cloudwatch alarm and adding action to reboot the instance in case of failure. This works well if I want my EC2 instance to just reboot in case of failure.

But it doesn't cover the case where AZ itself is not reachable. So I am trying to setup AWS autoscaling group with min/max instance number as 1.
In case of failure I want new instance to be launched with EBS volume of terminated instance. How do I go ahead with this?

Or if there is an alternate way to achieve what I want, that would be appreciated too.

Best Answer

So I run a lot of single servers in ASGs for auto-healing.

What I did was give the EBS volume a set tag.

On boot there's a cloud-init script that uses the aws meta data api and CLI and gets its own instance ID via the API and then searches for a EBS volume with a set tag in its own AZ. Once it has the volume ID it forces a detach to be on the safe side. Then attaches it to itself.

So far its worked pretty well.