QubOps

Day 20 - Spot Instances

AWS Cost Optimisation Advent Calendar 2024

5 min read time

#aws #ec2 #emr #elasticsearch #eks #redshift

Welcome to day 20 of the unofficial AWS Cost Optimisation Advent Calendar 2024, where every day we will be sharing new tips or tricks to help you optimise your cloud costs before Christmas 2024.

Today's topic we have touched on throughout this series but today we will go into more detail.

The topic is Spot Instances.

Spot Instances are spare capacity that AWS makes available at a reduced cost.

It is a win-win where customers can save money and AWS can make sure they aren't wasting capacity sitting idle.

Spot Instances are ideal for non-critical workloads that can be interrupted and restarted or even in production workloads as part of an auto-scaling strategy.

When to use Spot Instances

Spot Instances are ideal for workloads that are not time-sensitive and can be interrupted. The should be able pick up where they left off when they come back online.

Some examples of these types of workloads that are ideal for Spot Instances are:

  • Batch processing that is not time sensitive
  • Data analysis
  • Machine learning
  • Low priority queues

Another fairly common use case is to use Spot Instances as part of an auto-scaling strategy.

Many auto-scaling groups are designed to have instances shut down and come up again when load increases so this is often a good fit for Spot Instances.

How to use Spot Instances

Some use cases

You can launch a Spot Instance in a similar way to an On-Demand Instance however this only ever useful for one off tasks as your instance will eventually get terminated automatically.

To use Spot Instances in a more practical way you can use an Auto Scaling Group with a mix of On-Demand and Spot Instances.

AWS will automatically manage the Spot Instances for you and replace them if they are terminated.

This works by setting an ideal ratio of On-Demand to Spot Instances and AWS will try and fill your auto-scaling group with the cheapest instances available.

If no Spot Instances are available then AWS will launch On-Demand Instances so there is no risk of being left without capacity because of this strategy.

This is configured when you create the Instance Purchase Options section as below:

Spot Configuration

Another way to use Spot Instances is with Elastic Container Service (ECS) where you can use Spot Instances as part of your ECS cluster.

These are available for both EC2 and Fargate tasks.

For Fargate this is configured by setting both regular 'Fargate' and 'Fargate Spot' as Capacity Providers and then you can set a ratio in favour of 'Fargate Spot'.

For EC2 this is done the same way as regular EC2 auto-scaling groups.

Handling Interruptions

When using Spot Instances you need to be prepared for the instance to be terminated. AWS provides you with a 2-minute warning before the instance will shut down.

You can monitor for this warning by using the Instance Metadata.

More specifically by checking the following endpoint:

curl http://169.254.169.254/latest/meta-data/spot/instance-action

From here you can start to gracefully shutdown your services.

For example, when stopping a web server you will want to stop processing new requests and just finish the ones you are currently handling.

This is typically done with a cron on the instance, however the strategy will vary based on the AWS service and the type of workload you are running.

Other supported services

Spot Instances can also be used in:

  1. Amazon Elastic MapReduce (EMR): EMR is used for big data processing and analysis. You can use Spot Instances to supplement your EMR cluster. It can help you save costs on processing large datasets.

  2. Amazon Elasticsearch: Amazon Elasticsearch Service domains can leverage Spot Instances for data nodes to gain cost-efficiency.

  3. AWS Batch: AWS Batch plans, schedules, and executes your batch computing workloads across the full range of AWS compute services, including Spot Instances.

  4. EC2 Fleet and Spot Fleet: Spot Instances can be included as part of an EC2 Fleet or Spot Fleet request. These services attempt to launch the number of Spot Instances needed to meet the target capacity specified in the request.

  5. AWS Glue: Amazon Glue can use Spot Instances to perform ETL (Extract, Transform, Load) jobs.

  6. Amazon SageMaker: You can use Spot Instances for training machine learning models with Amazon SageMaker, to reduce the cost of training these models.

  7. Kubernetes (EKS/AWS KOPS/AWS Managed Node Groups): You can use Spot Instances for your Amazon EKS worker nodes.

  8. Amazon GameLift: Spot Instances can be used for hosting multiplayer games to save costs.

  9. Amazon Redshift: Spot Instances can be used with Amazon Redshift to meet your analytics needs.

Conclusion

Spot Instances can provide significant cost savings for non-critical workloads or as part of a scaling strategy.

With the correct configuration this can also come with minimal risk of any service interruption.

If you need help introducing Spot Instances into your cost saving strategy please don't hesitate to contact us for a free consultation.

To be the first to know when the next edition is published, follow us on LinkedIn, X, BlueSky or subscribe to the RSS feed.

Join our newsletter for Cost Optimization tips and tricks

By subscribing you agree to our Privacy Policy and provide consent to receive updates from our company.