Packer has the ability to auto-bid on spot instance prices for AWS EC2 but there are two things that you need to understand that may discourage you from using that feature.
First problem is related to a race condition between when
Packer checks the price and decides for how much to bid and starting the instance. What can happen (and it’s happened to me more than once) is that in that time the price will go up! In the result, if you don’t set a build timeout,
Packer will hang forever, or if you use the timeout, you’ll waste the time for unsuccessful build.
The second issue is comes from the nature of the spot instances – if the current spot price is higher that your max bid price, the instance will be terminated within 120 seconds. You can get that information from
http://169.254.169.254/latest/meta-data/spot/termination-time and script it so you’ll rerun the job from your CI/CD system or similar.
The easiest remedy for both issues is dead simple – bid high enough. With current spot instance prices being usually a fraction of a price of a regular EC2, you can safely bid even 50% more. I usually do this for m3 and m4 instances and I don’t have those problems any more!