patternjavascriptModerate
AWS cost optimization: right-sizing, Savings Plans, and Spot Instances
Viewed 0 times
AWS costSavings PlansSpot Instancesright-sizingCompute OptimizerCost Anomaly Detectionbudget alertscost optimization
Problem
AWS bills grow unexpectedly due to over-provisioned instances, on-demand pricing for predictable workloads, and forgotten resources (idle NAT Gateways, unattached EBS volumes, old snapshots).
Solution
Use AWS Compute Optimizer for right-sizing recommendations. Purchase Savings Plans (not Reserved Instances) for predictable compute usage (covers Lambda, Fargate, EC2 at 20-66% discount). Use Spot Instances for fault-tolerant batch workloads (70-90% discount). Enable AWS Cost Anomaly Detection and set budget alerts.
Why
On-demand pricing is the baseline. Savings Plans provide flexibility to move between EC2, Lambda, and Fargate while keeping the discount. Spot Instances can be interrupted with 2-minute notice — suitable for ECS tasks, batch processing, and CI/CD runners.
Gotchas
- Savings Plans commit to $/hour spend — buy based on your current steady-state usage, not peak
- Reserved Instances are instance-type specific; Compute Savings Plans are not — prefer Savings Plans for flexibility
- Spot Instance interruption gives a 2-minute warning via EC2 metadata API — implement termination handlers in your workers
- NAT Gateway hourly charges ($0.045/hr per gateway) accumulate — use VPC Endpoints to reduce traffic and consolidate gateways where possible
- Enable S3 Intelligent-Tiering for large buckets with unknown access patterns — automatic tiering to cheaper storage classes
Code Snippets
Polling EC2 metadata for Spot Instance termination notice
// Handle Spot Instance interruption in ECS task
// Poll EC2 metadata every 5 seconds inside worker
setInterval(async () => {
try {
const res = await fetch('http://169.254.169.254/latest/meta-data/spot/termination-time');
if (res.status === 200) {
console.log('Spot interruption notice received — shutting down gracefully');
await gracefulShutdown();
process.exit(0);
}
} catch {}
}, 5000);Context
Reviewing and reducing AWS infrastructure costs for production workloads
Revisions (0)
No revisions yet.