Skip to main content

AWS EC2 spot instance optimizer

Project description

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 Flexibility: Supports diverse use cases like Spark/EMR clusters, ML workloads, gaming servers, and more.
  • 🏗️ Scalable and Reliable: Automatically adjusts to changing resource needs while minimizing downtime.

Installation

For Users

pip install spot-optimizer

For Development

# 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"
)

# output
{
   "instances": {
      "type": "m6i.2xlarge",
      "count": 1
   },
   "mode": "balanced",
   "total_cores": 8,
   "total_ram": 32
}

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

# 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.

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. Spot Interruption Rates:
    • Include interruption rates in the selection criteria for spot instances.

Development

Make Commands

# Install dependencies
make install

# Run tests
make test

# Check test coverage
make coverage

# Clean up build artifacts
make clean

Performance Optimisations

  • 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.

Performance Benchmarks

Performance tests were run on GitHub Actions runner (2 vCPU, 7GB RAM) with 64,295 different combinations of resource requirements and constraints.

Quick Stats

  • Total Combinations Tested: 64,295
  • Total Processing Time: 338.88 seconds
  • Cache Preparation Time: 19.30 seconds

Query Performance

Metric Time (ms)
Average 5.4
Minimum 4.1
Maximum 21.5
Median 5.3
95th Percentile 6.4
Standard Deviation 0.6

Throughput

  • Average Processing Rate: ~190 queries/second
  • Effective Throughput: 64,295 combinations in 338.88 seconds

Note: These benchmarks were run on GitHub Actions' standard runner (2 vCPU, 7GB RAM). Performance in production environments will likely be better with dedicated hardware and more resources.


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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spot_optimizer-1.1.0.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spot_optimizer-1.1.0-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file spot_optimizer-1.1.0.tar.gz.

File metadata

  • Download URL: spot_optimizer-1.1.0.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.9.6 Darwin/24.5.0

File hashes

Hashes for spot_optimizer-1.1.0.tar.gz
Algorithm Hash digest
SHA256 734917510dc5bdb4542b926d13d8940ca7d9eea9e04da3b7be3d69aceb49f361
MD5 1273a4cef9ee7965c99c48131ce668ae
BLAKE2b-256 d1717444efdb220e2623038d9b011a47422c361952ad3943cb943b81d9fdc6bc

See more details on using hashes here.

File details

Details for the file spot_optimizer-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: spot_optimizer-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.9.6 Darwin/24.5.0

File hashes

Hashes for spot_optimizer-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d6467df069ff808f603e838ad0097518f2d94a9f542420f6b29a230867166765
MD5 9a88e6431eb37edf5f972ccfdf5f7f7f
BLAKE2b-256 f17c1f64f0e12df8b2d321486382b520fc38183e8ffe8b59014ae238fb190bcf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page