As an AWS developer or engineer, it’s essential to manage your AWS resources effectively to optimize your usage and reduce costs.
One common issue that arises is the accumulation of unattached EBS volumes, which can result in unnecessary costs.
In this tutorial, you’ll learn how to use Python Boto3 to automate the deletion of all unattached EBS volumes across all AWS regions.
Table of Contents
How to delete the unattached EBS Volumes across all AWS regions
Before you can start, you’re required to have done the following prerequisites before you can run the Python script on your AWS account.
- Install the AWS CLI and configure an AWS profile
- Setting up the Python Environment
If you’ve already done this, you can proceed to step 3.
1. Install AWS CLI and configure an AWS profile
The AWS CLI is a command line tool that allows you to interact with AWS services in your terminal.
Depending on if you’re running Linux, macOS, or Windows the installation goes like this:
# macOS install method:
brew install awscli
# Windows install method:
wget https://awscli.amazonaws.com/AWSCLIV2.msi
msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi
# Linux (Ubuntu) install method:
sudo apt install awscli
In order to access your AWS account with the AWS CLI, you first need to configure an AWS Profile. There are 2 ways of configuring a profile:
- Access and secret key credentials from an IAM user
- AWS Single Sign-on (SSO) user
In this article, I’ll briefly explain how to configure the first method so that you can proceed with running the python script on your AWS account.
If you wish to set up the AWS profile more securely, then I’d suggest you read and apply the steps described in setting up AWS CLI with AWS Single Sign-On (SSO).
In order to configure the AWS CLI with your IAM user’s access and secret key credentials, you need to login to the AWS Console.
Go to IAM > Users, select your IAM user, and click on the Security credentials tab to create an access and secret key.
Then configure the AWS profile on the AWS CLI as follows:
➜ aws configure
AWS Access Key ID [None]: <insert_access_key>
AWS Secret Access Key [None]: <insert_secret_key>
Default region name [None]: <insert_aws_region>
Default output format [json]: json
Your was credentials are stored in ~/.aws/credentials and you can validate that your AWS profile is working by running the command:
➜ aws sts get-caller-identity
{
"UserId": "AIDA5BRFSNF24CDMD7FNY",
"Account": "012345678901",
"Arn": "arn:aws:iam::012345678901:user/test-user"
}
2. Setting up the Python Environment
To be able to run the Python Boto3 script, you will need to have Python installed on your machine.
Depending on if you’re running Linux, macOS, or Windows the installation goes like this:
# macOS install method:
brew install python
# Windows install method:
wget https://www.python.org/ftp/python/3.11.2/python-3.11.2-amd64.exe
msiexec.exe /i https://www.python.org/ftp/python/3.11.2/python-3.11.2-amd64.exe
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
# Linux (Ubuntu) install method:
sudo apt install python3 python3-pip
Once you have installed Python, you will need to install the Boto3 library.
You can install Boto3 using pip, the Python package manager, by running the following command in your terminal:
pip install boto3
3. Create Python Script to Delete All Unattached EBS Volumes in All AWS Regions
Once you have our environment set up, you can create the Python script.
Copy the following code into a new file on the desired location and name it: delete_all_unattached_volumes.py
.
# https://github.com/dannysteenman/aws-toolbox
#
# License: MIT
#
# This script deletes all unattached EBS volumes in all AWS Regions
import boto3
ec2 = boto3.client("ec2")
count = 0
for region in ec2.describe_regions()["Regions"]:
region_name = region["RegionName"]
try:
ec2conn = boto3.resource("ec2", region_name=region_name)
unattached_volumes = [
volume for volume in ec2conn.volumes.all() if not volume.attachments
]
for volume in unattached_volumes:
count += 1
volume.delete()
print(f"Deleted unattached volume {volume.id} in region {region_name}")
except Exception as e:
print(f"No access to region {region_name}: {e}")
if count > 0:
print(f"Deleted {count} unattached volumes")
The script displayed above uses the Boto3 library to interact with the Amazon EC2 API. It first describes all AWS regions and then iterates through them.
For each region, it creates a Boto3 EC2 client and retrieves a list of unattached EBS volumes. It then loops through this list and deletes each volume.
The script also prints a message for each deleted volume to indicate the status of the deletion process.
If no unattached volumes are found, the script prints a message indicating that there were no volumes to delete.
4. Run the Python Boto3 script on your AWS account
To run the script, simply execute the following command in your terminal or command prompt:
python delete_all_unattached_volumes.py
The script will start running, and you should see output similar to the following:
Deleted unattached volume vol-0123456789abcdef in region us-east-1
Deleted unattached volume vol-9876543210fedcba in region eu-west-1
Deleted 2 unattached volumes
Conclusion
In conclusion, automating the deletion of unattached EBS volumes is an easy and effective way to reduce unnecessary costs in your AWS environment.
By using Boto3, we can easily script this process to ensure that all unattached volumes are consistently deleted across all regions.
Remember to always test your code before executing it in production and make sure to double-check your permissions before running any scripts that can modify your AWS environment.