All posts

Moving Builds to EC2 Spot Instances

My Web Application “MyGarage” is hosted in an EC2 instance, running as a service. Build is happening on an On Demand EC2 instance using Amazon EC2 Instance plugin.

I read about Amazon Spot Instances and biggest advantage is their cost savings. And farnkly would’nt want that?

And the best part is, enabling them is just a matter of checking this checkbox in the EC2 Plugin configuration

Screenshot 1 from "Moving Builds to EC2 Spot Instances"

So, now instead of a regular On Demand EC2 instance, a Spot Instance will be created and used as a Jenkins slave. There is no difference, except the pricing.

However there is one catch, since the Spot instances are based on bidding, if anyone bids higher than the set bid price, our spot instance will be terminated. So its a good idea to check the current spot price by clicking “Check Current Spot Price” next to set bid price and mention max spot price that we can bid.

Screenshot 2 from "Moving Builds to EC2 Spot Instances"

Another thing to do is if we need the instance for a certain time, we can mention that in the “Spot Block Reservation Duration” ex: 1 (i.e 1 Hour). So, even if spot price changes our instance will remain active.

If we do not want instance to be active for entire spot block time, we can still control the termination based on the Idle timeout. When the job is done instance will be terminated after idle time out even if spot block reservation duraion is set.

Lastly, if we do not get Spot Instance for wahteever reasons, we can enable “Fallback to on-demand instances” so that our job would still spawn an On-Demand EC2 instance and complete the execution.

So all in all, a great cost savings option to use as jenkins slaves. After figuring this out, all my builds are now using spot insatnces and its been great.

First published on my old blog, Write Code. Be Agile. Go DevOps.