Skip to main content

Spot Optimizer

PyPI version Python Tests codecov PyPI - Python Version License PyPI Downloads

🚀 Spot Optimizer is a Python library that helps users select the best AWS spot instances based on their resource requirements, including cores, RAM, storage type (SSD), instance architecture (x86 or ARM), AWS region, EMR version compatibility, and instance family preferences.

It replaces complex, in-house logic for finding the best spot instances with a simple and powerful abstraction. No more manual guesswork — just the right instances at the right time.

Why Spot Optimizer?

Managing spot instance selection within your codebase can be tedious and error-prone. Spot Optimizer provides a clean, abstracted solution to intelligently select the most stable and cost-effective instances.

Configuration Guarantee

It ensures that the selected configuration meets or exceeds the user's requirements. For example, if you request 20 cores and 100GB of RAM, the library will suggest a configuration with at least those resources, rounding up to the nearest available configuration.

Key Benefits

  • 💡 Informed Decisions: Picks instances with the lowest interruption rates and the best fit for your workload.
  • 🧠 Dynamic Reliability: Smartly updates interruption rates every hour to ensure the most stable instance selection.
  • 🛠️ Operational Efficiency: No more homegrown scripts or complex logic — just plug and play.
  • ⚡ High Performance: DuckDB-powered analytics with 98.4% test coverage and 5.4ms average response time.
  • 🏗️ Scalable and Reliable: Automatically adjusts to changing resource needs while minimizing downtime.

Spot Optimizer vs AWS EC2 Fleet

How They Differ

Spot Optimizer is a decision-making tool that helps you choose the optimal instance types before launching, while AWS EC2 Fleet is a provisioning service that launches and manages the actual instances.

Aspect Spot Optimizer AWS EC2 Fleet
Purpose Instance type selection & optimization Instance provisioning & management
When to Use Before launching instances When launching instances
Output Recommended instance types & counts Running EC2 instances
Focus Interruption rates, resource fit, cost optimization Capacity fulfillment, diversification
Scope Analysis and recommendations Infrastructure deployment

How They Work Together

Perfect Complementary Workflow:

# Step 1: Use Spot Optimizer to find the best instance types
from spot_optimizer import optimize

result = optimize(cores=64, memory=256, region="us-east-1", mode="fault_tolerance")
# result["candidates"]: ranked instance types, each with the count needed on its own

# Step 2: Use the recommendations with AWS EC2 Fleet
import boto3

ec2 = boto3.client('ec2')
response = ec2.create_fleet(
    LaunchTemplateConfigs=[
        {
            'LaunchTemplateSpecification': {
                'LaunchTemplateName': 'my-template',
                'Version': '1'
            },
            # Diversify across every candidate, weighted by vCPUs per instance
            'Overrides': [
                {
                    'InstanceType': c['instances']['type'],
                    'WeightedCapacity': c['total_cores'] / c['instances']['count'],
                }
                for c in result['candidates']
            ]
        }
    ],
    TargetCapacitySpecification={
        'TotalTargetCapacity': 64,  # vCPUs
        'DefaultTargetCapacityType': 'spot'
    },
    SpotOptions={'AllocationStrategy': 'price-capacity-optimized'}
)

Use Cases for Each

Use Spot Optimizer when:

  • Planning infrastructure for new workloads
  • Optimizing existing deployments for cost/stability
  • Analyzing instance options across regions
  • Building infrastructure-as-code templates
  • Integrating spot instance logic into applications

Use AWS EC2 Fleet when:

  • Actually launching the recommended instances
  • Managing instance lifecycle and replacement
  • Handling spot interruptions automatically
  • Scaling capacity up/down dynamically
  • Diversifying across multiple instance types/AZs

Integration Benefits

  1. Smarter Fleet Configurations: Use Spot Optimizer's recommendations to configure more effective EC2 Fleet launch templates
  2. Reduced Trial and Error: Skip the guesswork of which instance types to include in your fleet
  3. Better Cost Optimization: Combine Spot Optimizer's interruption rate analysis with Fleet's diversification
  4. Improved Reliability: Use fault-tolerance mode recommendations for mission-critical Fleet deployments

Installation

For Users

pip install spot-optimizer

For Development

Requires Python 3.11+ and uv.

# Clone the repository
git clone git@github.com:amarlearning/spot-optimizer.git
cd spot-optimizer

# Install dependencies and set up development environment
make install

Usage

API Usage

from spot_optimizer import optimize

# Basic usage
result = optimize(cores=8, memory=32)

