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

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

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

How to get the Region Name for AWS Price List API using Python and Boto3

When using the AWS Price List API, one of the most used attribute is the location attribute. This attribute is used to filter the region of your target resource.

Unfortunately, the location attribute is not in the region code that we use when programming in Python and Boto3, like us-east-1. Instead, it is descriptive like US East (N. Virginia). This is not readily available in Boto3 client or resource.

Below is the python function get_region_name, this converts the region code, eu-west-2, to the region name that AWS Price List API attribute location will accept, EU (London).

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


if __name__ == "__main__":
    region_code = 'ap-south-1'
    region_name = get_region_name(region_code)
    print(region_name)

    region_code = 'eu-west-2'
    region_name = get_region_name(region_code)
    print(region_name)

    region_code = 'us-east-2'
    region_name = get_region_name(region_code)
    print(region_name)

Output

Asia Pacific (Mumbai)
EU (London)
US East (Ohio)
Continue reading How to get the Region Name for AWS Price List API using Python and Boto3

What regions will the AWS Price List API work?

I tried accessing the AWS Price List API using the Singapore region (ap-southeast-1) but it resulted the error Could not connect to the endpoint URL: "https://api.pricing.ap-southeast-1.amazonaws.com/".

But whenever I use the N. Virginia Region (us-east-1) it works without any issues.

Continue reading What regions will the AWS Price List API work?

Amazon Transcribe AWS Price List API all Attribute Names and Values

Below is a reference for all attribute names and attribute values in AWS Price List API for Amazon Transcribe.

Note: All the list below are in alphabetical order.

Service Code: transcribe

Python Code to get Attribute Names for Amazon Transcribe using Boto3

import boto3

pricing_client = boto3.client('pricing', region_name='us-east-1')

response = pricing_client.describe_services(ServiceCode='transcribe')

attribute_names = response['Services'][0]['AttributeNames']

print(attribute_names)
Continue reading Amazon Transcribe AWS Price List API all Attribute Names and Values

Amazon Translate AWS Price List API all Attribute Names and Values

Below is a reference for all attribute names and attribute values in AWS Price List API for Amazon Translate.

Note: All the list below are in alphabetical order.

Service Code: translate

Python Code to get Attribute Names for Amazon Translate using Boto3

import boto3

pricing_client = boto3.client('pricing', region_name='us-east-1')

response = pricing_client.describe_services(ServiceCode='translate')

attribute_names = response['Services'][0]['AttributeNames']

print(attribute_names)
Continue reading Amazon Translate AWS Price List API all Attribute Names and Values

AWS VMware Cloud Price List API all Attribute Names and Values

Below is a reference for all attribute names and attribute values in AWS Price List API for VMware Cloud on AWS.

Note: All the list below are in alphabetical order.

Service Code: VMwareCloudOnAWS

Python Code to get Attribute Names for VMware Cloud on AWS using Boto3

import boto3

pricing_client = boto3.client('pricing', region_name='us-east-1')

response = pricing_client.describe_services(ServiceCode='VMwareCloudOnAWS')

attribute_names = response['Services'][0]['AttributeNames']

print(attribute_names)
Continue reading AWS VMware Cloud Price List API all Attribute Names and Values

AWS Snowball Extra Days Price List API all Attribute Names and Values

Below is a reference for all attribute names and attribute values in AWS Price List API for AWS Snowball Extra Days.

Note: All the list below are in alphabetical order.

Service Code: SnowballExtraDays

Python Code to get Attribute Names for AWS Snowball Extra Days using Boto3

import boto3

pricing_client = boto3.client('pricing', region_name='us-east-1')

response = pricing_client.describe_services(ServiceCode='SnowballExtraDays')

attribute_names = response['Services'][0]['AttributeNames']

print(attribute_names)
Continue reading AWS Snowball Extra Days Price List API all Attribute Names and Values

AWS OpsWorks Price List API all Attribute Names and Values

Below is a reference for all attribute names and attribute values in AWS Price List API for AWS OpsWorks.

Note: All the list below are in alphabetical order.

Service Code: OpsWorks

Python Code to get Attribute Names for AWS OpsWorks using Boto3

import boto3

pricing_client = boto3.client('pricing', region_name='us-east-1')

response = pricing_client.describe_services(ServiceCode='OpsWorks')

attribute_names = response['Services'][0]['AttributeNames']

print(attribute_names)
Continue reading AWS OpsWorks Price List API all Attribute Names and Values

Amazon Mobile Analytics AWS Price List API all Attribute Names and Values

Below is a reference for all attribute names and attribute values in AWS Price List API for Amazon Mobile Analytics.

Note:

  • All the list below are in alphabetical order.
  • I am not sure if Amazon Mobile Analytics here is the same as Amazon Pinpoint.

Service Code: mobileanalytics

Python Code to get Attribute Names for Amazon Mobile Analytics using Boto3

import boto3

pricing_client = boto3.client('pricing', region_name='us-east-1')

response = pricing_client.describe_services(ServiceCode='mobileanalytics')

attribute_names = response['Services'][0]['AttributeNames']

print(attribute_names)
Continue reading Amazon Mobile Analytics AWS Price List API all Attribute Names and Values

AWS IoT Device Management Price List API all Attribute Names and Values

Below is a reference for all attribute names and attribute values in AWS Price List API for AWS IoT Device Management.

Note: All the list below are in alphabetical order.

Service Code: IoTDeviceManagement

Python Code to get Attribute Names for AWS IoT Device Management using Boto3

import boto3

pricing_client = boto3.client('pricing', region_name='us-east-1')

response = pricing_client.describe_services(ServiceCode='IoTDeviceManagement')

attribute_names = response['Services'][0]['AttributeNames']

print(attribute_names)
Continue reading AWS IoT Device Management Price List API all Attribute Names and Values