Thursday, April 24, 2025

Requests an AWS Spot Instances | Procedure.

 

An overview of how to Requests an AWS Spot Instances. 

Focus:

  • Tailored for Devops, DevSecops & Cloud Engineers.

Breakdown:

  • Intro,
  • Using the AWS Management Console,
  • Using the AWS CLI,
  • The concept: Spot Instances,
  • The concept: Spot Request,
  • Types of Spot Instances,
  • Relationship Between One-time & Persistent Spot Instance,
  •  Lifecycle Flow (Typical),
  • Spot Request Auto-Replacement,
  •  Canceling the Spot Request First,
  •  One-time vs. Persistent Spot Requests,
  • Best Practice.

Intro:

  • AWS Spot Instances can be requested using the AWS Management Console, AWS Command Line Interface (CLI), or the EC2 APIs
  • The most common method is through the console using a launch template or the launch wizard.

Using the AWS Management Console
  1. Log in to the AWS Management Console and navigate to the EC2 dashboard.
  2. In the left navigation pane, click on Spot Requests under the "Instances" or "Instances (new)" section.
  3. Click the Request Spot Instances button.
  4. twtech can either select an existing Launch template or manually configure the launch parameters through the wizard. The launch template approach is recommended for reusability.
  5. If using the wizard, twtech will specify details such as:
    • Amazon Machine Image (AMI): The template for twtech instance's operating system and software.
    • Instance type(s): Select multiple instance types across different Availability Zones for better availability and cost optimization (using the price-capacity-optimized allocation strategy is best practice).
    • Target capacity: The number of instances twtech wants to run.
    • Request type: Choose "one-time" or "persistent". A persistent request attempts to maintain the target capacity even after interruptions.
    • Network and security: Configure the VPC, subnet, security groups, and key pair for SSH access.
    • User data: Optional script to run when the instance starts.
NB:
  • Once all parameters are configured & twtech reviews settings and click Submit to place its Spot Instance request.
  • AWS will fulfill the request if capacity is available and the current Spot price is at or below the On-Demand price 
  • twtech doesn't  need to set a maximum price.

Using the AWS CLI

twtech can also request instances using the AWS CLI by using the request-spot-instances command and providing the launch specifications in a JSON file. 
# bash
aws ec2 request-spot-instances --instance-count 1 --type "one-time" --launch-specification file://specification.json

The specification.json file would contain details like the AMI ID, instance type, security group, and key pair information.
twtech can check the status of its request with the describe-spot-instance-requests command. 
NB:
  • These guides explain how to request AWS Spot Instances using the AWS Management Console and AWS CLI:

The concept: Spot Instances

  • Spot Instances are unused EC2 capacity offered at a discount (up to 90% cheaper than On-Demand).
  • Spot Instances can be terminated by AWS at any time if the capacity is needed back or twtech max bid is exceeded by another bider.
  • Spot Instances is Great for fault-tolerant or flexible workloads (e.g., batch jobs, CI/CD, big data, stateless microservices).

The concept: Spot Request

A Spot Request is the procedure twtech takes to ask AWS to give its account a Spot Instance.

 Types of Spot Instances:

Type

Description

One-time

AWS launches the Spot Instance once. After it runs (or is terminated), it’s done.

Persistent

AWS will keep trying to launch a new instance if the existing one is terminated (e.g., due to capacity issues or manual termination).

Relationship Between One-time & Persistent Spot Instance
  • twtech submits a Spot Request, and if capacity and price allow, a Spot Instance is launched.
  • If you terminate the instance but leave the request active (persistent), AWS may launch another.
  • To prevent that, you need to cancel the request before terminating the instance.

 Lifecycle Flow (Typical)

  1. twtech submits a Spot Request via Console, CLI, SDK, or IaC (like Terraform).
  2. AWS evaluates if there’s capacity and if twtech max price is acceptable.
  3. If yes, AWS launches a Spot Instance.
  4. If the request is persistent, AWS will monitor and attempt re-launches if needed.

In AWS, Spot Instances are tied to Spot Requests — and here's why the request must be canceled before terminating the instance:

 Spot Request Auto-Replacement

  • When you launch a Spot Instance via a persistent spot request, AWS assumes twtech wants that instance type at that price continuously
  • So if twtech just terminate the instance without canceling the request:

    • AWS will automatically launch a replacement instance (because the request is still active).
    • That means twtech cost continues, and it might lose control over what's running.

 Canceling the Spot Request First

When twtech cancels the spot request first, twtech tell AWS:

  • "twtech doesn't want the Spot instance anymore — stop trying to fulfill this request."
  • when twtech terminates the instance, AWS won't try to replace Spot Instance anymore.

 One-time vs. Persistent Spot Requests

  • Persistent spot request: Keeps trying to fulfill the request even after termination.
  • One-time spot request: If fulfilled and the instance is terminated, AWS won't try to replace it. But still best practice to cancel it for clarity and cost control

twtech Best Practice:

  1. Spot Instances can be terminated by AWS at any time if the capacity is needed back or twtech max bid is exceeded by another bider.
  2. Spot Instances are unused EC2 capacity offered at a discount (up to 90% cheaper than On-Demand).
  3. Spot Instances is Great for fault-tolerant or flexible workloads (e.g., batch jobs, CI/CD, big data, stateless microservices).
  4. Persistent spot request: Keeps trying to fulfill the request even after termination.
  5. Always cancel the spot request first before terminating Spot Instance.
  6. Second, terminate the instance if no longer needed to avoid cost.
  7. Always cancel persistent requests before terminating instances to avoid auto-replacement.
  8.  Clean up old or failed spot requests to avoid conflicts and extra billing in envieronment.
  9.  Use Auto Scaling Groups with Mixed Instances or EC2 Fleet for more control and resiliency.

No comments:

Post a Comment

Amazon EventBridge | Overview.

Amazon EventBridge - Overview. Scope: Intro, Core Concepts, Key Benefits, Link to official documentation, Insights. Intro: Amazon EventBridg...