# Advanced usage with all options
result = optimize(
    cores=8,
    memory=32,
    region="us-east-1",
    ssd_only=True,
    arm_instances=False,
    instance_family=["m6i", "r6i"],
    mode="balanced",
    top_n=5
)

# Top-level keys are the best match; "candidates" is the ranked list (best first)
{
   "instances": {
      "type": "m6i.xlarge",
      "count": 2
   },
   "total_cores": 8,
   "total_ram": 32,
   "reliability": {
      "spot_score": 72,       # % savings over On-Demand (Spot Advisor "s")
      "interruption_rate": 0  # Spot Advisor bucket: 0 = <5%, 1 = 5-10%, ... 4 = >20%
   },
   "mode": "balanced",
   "candidates": [
      {"instances": {"type": "m6i.xlarge", "count": 2}, "total_cores": 8, "total_ram": 32, "reliability": {...}},
      {"instances": {"type": "r6i.xlarge", "count": 2}, "total_cores": 8, "total_ram": 64, "reliability": {...}}
   ]
}

CLI Usage

# Basic usage
spot-optimizer --cores 8 --memory 32

# Advanced usage
spot-optimizer \
    --cores 8 \
    --memory 32 \
    --region us-east-1 \
    --ssd-only \
    --no-arm \
    --instance-family m6i r6i \
    --mode balanced \
    --top-n 5

# Get help
spot-optimizer --help

Inputs

Required Parameters

  1. cores (int): The total number of CPU cores required.
  2. memory (int): The total amount of memory required in GB.

Optional Parameters

  1. region (str): AWS region for spot instance selection (default: "us-west-2").
  2. ssd_only (bool): If True, only suggest instances with SSD-backed storage (default: False).
  3. arm_instances (bool): If True, include ARM-based instances (default: True).
  4. instance_family (List[str]): Filter by specific instance families (e.g., ['m6i', 'r6i']).
  5. emr_version (str): Optional EMR version to ensure instance compatibility.
  6. mode (str):
    • latency: Optimize for fewer, larger nodes (lower latency).
    • fault_tolerance: Optimize for more, smaller nodes (better fault tolerance).
    • balanced: Aim for a middle ground between fewer and more nodes.
  7. top_n (int): How many ranked instance types to return in candidates (default: 5). Spot works best when spread across several types, so pass the whole list to your Fleet or ASG.

Development

Make Commands

# Install dependencies
make install

# Run all tests (unit + integration)
make test

# Run only unit tests
make test-unit

# Check test coverage
make coverage

# Clean up build artifacts
make clean

Performance Optimizations

  • Efficiently updates the instance interruption table only every hour, avoiding unnecessary data fetches.
  • Focuses on providing the most stable instances based on the latest interruption rate data.
  • DuckDB-powered analytics with average 5.4ms query response time.

Future Enhancements

  1. Cost Optimization:
    • Include estimated instance costs and recommend the most cost-effective configuration.
  2. Support for Other Cloud Providers:
    • Extend the library to support GCP and Azure instance types.
  3. Advanced Analytics:
    • Historical interruption pattern analysis and predictive modeling.

Issues

If you encounter any bugs, please report them on the issue tracker. Alternatively, feel free to tweet me if you're having trouble. In fact, you should tweet me anyway.


License

Built with ♥ by Amar Prakash Pandey(@amarlearning) under Apache License 2.0.

Metadata

Release files for spot-optimizer 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spot-optimizer 1.3.0
File Size Uploaded Provenance Actions
spot_optimizer-1.3.0.tar.gz 22.8 kB Without provenance Details for spot_optimizer-1.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spot-optimizer 1.3.0
File Interpreter ABI Platform Actions
spot_optimizer-1.3.0-py3-none-any.whl Python 3 none any Details for spot_optimizer-1.3.0-py3-none-any.whl

Total release size: 48.4 kB

Release files / spot_optimizer-1.3.0.tar.gz

Download URL spot_optimizer-1.3.0.tar.gz
Size 22.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bfcdaedc630f67d0841c6523a33bb58e261085c88438a49b8afae2b82fa26ca7
BLAKE2b-256 checksum
How to use checksums
cd72533c408929b30b17c4eee794c9278837419f6bc93cb11a839fd8778d9cd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / spot_optimizer-1.3.0-py3-none-any.whl

Download URL spot_optimizer-1.3.0-py3-none-any.whl
Size 25.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f23aa8f6f20a4a8c6e1faf538bb508576cd8c1a3966a375004c27630a626211
BLAKE2b-256 checksum
How to use checksums
5cedc806c82b51c768d6f5cb794122ea05b7e694588d882b6821ceab62e2812e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for spot-optimizer 1.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page