3 Simple and Efficient Ways to Optimize Costs Using AWS CLI

In this blog post, I will talk about three simple ways I use to save costs using AWS CLI
Find and delete old EBS snapshosts Find and delete unused EBS volumes Find and delete old RDS snapshots
AWS offers a range of services that allow your company to store data and run applications in the cloud. However, these services come with costs that can quickly add up if not managed properly.
One way to save costs is deleting old RDS and EBS snapshots, also deleting EBS unused volumes.
Here are two other topics I wrote about cost savings.
Optimize your costs by automating the start and stop of your EC2 and RDS instances based on the schedules.
2024 AWS changes that might affect your costs if you don’t keep an eye
EBS and RDS Snapshot Essentials
RDS (Relational Database Service) and EBS (Elastic Block Storage) snapshots are backup copies of your data stored in the AWS infrastructure. While these snapshots are essential for disaster recovery and data retention, they can also incur significant costs if you do not keep a close eye.
Snapshots can be created manually or scheduled to run automatically at specified intervals. Both EBS and RDS snapshots are billed based on the amount of data stored, and the number of snapshots taken.
To save costs, you have different mechanisms to automate the way you delete old snapshots. You can use AWS Lambda, for example.
However, it is quite common for people to create EBS or RDS manual snapshots and simply FORGET about that backup.
Snapshot Data Stored Pricing
The storage cost for a snapshot is based on the additional Gb-month, and it varies by region.
As of today, the cost can be approximately as follows:
EBS snapshot -> $0.05/Gb-month. Let’s say you have 100 EBS snapshots with 500Gb of size each. You might be paying 100 x 500 x $0.05 = $2,500/month.
RDS snapshot -> $0.095/Gb-month. Let’s say you have 100 RDS snapshots with 50Gb of size each. You might be paying 100 x 500 x $0.095 = $4,750/month.
This is quite a lot of waste.
EBS Volume Essentials
With EBS, you pay only for what you provision, and it depends on the region. However, Predicting and understanding EBS costs is initially quite intimidating.
For instance, if you decide on high-performance disks, you have to pay attention to the prices as costs can add up quickly.
In addition, you have to make sure you are not overpaying for storage by allocating significantly more disk space than your application needs.
Let’s break through how you can find and delete OLD EBS SNAPSHOTS, OLD RDS SNAPSHOTS, and UNUSED EBS VOLUMES.
With AWS CLI, you have two ways to do the work, and I assume you sorted that out already.
- You can install and configure AWS CLI on your Linux machine.
- You can connect your AWS console and use Cloudshell.
1. Find and Delete Old EBS Snapshots with AWS CLI
Note: If the snapshot is part of the AMI (Amazon Machine Images), then you must first deregister the AMI. The corresponding snapshot is then deleted.
Find EBS snapshots that you own, older than 30 days, in region us-east-1. Then save the snapshot IDs on the old_ebs_snapshosts.txt file
$ aws ec2 describe-snapshots \
--owner-ids self \
--query "Snapshots[?(StartTime<='$(date --date='30 day ago' '+%Y-%m-%d')')].[SnapshotId]" \
--region us-east-1 \
--output text >old_ebs_snapshosts.txtNow, you can read the old_ebs_snapshosts.txt file and delete all EBS snapshots listed in the file.
$ for SnapshotID in $(cat old_ebs_snapshosts.txt); do
aws ec2 delete-snapshot --snapshot-id ${SnapshotID} --region us-east-1
done2. Find and Delete Old RDS Snapshots with AWS CLI
Find manual RDS snapshots, older than 30 days, in region us-east-1. Then save the snapshot IDs on the old_rds_snapshosts.txt file
$ aws rds describe-db-snapshots \
--snapshot-type manual \
--query 'DBSnapshots[?SnapshotCreateTime<`date-30`].[DBSnapshotIdentifier]' \
--region us-east-1 \
--output text >old_rds_snapshosts.txtNow, you can read the old_rds_snapshosts.txt file and delete all manual RDS snapshots listed in the file.
for SnapshotID in $(cat old_rds_snapshosts.txt); do
aws rds delete-db-snapshot --db-snapshot-identifier ${SnapshotID}
done3. Find and delete unused EBS volumes
Find unused EBS volumes, in region us-east-1. Then save the volume IDs on the unused_ebs_volumes.txt file
$ aws ec2 describe-volumes \
--filter "Name=status,Values=available" \
--query 'Volumes[*].[VolumeId]' \
--region us-east-1 \
--output text >unused_ebs_volumes.txtNow, you can read the unused_ebs_volumes.txt file and delete all unused EBS volumes listed in the file.
for SnapshotID in $(cat old_rds_snapshosts.txt); do
aws rds delete-db-snapshot --db-snapshot-identifier ${SnapshotID}
doneConclusion
Imagine if you have a company with a large environment where multiple unused EBS volumes and old snapshots are being created on a daily basis. I have no doubt it is a big challenge to keep track of those volumes and backups manually.
Consequently, these costs will hurt your company’s budget.
This simple way I just mentioned will help your company to save a huge amount of money.
Final tips and mistakes to avoid:
Where to start managing EBS snapshots? Use AWS Cost Explorer to have a big picture and analyze your snapshots, volumes costs and usage.
Tag Your EBS Snapshots Tagging EBS snapshots allows grouping snapshots by project, application, environment or other criteria. Tagging allows you to easily identify the resources and have control for compliance, automation, cost allocation, etc.
Hope it helps you!
If you have any questions, drop a comment. Share it with your friends.
