Direct Connect and VLANs

We were troubleshooting a network configuration on using AWS Direct Connect and as it turns out that the VLAN can be different coming from the Customer Gateway to the Customer POP Router and Customer POP Router to Public/Private Virtual Interface (VIF).

Continue reading Direct Connect and VLANs

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

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

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

Hosted Public VIFs and LOA for Public IP and ASN

Let’s say there are 2 companies. Each company has an AWS Account of their.

Company A account owns the Direct Connect connection. They then created a Hosted Public Virtual Interface (VIF) to Company B account.

Company B account will then accept the Hosted Public VIF from Company A account.

Company B owns the public IP prefix and public ASN that will be used for the Hosted Public VIF.

After 72 hours, the Hosted Public VIF is still in “Verifying” state.

Which account should the Letter of Authorization (LOA) for Company B’s Public IP prefix and public ASN be submitted? Should it be Company A’s account or Company B’s account?

Continue reading Hosted Public VIFs and LOA for Public IP and ASN

Public Virtual Interface (VIF) only advertising the AWS Prefixes on the same region

We have a Direct Connect (DX) connection in Mumbai Region and we need to access an S3 bucket or an EC2 Instance with Elastic IP in N. Virginia region. For this we will need to use Public Virtual Interface (VIF).

When we setup the Public VIF and configured the router in the Corporate Data Center, we cannot route traffic to any resources in the N. Virginia Region.

Is it possible to route traffic from a region where my Direct Connect is to AWS resources in another region using Public Virtual Interface (VIF)?

Continue reading Public Virtual Interface (VIF) only advertising the AWS Prefixes on the same region

How to get the On-Demand price of EC2 Instances using Python and Boto3

The Python code below gets the on-demand price (in USD) of the EC2 instance type t2.micro in the Asia Pacific (Mumbai) region with Linux operating system.

Note: There are more examples below where we get the hourly price of different on-demand EC2 Instances like other operating system or EC2 Instances that are running on Dedicated Instances or Dedicated Hosts.

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_ec2_instance_hourly_price(region_code, 
                                  instance_type, 
                                  operating_system, 
                                  preinstalled_software='NA', 
                                  tenancy='Shared', 
                                  is_byol=False):
                                       
    region_name = get_region_name(region_code)

    if is_byol:
        license_model = 'Bring your own license'
    else:
        license_model = 'No License required'

    if tenancy == 'Host':
        capacity_status = 'AllocatedHost'
    else:
        capacity_status = 'Used'
    
    filters = [
        {'Type': 'TERM_MATCH', 'Field': 'termType', 'Value': 'OnDemand'},
        {'Type': 'TERM_MATCH', 'Field': 'capacitystatus', 'Value': capacity_status},
        {'Type': 'TERM_MATCH', 'Field': 'location', 'Value': region_name},
        {'Type': 'TERM_MATCH', 'Field': 'instanceType', 'Value': instance_type},
        {'Type': 'TERM_MATCH', 'Field': 'tenancy', 'Value': tenancy},
        {'Type': 'TERM_MATCH', 'Field': 'operatingSystem', 'Value': operating_system},
        {'Type': 'TERM_MATCH', 'Field': 'preInstalledSw', 'Value': preinstalled_software},
        {'Type': 'TERM_MATCH', 'Field': 'licenseModel', 'Value': license_model},
    ]

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

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

        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-south-1'
    instance_type = 't2.micro'
    operating_system = 'Linux'


    ec2_instance_price = get_ec2_instance_hourly_price(
        region_code=region_code, 
        instance_type=instance_type, 
        operating_system=operating_system,
    )

    print(instance_type, operating_system, region_code)
    print(ec2_instance_price)

Output

Continue reading How to get the On-Demand price of EC2 Instances using Python and Boto3

AWS Direct Connect connection Status still ‘available’ when there is an issue with our On-Premise Router

When we were setting up a Direct Connect I was wondering why the Direct Connect connection has state of ‘available’ but the router in the Corporate Data Center was off.

Continue reading AWS Direct Connect connection Status still ‘available’ when there is an issue with our On-Premise Router

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

AWS Direct Connect Virtual Network Interface BGP Status Delay

When we were troubleshooting an issue with the BGP status of a Private Virtual Interface (VIF), we encountered that the BGP status in the router was up and we are able to communicate with our resources in AWS while in the AWS console, it was still down.

After some time, the BGP status of our Private Virtual Interface was also changed to up.

Continue reading AWS Direct Connect Virtual Network Interface BGP Status Delay

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