DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

Efficiently Managing AWS Unattached Volumes with a Shell Script

In the realm of cloud computing, managing resources efficiently is key to controlling costs and ensuring smooth operations. For AWS (Amazon Web Services) users, one often overlooked task is dealing with unattached Elastic Block Store (EBS) volumes. These volumes can accumulate over time, leading to unnecessary storage costs. In this article, we’ll explore a practical approach to identifying and deleting unattached EBS volumes using a simple shell script.

The Problem: Unattached EBS Volumes

When you delete an EC2 instance, the attached EBS volumes might not be removed automatically. This leaves behind unattached volumes that still incur charges. Identifying and cleaning up these unused volumes can help reduce costs and keep your AWS environment tidy.

The Solution: A Shell Script for Automated Cleanup

Below is a shell script designed to find and delete unattached EBS volumes across all AWS regions. This script leverages AWS CLI commands to automate the process, saving you time and effort.

The Script

#!/bin/bash

now=$(date)
echo ".................................................................. "
echo "Delete Unattached Volumes"
echo "Date: $now "
echo ".................................................................. "
tot_val=0
# Iterate through each AWS region
for region in $(aws ec2 describe-regions --query 'Regions[].RegionName' --output text)
do
    # Count unattached volumes in the current region
    n=$(aws ec2 describe-volumes --region ${region} --filters Name=status,Values=available | grep VolumeId | wc -l)
    printf "Unattached Volumes in the region %15s: %10s\n" $region $n
    # Loop through each unattached volume and delete it
    for the_volume in $(aws ec2 describe-volumes --region ${region} --filters Name=status,Values=available | grep VolumeId | awk ' { print substr($2, 2, 21) } ')
    do
        echo "aws ec2 delete-volume --region ${region} --volume-id ${the_volume}"
        aws ec2 delete-volume --region ${region} --volume-id ${the_volume}
        echo "${the_volume} - deleted"
        echo " "
    done
    tot_val=$((tot_val + n))
done
echo ".................................................................. "
printf "Unattached Volumes in TOTAL:                       %10s\n" $tot_val
echo ".................................................................. "
Enter fullscreen mode Exit fullscreen mode

Breaking Down the Script

Initialization: The script starts by recording the current date and time, which helps in logging and tracking when the cleanup operation was performed.

Region Iteration: It fetches a list of all AWS regions using aws ec2 describe-regions and iterates through each region. This ensures that the script covers every part of your AWS infrastructure.

Counting Unattached Volumes: For each region, the script counts the number of unattached volumes by filtering volumes with the status available. It uses grep and wc -l to determine the count.

Deleting Unattached Volumes: The script then loops through each unattached volume in the region, constructs the delete command, and executes it. Each deletion is logged to the console for visibility.

Summary: Finally, it summarizes the total number of unattached volumes deleted across all regions.

Benefits of This Approach

Cost Efficiency: By regularly running this script, you can avoid unnecessary charges from unused EBS volumes.
Time Savings: Automating the cleanup process reduces manual effort and potential errors.
Scalability: The script is designed to handle multiple regions, making it suitable for large AWS environments.

Conclusion

Maintaining a clean AWS environment is crucial for managing costs and ensuring optimal performance. This shell script provides a straightforward method for identifying and deleting unattached EBS volumes across all AWS regions. By integrating such automation into your routine, you can streamline your cloud resource management and focus on more strategic tasks.

Feel free to adapt and extend this script according to your specific needs.

Top comments (0)