Chatwithcloud logo

ChatWithCloud

AWS GenAI Tools

Find and Tag Unattached Elastic Block Storage (EBS) Volumes to Optimize Costs

In this guide, we will focus on a JS (JavaScript) script which iterates through all existing AWS (Amazon Web Services) EBS volumes in a specific region and finds those not attached to any EC2 instance. It then assigns them a specific tag, which can be useful for cost management and optimization.

Code

Previously, make sure to install the AWS SDK for JavaScript in your project by using npm install aws-sdk.

Here is the script:

const AWS = require('aws-sdk'); AWS.config.update({region:'us-west-2'}); const ec2 = new AWS.EC2(); const tagUnattachedVolumes = async () => { const volumes = await ec2.describeVolumes().promise(); const unattachedVolumes = volumes.Volumes.filter(volume => volume.Attachments.length == 0 ); for (const volume of unattachedVolumes) { const params = { Resources: [volume.VolumeId], Tags: [{ Key: 'Status', Value: 'unattached' }] } await ec2.createTags(params).promise(); } console.log(`Tagged ${unattachedVolumes.length} unattached volumes`); } tagUnattachedVolumes();

This script allows for quick location and tagging of all unattached volumes in your specified AWS region.

Detailed Code Explanation

First, the script initiates the AWS SDK and sets your preferred region. In this code, 'us-west-2' is used, but it can be replaced with any valid AWS region.

The script then sets an async function, tagUnattachedVolumes(), which will call AWS SDK's built-in describeVolumes() function to fetch details of all EBS volumes in the set region.

The script filters out all the attached volumes by checking if the Attachments array in volume details exists or is empty. An empty Attachments array implies no EC2 instances are attached to the volume.

Subsequently, it loops through all unattached volumes and assigns them a tag "Status: unattached" using the createTags() function. Finally, it logs the total number of volumes tagged.

Expected output format

The expected output will be shown in your console log and should look as follows:

"Tagged 5 unattached volumes"

Where the number represents the total count of unattached volumes tagged.

Considerations & Caveats

Required IAM permissions and example policy

The minimum required IAM permissions for the script are ec2:DescribeVolumes and ec2:CreateTags.

Here is an example IAM policy:

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "ec2:DescribeVolumes", "ec2:CreateTags" ], "Resource": "*", "Effect": "Allow" } ] }

FAQ

  1. Can this script run across multiple regions?

    • Currently, the script is set for a single region. If you want to run it across multiple regions, you should modify the code to loop through your preferred regions.
  2. Can this script delete unattached volumes instead of tagging them?

    • This script is not designed to delete volumes. It's strongly recommended not to delete volumes automatically as they might contain valuable data. Always review before executing any deletion.
  3. Does the script need to be run manually each time?

    • Yes, the script is stand-alone and must be run manually each time. However, you can automate this process by implementing the code in a lambda function and setting up a scheduled CloudWatch event.
  4. What can I do with the tagged volumes?

    • Tagged volumes can easily be identified in your AWS Management Console, where you can review them for deletion or snapshotting, depending on your cost and data retention needs.




Related articles
Get the number of invocations for Lambda functions in the last 24 hoursFind Out Currently Assumed Role IAM Permissions in Typescript and AWS SDK V3Step By Step Troubleshoot Why My Route53 Domain and Cloudfront Distribution Is Not Working