EC2 instance retry until it starts
With issues launching AWS instances, I am trying to find a way to retry every 10 minutes for my instance to restart. I was looking into Systems Manager, but that does not seem to have a retry. Is there another way to make AWS check the status of an instance, if it has not started, then retry again?
Posted Dec 1, 2021, 11:21 PM UTC
28
An auto scaling group with a desired capacity of 1 should accomplish this for you.
I do not want the instance always on, but when I try to start it I want it to retry until it starts. But if this is the way I will try it.
Thanks for the suggestion.
So a pattern I use- Auto scaling group defaulted to 0. A job comes in and I want to process something, let’s say on a cron schedule. A lambda fires off of a cloud watch event, which sets the ASG to 1. The instance comes up, does what it needs to do, and at the end, it sets its own ASG back to 0. The ASG terminates the instance then. That way it’s self healing and keeps trying until success or timeout, but also only runs as needed.
A more modern way to do this would be with containers, maybe fargate, but ec2 works fine this way too!
Just change the ASG from 0 to 1 when you are ready.
Will this work for an existing instance? I do not want to create a new instance. I want to start an existing instance, but am getting a lot of Insufficient Capacity issues, so want to retry until it starts. When I try to create an Auto Scaling group, it asks me to define my AMI etc, so I assume it is trying to create a new instance.
Yeah it would be a new instance
Many many things in the cloud will likely be less painful for you if you are able to design systems that do not depend on specific, long-lived instances. Is the setup of this instance automated? What is your motivation for keeping the instance around when you are not using it?
If you want to persist data when the instance is down, either keep it on EFS and attach on boot, or write a lambda lifecycle hook to detach/reattach an EBS volume on launch/termination.