Get started with EC2 Spot Instances and Spot Requests

Maxat AkbanovMaxat Akbanov
10 min read

Amazon EC2 Spot Instances are a feature of Amazon Web Services (AWS) that allow users to use spare EC2 computing capacity at steep discounts compared to On-Demand prices.

How Spot Instances work?

Here's a breakdown of EC2 Spot Instances and their key characteristics:

  1. Cost Savings: Spot Instances can be significantly cheaper than regular On-Demand Instances. You bid a price for instance types, and when the Spot market price is below your bid, your instances are launched.

  2. Flexibility: You can request Spot Instances for various use cases like data analysis, batch jobs, background processing, and applications that have flexible start and end times.

  3. Termination: One of the trade-offs for the reduced price is that Spot Instances can be terminated when the spot price goes above your bid or when AWS needs the capacity back. If a Spot Instance is terminated by Amazon EC2, you won't be charged for the partial hour of usage. However, if you terminate it yourself, you'll be charged for any hour in which the instance ran.

  4. Spot Price: The price of Spot Instances varies based on the supply and demand of spare capacity. AWS provides a history of Spot prices, but it doesn't provide a forecast. Therefore, you should be prepared for price fluctuations.

  5. Request Types: You can request Spot Instances using a one-time request (they run until they are interrupted or you terminate them) or using Spot Fleet requests (a collection of Spot Instances and, optionally, On-Demand Instances).

  6. Integration with AWS Services: Spot Instances can be integrated with other AWS services, such as EC2 Auto Scaling, EC2 Fleet, Amazon EMR, and AWS Batch.

  7. Capacity Pools: AWS has multiple capacity pools for Spot Instances, each defined by a combination of instance type, OS, and Availability Zone. By diversifying across different pools, you can increase the availability and reliability of your Spot Instances.

  8. Interruption: AWS provides a two-minute interruption notice when your Spot Instance is about to be reclaimed. This can give your applications a short time to save state, upload results, or gracefully shut down.

  9. Savings Plans for EC2 Instances: AWS also introduced EC2 Savings Plans, which provide cost savings compared to On-Demand pricing but with more predictable pricing than Spot Instances. They might be a consideration if you want some of the flexibility of Spot but with more predictability in terms of cost and longevity.

Use Cases for Spot Instances:

Spot Instances are well-suited for:

  • Batch processing jobs.

  • Scientific simulations and computations.

  • Test and development environments.

  • Stateless applications that can handle possible interruptions.

  • Web servers that can be added or removed from a load balancer pool.

  • High-performance computing (HPC) applications.

Recommendation:

If you're considering Spot Instances for your workloads, it's a good practice to architect your applications to be fault-tolerant and capable of handling interruptions. For example, if you're running a long computation, it might be wise to save intermediate results regularly so that if your Spot Instance is terminated, you don't lose a significant amount of work.

Spot Instance Request

A Spot Instance request is the mechanism by which you ask AWS to launch EC2 Spot Instances on your behalf at the bid price you specify. It's essentially your ticket into the Spot market.

When you create a Spot Instance request, you can specify parameters such as:

  1. Bid Price: The maximum price you're willing to pay per hour for the instance type you want. If the Spot price goes above your bid price, your instance may be terminated.

  2. Instance Type: The size and configuration of the EC2 instance you want, such as t2.micro, m5.large, etc.

  3. AMI (Amazon Machine Image): The OS and software stack that will be launched on the instance.

  4. Number of Instances: You can request one or multiple Spot Instances.

  5. Network Configuration: Which Virtual Private Cloud (VPC) and subnets the instances should be launched in.

  6. Persistent Request: By default, a Spot Instance request is a one-time request, meaning once AWS fulfills the request and the Spot Instance is running, the request is considered closed. If the instance is terminated or stopped, the request won't try to launch another instance. However, if you mark the request as "persistent," AWS will keep trying to fulfill the request again, even after the instance is terminated, until you cancel the Spot Instance request.

  7. Duration: In certain scenarios, you can specify a duration for which you need the Spot Instance. AWS guarantees the instance will not be terminated for the duration you specify, but you might pay a premium for this.

  8. Interruption Behavior: You can define what happens when the Spot Instance is about to be terminated. By default, the instance is terminated, but you can choose to stop or hibernate it (for some instance types). If stopped or hibernated, the Spot Instance will retain its data and can be started again later.

Once you create a Spot Instance request:

  • AWS evaluates the request based on the current Spot price. If the Spot price is lower than your bid price, AWS starts provisioning the Spot Instance for you.

  • If the Spot price goes above your bid price after the instance has been running, AWS can terminate the instance after giving a two-minute warning (known as the Spot Instance interruption notice).

  • You can view, modify, or cancel your Spot Instance requests from the AWS Management Console, AWS CLI, or AWS SDKs.

Keep in mind, while the Spot Instance is running, you're only charged based on the current Spot price, not your bid price. So, if the Spot price is lower than your bid, you pay the lower Spot price.

It's also worth noting that with the evolution of AWS services, AWS introduced a simplified pricing model where users no longer have to bid on Spot Instances. Instead, they get the Spot price which can be up to 90% off the On-Demand price. However, the fundamental characteristic remains the same – Spot Instances can be terminated if AWS needs the capacity back.

Create a Spot Instance request

To create a Spot Instance request using the launch instance wizard

