$ show awslabs/ec2-spot-labs
This repository is a collection of AWS EC2 Spot Instance best practices and workshop examples. It contains diverse implementations showing how to use Spot Instances across various AWS services including EC2 Auto Scaling, EC2 Fleet, ECS, EKS, Lambda, SageMaker, AWS Batch, and EMR. The examples demonstrate patterns for handling Spot Instance interruptions, cost optimization, fault tolerance, and workload-specific optimizations. While not a single cohesive application, it serves as a practical reference for deploying cost-effective, interruption-resilient workloads on AWS Spot Instances.
The LLM identified 4 files worth highlighting for reuse:
A self-contained shell script for EC2 instance user data that gracefully handles Spot termination notices by polling the instance metadata service. It demonstrates the pattern of detecting interruptions via metadata endpoint and performing cleanup actions (like deregistering from load balancers) before termination. Low coupling - only depends on standard shell utilities and AWS CLI, making it reusable in any EC2 Spot Instance workload.
A Lambda function that processes Spot Instance interruption notices from CloudWatch Events and executes SSM Run Command on affected instances. Shows centralized interruption handling across multiple instances using event-driven architecture. Minimal dependencies (boto3) and isolated logic - could be extracted for use in any environment needing to react to Spot interruption events at scale.
A Python script designed to run as a cron job on ECS container instances to check for Spot termination notices and initiate container draining. Demonstrates ECS-specific Spot handling pattern with AWS CLI and ECS agent commands. Self-contained with clear separation of concerns - reusable for any ECS on Spot workload needing graceful container instance termination handling.
A Lambda function that periodically checks EC2 Spot Instance prices and triggers actions when prices exceed thresholds. Illustrates cost optimization pattern using AWS pricing APIs and conditional instance management. Well-structured with configurable parameters and error handling - adaptable for various cost-based automation scenarios beyond Spot Instances.