Skip to content

How to find gp2 volumes in AWS cloud | Lambda & CLI tutorial

How to find all gp2 volumes in all regions 2023 aws lambda and cli

Hi, in this article I will show you how using lambda and CLI you can find all gp2 volumes in all regions. Why is it worth doing? Because gp3 is in most cases faster, more efficient and of course cheaper 🙂

I already wrote a bit about gp2 and gp3 in Amazon EBS Volumes and gp3 – you can save up to 20% (lepczynski.it). There you will also find an easy way to change the disk type.

Typically, the default volume type is gp2. If you forgot to change the type to another, the volume is created in the default format, i.e. gp2. If you have few disks, you can check all disks through the console one by one, in all regions. However, for those who have a lot of disks and those who like automation, I have prepared the following tutorial. Thanks to it, you can make sure that you haven’t forgotten about an old gp2 disk in some region.

Lambda function

lambda find gp2
lambda – find gp2

If you like the lambda function, below you will find a code that will allow you to find all gp2 drives in all regions at once and write their ID. You receive a specific list with which you can do whatever you want.

import json
import boto3

ec2_client = boto3.client('ec2')
regions = [region['RegionName'] for region in ec2_client.describe_regions()['Regions']]
list_all_gp2_volumes = list()

volume_types = ['gp2']

def lambda_handler(event, context):
    
    for region in regions:
        ec2_client = boto3.client('ec2', region)
        gp2_volumes = ec2_client.describe_volumes(
            Filters=[
                {
                    'Name': 'volume-type',
                    'Values': volume_type
                },
            ]
        )
        if gp2_volumes:
            for gp2_volume in gp2_volumes['Volumes']:
                    print("Region: %s" % region)
                    print(gp2_volume['VolumeId'])
                    list_all_gp2_volumes.append(gp2_volume['VolumeId'])

    return list_all_gp2_volumes

Co ważne zmieniając parametr volume_type możesz poszukać także innych typów dysków. Możesz także dodać po przecinku kolejne typy. Dzięki temu za jednym uruchomieniem funkcji znajdziesz wszystkie dyski określonych typów we wszystkich regionach. Poniżej lista dostępnych typów dla EBS volumes:

Important. By changing the volume_type parameter, you can also search for other types of volumes. You can also add more types after a comma. This allows you to find all drives of the specified types in all regions with one run of the function. Below is a list of available types for EBS volumes:

  • gp2
  • gp3
  • io1
  • io2
  • st1
  • sc1
  • standard

Just remember to give your lambda function proper permissions. It’s best to create a dedicated role just for this purpose, with appropriate permissions, e.g.:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "ec2:Describe*",
            "Resource": "*"
        }
    ]
}

CLI – aws ec2 describe-volumes

cli find gp2
cli – find gp2

If you prefer to use the CLI, you can check what type of disks you have in a given region with a single command. The command shown below will only list the VolumeId of the gp2 disks from the eu-central-1 region.

aws ec2 describe-volumes \
    --region eu-central-1 \
    --filters Name=volume-type,Values=gp2 \
    --query "Volumes[*].{ID:VolumeId}"

Summary

I hope that the knowledge gained in this article will be useful to you. Thanks to it, you can improve your cloud, lower your bills and even increase your income. You can save real money by changing the drive type. The truth is that by increasing your knowledge, you increase your value on the Job market 🙂

More tips to automate your work, improve it and reduce the cost of using the cloud are coming soon. Also check out my YouTube channel for more cloud tips.

Amazon EBS Volumes and gp3 – you can save up to 20%
A beginner’s guide. How to start your adventure with the AWS cloud ??