How to get the price of AWS Load Balancers using Boto3 and Python


Last Updated on May 15, 2021

Below is a Python Script that gets the price of AWS Elastic Load Balancers (ELB). This can retrieve prices for Classic, Application, Network and Gateway Load Balancers. It can also get the price of load balancers in AWS Outposts. See the different examples of how to use this Python code below.

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_load_balancer_price(region_code, lb_product_family, is_outposts=False):

    price_details = {}

    region_name = get_region_name(region_code)

    if is_outposts:
        location_type = 'AWS Outposts'
    else:
        location_type = 'AWS Region'

    filters = [
        {'Type': 'TERM_MATCH', 'Field': 'locationType', 'Value': location_type},
        {'Type': 'TERM_MATCH', 'Field': 'location', 'Value': region_name},
        {'Type': 'TERM_MATCH', 'Field': 'productFamily', 'Value': lb_product_family},
    ]

    pricing_client = boto3.client('pricing', region_name='us-east-1')
    
    response = pricing_client.get_products(ServiceCode='AWSELB', Filters=filters)

    if len(response['PriceList']) == 0:
        return None

    for price in response['PriceList']:
        price = json.loads(price)

        usage_type = price['product']['attributes']['usagetype']

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

                if 'LoadBalancerUsage' in usage_type:
                    price_details['LoadBalancerUsage'] = price_value
                elif 'DataProcessing-Bytes' in usage_type:
                    price_details['DataProcessing-Bytes'] = price_value
                elif 'LCUUsage' in usage_type:
                    price_details['LCUUsage'] = price_value
            
    return price_details


if __name__ == "__main__":
    region_code = 'ap-south-1'
    lb_product_family = 'Load Balancer'    # Classic Load Balancer
    
    price_details = get_load_balancer_price(region_code, lb_product_family)

    print(region_code, lb_product_family)
    print(price_details)

Output

ap-south-1 Load Balancer
{'LoadBalancerUsage': 0.0266, 'DataProcessing-Bytes': 0.008}

You can check if the price is correct in the AWS Load Balancer Pricing page.



Code Explained

The get_load_balancer_price accepts three parameters – region_code, lb_product_family and is_outposts.

region_code (string) is the target region for the price of the load balancers. It’s in the format of region codes like ap-south-1, us-east-1, eu-west-1.

lb_product_family (string) is the product family attribute in the AWS Price List API for Elastic Load Balancing (ELB).

The values for lb_product_family are

  • Load Balancer for Classic Load Balancers
  • Load Balancer-Application for Application Load Balancers
  • Load Balancer-Network for Network Load Balancers
  • Load Balancer-Gateway for Gateway Load Balancer

is_outposts (boolean) is an optional parameter. If this is not set, by default this is set to False or the load balancer price being retrieved is in an AWS region, not in AWS Outposts. If this is set to True, then it will retrieve the price in AWS Outposts.

The output of the get_load_balancer_price function is the price_details dictionary.

Depending on the type of Load Balancer it will have different key-value pairs.

For Classic Load Balancer price_details will have the following keys.

  • LoadBalancerUsage the hourly price of Classic Load Balancer
  • DataProcessing-Bytes the per GB price of data processed

For new generation of Load Balancers like Application, Network and Gateway Load Balancers, including the ones in AWS Outposts, price_details will have the following keys.

  • LoadBalancerUsage the hourly price of the load balancers
  • LCUUsage the LCU-hour price (or partial hour)

For more information on LCU Usage please visit the Elastic Load Balancer pricing page.


Examples on how to use the Python Script to retrieve Elastic Load Balancer (ELB) prices

On the examples below we will only use the main part of the python script to show how to use the get_load_balancer_price for different types of AWS Load Balancers.

Classic Load Balancer

For Classic Load Balancers the lb_product_family must be set to Load Balancer.

if __name__ == "__main__":
    region_code = 'us-east-1'
    lb_product_family = 'Load Balancer'    # Classic Load Balancer
    
    price_details = get_load_balancer_price(region_code, lb_product_family)

    print(region_code, lb_product_family)
    print(price_details)

Output

us-east-1 Load Balancer
{'LoadBalancerUsage': 0.025, 'DataProcessing-Bytes': 0.008}

Application Load Balancer

For Application Load Balancers the lb_product_family must be set to Load Balancer-Application.

if __name__ == "__main__":
    region_code = 'eu-south-1'
    lb_product_family = 'Load Balancer-Application'   # Application Load Balancer
    
    price_details = get_load_balancer_price(region_code, lb_product_family)

    print(region_code, lb_product_family)
    print(price_details)

Output

eu-south-1 Load Balancer-Application
{'LoadBalancerUsage': 0.0265, 'LCUUsage': 0.0084}

Network Load Balancer

For Network Load Balancers the lb_product_family must be set to Load Balancer-Network.

