Shell script: Find the bad burstable instances in AWS cloud

kaustubh shukla
1 min readDec 3, 2023

--

Following is the shell script to find bad burstable instances in AWS cloud architecture where all the credits are exhausted:

#!/bin/bash

# Get the list of all running instances
instances=$(aws ec2 describe-instances — query “Reservations[*].Instances[*].InstanceId” — output text)

# Filter the list to only include burstable instances
burstable_instances=$(echo “$instances” | grep -E “t2|a1”)

# For each burstable instance, check its CPU credits
for instance in $burstable_instances; do
cpu_credits=$(aws ec2 describe-instances-status — instance-ids $instance — query “InstanceStatuses[*].InstanceStatus.CpuCredits” — output text)

# If the CPU credits are below 10, then the instance is bad
if [ $cpu_credits -lt 10 ]; then
# Provision a new instance of the same type
new_instance=$(aws ec2 run-instances — image-id $(aws ec2 describe-instances — instance-ids $instance — query “Reservations[*].Instances[*].ImageId” — output text) — instance-type $(aws ec2 describe-instances — instance-ids $instance — query “Reservations[*].Instances[*].InstanceType” — output text) | grep InstanceId | awk ‘{print $2}’)

# Wait for the new instance to be running
while [ $(aws ec2 describe-instances — instance-ids $new_instance — query “Reservations[*].Instances[*].InstanceState.Name” — output text) != “running” ]; do
sleep 10
done

# Terminate the bad instance
aws ec2 terminate-instances — instance-ids $instance
fi
done

Above script gives a base idea about figuring out the bad instances using shell script. It needs to be further modified to cater specific scenario and requirements.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response