Optimizing Cloud Costs: Effective Use of Amazon EC Spot Instances

Listen to this Post

One of the biggest costs on AWS is EC2 VM’s. EC2 nodes are used in Elastic Kubernetes Service (EKS) clusters, Elastic Container Service (ECS), and other services. The default On-demand pricing can be expensive, but in many cases, you can choose Spot instead.

With Spot instance types, you pay a percentage of the On-demand cost, but the catch is they can be reclaimed by AWS with a 2-minute warning at any time. If your workload is okay with restarting or can save its progress and continue on a new instance, then using Spot is a great choice.

When you set up auto-scaling groups for your EC2 instances, you can decide which types of EC2 nodes to use and also what type of pricing—i.e., On-demand vs. Spot.

Micaela Marrero shows how you can set up scaling groups with Terraform to use a mix of on-demand and Spot instance types.

Read the full article here

You Should Know:

  1. Terraform Configuration for Mixed Spot & On-Demand Instances
    Here’s a sample Terraform script to configure an Auto Scaling Group (ASG) with both Spot and On-Demand instances:
resource "aws_autoscaling_group" "mixed_instances" {
name = "mixed-instances-asg"
min_size = 2
max_size = 10
desired_capacity = 4
vpc_zone_identifier = [aws_subnet.example.id]

mixed_instances_policy {
launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.example.id
version = "$Latest"
}

override {
instance_type = "t3.medium"
weighted_capacity = "1"
}

override {
instance_type = "t3.large"
weighted_capacity = "2"
}
}

instances_distribution {
on_demand_base_capacity = 2
on_demand_percentage_above_base_capacity = 50
spot_allocation_strategy = "capacity-optimized"
}
}
}

2. AWS CLI Commands to Monitor Spot Instances

Check Spot Instance pricing and availability:

aws ec2 describe-spot-price-history --instance-types t3.medium --product-descriptions "Linux/UNIX"

List active Spot Instance requests:

aws ec2 describe-spot-instance-requests --filters Name=state,Values=active

3. Handling Spot Interruptions in Kubernetes (EKS)

To gracefully handle Spot interruptions in EKS, use a Spot Interrupt Handler:

kubectl apply -f https://github.com/aws/aws-node-termination-handler/releases/download/v1.18.0/all-resources.yaml
  1. Linux Commands to Check Instance Termination Notices
    AWS sends Spot termination notices via the instance metadata service. Check if your instance is marked for termination:

    curl -s http://169.254.169.254/latest/meta-data/spot/instance-action
    

5. Automating Workload Migration

Use a script to save progress before termination:

!/bin/bash
if curl -s http://169.254.169.254/latest/meta-data/spot/instance-action | grep -q "action"; then
echo "Spot instance termination detected. Saving state..."
 Add backup/state-saving commands here
rsync -avz /critical/data s3://my-backup-bucket/
fi

What Undercode Say:

Using AWS Spot Instances can reduce cloud costs by up to 90%, but requires proper automation to handle interruptions. Key takeaways:
– Terraform enables mixed instance policies for cost optimization.
– AWS CLI helps monitor Spot pricing and availability.
– Kubernetes termination handlers ensure smooth EKS workload transitions.
– Linux metadata checks allow proactive response to Spot interruptions.

For fault-tolerant workloads, Spot Instances are a game-changer—just ensure your systems can recover automatically.

Expected Output:

A well-optimized AWS environment using Spot Instances with automated recovery mechanisms, reducing costs while maintaining reliability.

Read the full article here

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image