if __name__ == "__main__":
    region_code = 'me-south-1'
    lb_product_family = 'Load Balancer-Network'   # Network Load Balancer
    
    price_details = get_load_balancer_price(region_code, lb_product_family)

    print(region_code, lb_product_family)
    print(price_details)

Output

me-south-1 Load Balancer-Network
{'LoadBalancerUsage': 0.02772, 'LCUUsage': 0.0066}

Gateway Load Balancer

For Gateway Load Balancers the lb_product_family must be set to Load Balancer-Gateway.

if __name__ == "__main__":
    region_code = 'ap-northeast-2'
    lb_product_family = 'Load Balancer-Gateway'   # Gateway Load Balancer
    
    price_details = get_load_balancer_price(region_code, lb_product_family)

    print(region_code, lb_product_family)
    print(price_details)

Output

ap-northeast-2 Load Balancer-Gateway
{'LCUUsage': 0.004, 'LoadBalancerUsage': 0.0125}

AWS Outposts Application Load Balancers

For Load Balancers that are in AWS Outposts, the lb_product_family will still be the same depending on your target type of Load Balancer.

This time, we have to set the is_outposts parameter to True.

if __name__ == "__main__":
    region_code = 'ap-southeast-1'
    lb_product_family = 'Load Balancer-Application'   # Application Load Balancer
    is_outposts = True
    
    price_details = get_load_balancer_price(region_code, lb_product_family, is_outposts)

    print(region_code, lb_product_family)
    print(price_details)

Output

ap-southeast-1 Load Balancer-Application
{'LCUUsage': 0.0, 'LoadBalancerUsage': 0.0252}

Listing Load Balancer Price for All Regions

Below is a code to get the price of Application Load Balancers in the all AWS regions. It uses the listing of AWS regions using Boto3.

if __name__ == "__main__":

    ec2_client = boto3.client('ec2')

    response = ec2_client.describe_regions(AllRegions=True)

    regions = response['Regions']

    region_code_list = []

    for region in regions:
        region_code = region['RegionName']
        region_code_list.append(region_code)

    region_code_list = sorted(region_code_list)

    for region_code in region_code_list:
        lb_product_family = 'Load Balancer-Application'   # Application Load Balancer
        
        price_details = get_load_balancer_price(region_code, lb_product_family)

        load_balancer_usage_price = price_details['LoadBalancerUsage']
        lcu_usage_price = price_details['LCUUsage']

        print(load_balancer_usage_price, '\t', lcu_usage_price, '\t', lb_product_family, '\t', region_code, '\t', get_region_name(region_code))

Output. Fixed the formatting for easier reading.

0.03     0.0095  Load Balancer-Application    af-south-1      Africa (Cape Town)
0.0277   0.0088  Load Balancer-Application    ap-east-1       Asia Pacific (Hong Kong)
0.0243   0.008   Load Balancer-Application    ap-northeast-1  Asia Pacific (Tokyo)
0.0225   0.008   Load Balancer-Application    ap-northeast-2  Asia Pacific (Seoul)
0.0243   0.008   Load Balancer-Application    ap-northeast-3  Asia Pacific (Osaka)
0.0239   0.008   Load Balancer-Application    ap-south-1      Asia Pacific (Mumbai)
0.0252   0.008   Load Balancer-Application    ap-southeast-1  Asia Pacific (Singapore)
0.0252   0.008   Load Balancer-Application    ap-southeast-2  Asia Pacific (Sydney)
0.02475  0.0088  Load Balancer-Application    ca-central-1    Canada (Central)
0.027    0.008   Load Balancer-Application    eu-central-1    EU (Frankfurt)
0.02394  0.0076  Load Balancer-Application    eu-north-1      EU (Stockholm)
0.0265   0.0084  Load Balancer-Application    eu-south-1      EU (Milan)
0.0252   0.008   Load Balancer-Application    eu-west-1       EU (Ireland)
0.02646  0.0084  Load Balancer-Application    eu-west-2       EU (London)
0.02646  0.0084  Load Balancer-Application    eu-west-3       EU (Paris)
0.02772  0.0088  Load Balancer-Application    me-south-1      Middle East (Bahrain)
0.034    0.011   Load Balancer-Application    sa-east-1       South America (Sao Paulo)
0.0225   0.008   Load Balancer-Application    us-east-1       US East (N. Virginia)
0.0225   0.008   Load Balancer-Application    us-east-2       US East (Ohio)
0.0252   0.008   Load Balancer-Application    us-west-1       US West (N. California)
0.0225   0.008   Load Balancer-Application    us-west-2       US West (Oregon)

First column is the Load Balancer Usage price.

Second column is the LCU Usage price.


I hope the above helps in getting the price of AWS Elastic Load Balancers (ELB).


Leave a Reply

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