Steps 1–9 are the same steps you'd use to launch an On-Demand Instance. At Step 10, you configure the Spot Instance request.

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation bar at the top of the screen, select a Region.

  3. From the Amazon EC2 console dashboard, choose Launch instance.

  4. (Optional) Under Name and tags, you can name your instance, and tag the Spot Instance request, the instance, the volumes, and the elastic graphics. For information about tags, see Tag your Amazon EC2 resources.

    1. For Name, enter a descriptive name for your instance.

      The instance name is a tag, where the key is Name, and the value is the name that you specify. If you don't specify a name, the instance can be identified by its ID, which is automatically generated when you launch the instance.

    2. To tag the Spot Instance request, the instance, the volumes, and the elastic graphics, choose Add additional tags. Choose Add tag, and then enter a key and value, and select the resource type to tag. Choose Add tag again for each additional tag to add.

  5. Under Application and OS Images (Amazon Machine Image), choose the operating system (OS) for your instance, and then select an AMI. For more information, see Application and OS Images (Amazon Machine Image).

  6. Under Instance type, select the instance type that meets your requirements for the hardware configuration and size of your instance. For more information, see Instance type.

  7. Under Key pair (login), choose an existing key pair, or choose Create new key pair to create a new one. For more information, see Amazon EC2 key pairs and Linux instances.

    Important

    If you choose the Proceed without key pair (Not recommended) option, you won't be able to connect to the instance unless you choose an AMI that is configured to allow users another way to log in.

  8. Under Network settings, use the default settings, or choose Edit to configure the network settings as necessary.

    Security groups form part of the network settings, and define firewall rules for your instance. These rules specify which incoming network traffic is delivered to your instance.

    For more information, see Network settings.

  9. The AMI you selected includes one or more volumes of storage, including the root device volume. Under Configure storage, you can specify additional volumes to attach to the instance by choosing Add new volume. For more information, see Configure storage.

  10. Under Advanced details, configure the Spot Instance request as follows:

    1. Under Purchasing option, select the Request Spot Instances check box.

    2. You can either keep the default configuration for the Spot Instance request, or choose Customize (at the right) to specify custom settings for your Spot Instance request.

      When you choose Customize, the following fields appear.

      1. Maximum price: You can request Spot Instances at the Spot price, capped at the On-Demand price, or you can specify the maximum amount you're willing to pay.

        Warning

        If you specify a maximum price, your instances will be interrupted more frequently than if you choose No maximum price.

        • No maximum price: Your Spot Instance will launch at the current Spot price. The price will never exceed the On-Demand price. (Recommended)

        • Set your maximum price (per instance/hour): You can specify the maximum amount you're willing pay.

          • If you specify a maximum price that is less than the current Spot price, your Spot Instance will not launch.

          • If you specify a maximum price that is more than the current Spot price, your Spot Instance will launch and be charged at the current Spot price. After your Spot Instance is running, if the Spot price rises above your maximum price, Amazon EC2 interrupts your Spot Instance.

          • Regardless of the maximum price you specify, you will always be charged the current Spot price.

To review Spot price trends, see Spot Instance pricing history.

  1. Request type: The Spot Instance request type that you choose determines what happens if your Spot Instance is interrupted.

    • One-time: Amazon EC2 places a one-time request for your Spot Instance. If your Spot Instance is interrupted, the request is not resubmitted.

    • Persistent request: Amazon EC2 places a persistent request for your Spot Instance. If your Spot Instance is interrupted, the request is resubmitted to replenish the interrupted Spot Instance.

If you do not specify a value, the default is a one-time request.

  1. Valid to: The expiration date of a persistent Spot Instance request.

    This field is not supported for one-time requests. A one-time request remains active until all the instances in the request launch or you cancel the request.

    • No request expiry date: The request remains active until you cancel it.

    • Set your request expiry date: The persistent request remains active until the date that you specify, or until you cancel it.

  2. Interruption behavior: The behavior that you choose determines what happens when a Spot Instance is interrupted.

    • For persistent requests, valid values are Stop and Hibernate. When an instance is stopped, charges for EBS volume storage apply.

    • For one-time requests, only Terminate is valid.

If you do not specify a value, the default is Terminate, which is not valid for a persistent Spot Instance request. If you keep the default and try to launch a persistent Spot Instance request, you'll get an error.

For more information, see Interruption behavior.

  1. On the Summary panel, for Number of instances, enter the number of instances to launch.

    Note

    Amazon EC2 creates a separate request for each Spot Instance.

  2. On the Summary panel, review the details of your instance, and make any necessary changes. After you submit your Spot Instance request, you can't change the parameters of the request. You can navigate directly to a section in the launch instance wizard by choosing its link in the Summary panel. For more information, see Summary.

  3. When you're ready to launch your instance, choose Launch instance.

    If the instance fails to launch or the state immediately goes to terminated instead of running, see Troubleshoot instance launch issues.

  4. To check whether the running EC2 instance is a Spot instance type on the AWS Management Console:

    • Navigate to the EC2 dashboard.

    • Click on "Instances" in the sidebar.

    • In the list of instances, look for the "Lifecycle" column. If the instance is a Spot Instance, it will show "spot" in this column.

References

  1. Work with Spot Instances

  2. Amazon EC2 Spot Instances

0
Subscribe to my newsletter

Read articles from Maxat Akbanov directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Maxat Akbanov
Maxat Akbanov

Hey, I'm a postgraduate in Cyber Security with practical experience in Software Engineering and DevOps Operations. The top player on TryHackMe platform, multilingual speaker (Kazakh, Russian, English, Spanish, and Turkish), curios person, bookworm, geek, sports lover, and just a good guy to speak with!