Lloyd Moore
Lloyd Moore

Reputation: 3197

Ensure the availability of a pool of stopped ec2 instances

I want to maintain a pool of stopped amazon ec2 instances. Whenever the amount is below the threshold, I would like to be able to create new instances and then immediately stop them once they are running. Is this possible within the amazon infrastructure alone?

Upvotes: 0

Views: 132

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 270184

You can certainly create Amazon EC2 instances and then Stop them, making the available to Start later. As you point out, this has the benefit that instances will Start faster than they take to Launch a new instance.

There is no automated method to assist with this. You could have to code a solution that does the following:

  • Monitor the number of Stopped instances
  • If the quantity is below the threshold, launch a new instance
  • The new instance could automatically stop itself via User Data (either via a Shutdown command to the Operating System, or via a StopInstances call to EC2)

Some things you would have to consider:

  • What triggers the monitoring? Would it be on a schedule?
  • The task that launches a new instance would need to wait for the new instance to Launch & Stop before launching any more instances
  • What Starts the instances when they are needed?
  • Do instances ever get Stopped when they are no longer required?

The much better choice would be to use Auto Scaling, with a scale-out alarm based on some metric that says your fleet is busy, and a scale-in alarm to remove instances when the fleet is not busy. The scale-out alarm could be set to launch instances once a threshold is passed (eg 80% CPU) that should allow the new instance(s) to launch before things are 100% busy. The time difference between launching a new instance and starting an existing instance is quite small (at least for Linux).

If you're using Windows, the biggest time delay when launching a new instance is due to Sysprep, which makes a "clean" machine with new Unique IDs. You could cheat by creating an AMI without Sysprep, which would boot faster.

Upvotes: 2

Michael - sqlbot
Michael - sqlbot

Reputation: 179384

Perhaps I am misunderstanding your objective... you can't "ensure availability" of instances without paying for them.

Instances in the stopped state are only logical entities that don't physically exist anywhere -- hardware is allocated on launch, deallocated on stop, reallocated on the next start. In the unlikely condition where an availability zone is exhausted of capacity for given instance class, stopped instances of that class won't start, because there is no hardware available for them to be deployed onto.

To ensure that instances are always available, you have to reserve them, and you have to specify the reservations in a specific availability zone:

Amazon EC2 Reserved Instances provide a significant discount (up to 75%) compared to On-Demand pricing and provide a capacity reservation when used in a specific Availability Zone. [emphasis added]

https://aws.amazon.com/ec2/pricing/reserved-instances/

Under most plans, reserved instances are billed the same rate whether they are running or not, so there would be little point in stopping them.

Upvotes: 0

Related Questions