AWS EC2 Spot Instance - Best price options for EC2

AWS EC2 Spot Instance - Best price options for EC2

What is EC2 Spot Instance?

EC2 Spot instances come from the unused or excess capacity of AWS data centers.

It's like an auction, where the highest bidder wins. The same context is applied to the EC2 Spot Instance.

We define the maximum spot price we're willing to pay for that instance. As long as the price of the instance (the price can go up or down) is less than our spot price, we'll keep that instance. But if the price goes higher, we can choose to stop or terminate our instance with a 2-minute grace period. When the instance price is lower than our spot price, we can restart our instance, and continue where we left off.

Spot instances allow us to bid on unused EC2 capacity, the price is significantly reduced compared to the price of On-Demand instances. But AWS can reclaim this instance any time if your bid price is lower than the instance's current price.

Cost

Up to 90% discount compared to On-Demand instance. However, prices fluctuate based on supply and demand for spare capacity.

Pricing history of c5 x.large instance type

But everything is a trade-off. This is the trade-off of potential interruptions, AWS can terminate our spot instances in 2 minutes after a warning if it needs capacity for On-Demand instances.

When to use

Useful for workloads that are resilient to failure: batch jobs, data analysis, image processing, distributed workloads, flexible start time and end time workloads... Not suitable for critical jobs or databases.

Spot instance option at "Launch instance" screen

Spot Request

Is a request we make to AWS to purchase Spot instances. It specifies the details of the instances we want like instance type, the desired number of instances, and the maximum price we are willing to pay...

Types of Spot Request

  • One-time request: a one-time spot request will ask AWS to fulfill the request once. Once the request is processed, the spot request is considered fulfilled and does not request more instances if they are terminated.
  • Persistent request: If the Spot instance is terminated, the Spot request will automatically try to launch new Spot instances to maintain the desired capacity.

How it works

If the instance is stopped, the Spot request will launch another instance for us. Therefore, we need to stop the Spot request first. Otherwise, we cannot terminate our instance.?

Spot request has a "Desired number of instances" configuration, it will launch instances as long as the desired number of instances is active.

Spot request flow

How to terminate Spot instance?

Cancelling Spot requests does not terminate instances.

We need to cancel to Spot request first, and then terminate the instances. If we terminate the instances first, the Spot Request will automatically launch a new instance to replace the terminated instance.

Spot Fleets


Create spot fleet request screen

Is a feature of AWS EC2, it allows us to manage a group of Spot instances, On-Demand instances, or a mix of both, to meet specific requirements for capacity and cost optimization. Spot Fleets = set of spot instances + (optional) On-Demand instances.

  • It will try to meet the target capacity with the upper-bound price.

  • Define possible launch pools
  • Can have multiple launch pools
  • It stops launching instances when reaching capacity or max cost

Allocate spot instance strategy:

  • Lowest price: from the pool with the lowest price (cost optimization)
  • Diversified: Aws spreads our spot instance requests evenly across all specified instance pools if we have 2 instance pools:

Instance Pool 1: m5.large in us-west-2a

Instance Pool 2: m5.large in us-west-2b

We requested 4 spot instances => the instance is distributed as follows: 2 instances from pool 1, 2 instances from pool 2

  • Capacity-optimized: pool with the optimal capacity for the number of instances
  • Price capacity-optimized: pools with the highest capacity available but with the lowest price (best choice)
  • Spot Fleets help us automatically request spot instances with the lowest price.

Example: We require 100 vCPUs for our need:

  • Target capacity of 100 vCPUs
  • Instance types like m5.large, m4.xlarge
  • Lowest price strategy
  • Maximum 10% On-demand instance to ensure availability

If the price of the Spot instance exceeds the maximum price, or an instance is terminated, Spot Fleet automatically replaces the instances to maintain the target capacity (100 vCPUs) with the lowest price possible.

要查看或添加评论,请登录

Huy Nguyen的更多文章

社区洞察

其他会员也浏览了