How to get the price of EBS Snapshots using Boto3 and Python


Last Updated on May 2, 2021

The code below gets the price of an EBS Snapshots given the AWS Region using Python and Boto3.

import boto3
import json
from pkg_resources import resource_filename

def get_region_name(region_code):

    endpoint_file = resource_filename('botocore', 'data/endpoints.json')

    with open(endpoint_file, 'r') as f:
        endpoint_data = json.load(f)

    region_name = endpoint_data['partitions'][0]['regions'][region_code]['description']

    region_name = region_name.replace('Europe', 'EU')

    return region_name


def get_ebs_snapshot_price(region_code):

    region_name = get_region_name(region_code)
    
    filters = [
        {'Type': 'TERM_MATCH', 'Field': 'productFamily', 'Value': "Storage Snapshot"},
        {'Type': 'TERM_MATCH', 'Field': 'location', 'Value': region_name},
        {'Type': 'TERM_MATCH', 'Field': 'storageMedia', 'Value': 'Amazon S3'}
    ]

    pricing_client = boto3.client('pricing', region_name='ap-south-1')
    
    response = pricing_client.get_products(ServiceCode='AmazonEC2', Filters=filters)

    if len(response['PriceList']) > 0:
        price = json.loads(response['PriceList'][0])

        for on_demand in price['terms']['OnDemand'].values():
            for price_dimensions in on_demand['priceDimensions'].values():
                price_value = price_dimensions['pricePerUnit']['USD']
                
        return float(price_value)
    return None


if __name__ == "__main__":
    region_code = 'ap-southeast-1'
    snapshot_price = get_ebs_snapshot_price(region_code)
    print(snapshot_price, '\t', region_code)

    region_code = 'sa-east-1'
    snapshot_price = get_ebs_snapshot_price(region_code)
    print(snapshot_price, '\t', region_code)

    region_code = 'eu-west-1'
    snapshot_price = get_ebs_snapshot_price(region_code)
    print(snapshot_price, '\t', region_code)

Output

0.05     ap-southeast-1
0.068    sa-east-1
0.05     eu-west-1

Listing All the Price of EBS Snapshots for All Regions

To list all the price of EBS Snapshots for all AWS Region using Python and Boto3, replace the if name == "main": part of the code with the one below.

if __name__ == "__main__":
    ec2_client = boto3.client('ec2')

    response = ec2_client.describe_regions(AllRegions=True)

    regions = response['Regions']

    for region in regions:
        region_code = region['RegionName']
        snapshot_price = get_ebs_snapshot_price(region_code)
        
        print(snapshot_price, '\t', region_code, '\t', get_region_name(region_code))

Output

0.0595   af-south-1      Africa (Cape Town)
0.0475   eu-north-1      EU (Stockholm)
0.05     ap-south-1      Asia Pacific (Mumbai)
0.053    eu-west-3       EU (Paris)
0.053    eu-west-2       EU (London)
0.0525   eu-south-1      EU (Milan)
0.05     eu-west-1       EU (Ireland)
0.05     ap-northeast-3  Asia Pacific (Osaka)
0.05     ap-northeast-2  Asia Pacific (Seoul)
0.055    me-south-1      Middle East (Bahrain)
0.05     ap-northeast-1  Asia Pacific (Tokyo)
0.068    sa-east-1       South America (Sao Paulo)
0.055    ca-central-1    Canada (Central)
0.055    ap-east-1       Asia Pacific (Hong Kong)
0.05     ap-southeast-1  Asia Pacific (Singapore)
0.055    ap-southeast-2  Asia Pacific (Sydney)
0.054    eu-central-1    EU (Frankfurt)
0.05     us-east-1       US East (N. Virginia)
0.05     us-east-2       US East (Ohio)
0.055    us-west-1       US West (N. California)
0.05     us-west-2       US West (Oregon)

To check if the price are correct, visit the Amazon EBS pricing page then change the region to your desired AWS region and check the Amazon EBS Snapshots section.


Leave a Reply

Your email address will not be published. Required fields are marked *