An EC2 instance is billed at an hourly rate depending on the instance type and the region by default. This pricing model is called on-demand. It is the simplest but also most expensive way of using EC2 instances. There are two other purchasing options for EC2 instances: reserved instances and spot instances. Reserved instances offer a discount on the hourly rate if you commit yourself paying for an instance for 1 or 3 years. Spot instances allow you to bid for unused capacity and save up to 90% of EC2 costs. Read on to learn about spot instances and explore three different ways to use them.

Spot Instances

Infrastructure providers like AWS are running at large scale. This also implies spare capacity at scale. Infrastructure providers try to avoid spare capacity and usage fluctuations. This is why electric power providers offer cheaper rates during the nights and why AWS offers the ability to bid on unused computing capacity.

A spot market is a market where standardized products are traded for immediate delivery. The price of the products on the market depends on supply and demand. On the AWS spot market, the traded products are EC2 instances, and they’re delivered by starting an EC2 instance.

The following figure shows the price chart for an EC2 instance. If the current spot price is lower than your maximum price, your spot request will be fulfilled, and an EC2 instance will start. If the current spot price exceeds your bid, your virtual machine will be terminated (not stopped) automatically after two minutes.

There are many different spot markets available. A spot market is defined by:

Instance type (e.g. m3.medium)

Region (e.g. eu-west-1)

Availability Zone (e.g. eu-west-1a)

Each spot market is offering a separate current price. So when using spot instances it is an advantage to be able to use different instance types, in different availability zones or even regions, as this allows you to pick the lowest price available.

1. Defined-Duration Workloads

A spot instance is terminated automatically if the spot price exceeds your bid. This may sound scary at first. If you need EC2 instances for defined-duration workloads less than 6 hours there is a simple solution for that.

It is possible to bid on a spot block. A spot block defines a duration between 1 and 6 hours. An EC2 instances launched by a spot block will run continuously without interruption until it reaches the end of the defined time block. Pricing depends on the requested spot block: typically 30% to 45% less than on-demand pricing. So you are paying an extra charge on the spot price for a continuously running EC2 instance.

Creating a spot block request is simple. Executing the following command with the help of the AWS CLI will request a spot block for 1 instance running 1 hours with a maximum price of USD 0.15 per hour.

$ aws ec2 request-spot-instances \ --block-duration-minutes 60 \ --instance-count 1 \ --spot-price "0.15" ...

Whenever you are using EC2 instances for tasks running less than 6 hours using spot blocks is a very easy and reliable way of saving EC2 costs.