Skip to content

Instantly share code, notes, and snippets.

@tdmalone
Created December 28, 2023 04:39
Show Gist options
  • Select an option

  • Save tdmalone/5b1454cc00d6d14b7155a63b8c9fdd36 to your computer and use it in GitHub Desktop.

Select an option

Save tdmalone/5b1454cc00d6d14b7155a63b8c9fdd36 to your computer and use it in GitHub Desktop.

Revisions

  1. tdmalone created this gist Dec 28, 2023.
    116 changes: 116 additions & 0 deletions aws-services.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@
    #!/usr/bin/env python

    import json, sys
    from urllib.request import urlopen


    def green(str): return f'\033[92m{str}\033[0m'
    def orange(str): return f'\033[38;5;214m{str}\033[0m'
    def red(str): return f'\033[91m{str}\033[0m'
    def yellow(str): return f'\033[93m{str}\033[0m'


    def main():

    # Get command line argument if it exists, otherwise print an example of providing it.
    if len(sys.argv) < 2 or len(sys.argv) > 3 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
    print(f'This script lists all AWS services, with those available in a given region in '
    f'{green("green")} and\nthose not available in {red("red")}.\n\n'
    + yellow(f'$ {sys.argv[0]} eu-west-1\n'))
    print(f'When a second region is provided, services available in both regions are in '
    f'{green("green")}, those\nin one region in {orange("orange")}, and those in neither '
    f'region in {red("red")}.\n\n'
    + yellow(f'$ {sys.argv[0]} us-east-1 eu-west-1'))

    if len(sys.argv) < 2 or len(sys.argv) > 3:
    raise Exception

    return

    region1 = sys.argv[1]
    region2 = sys.argv[2] if len(sys.argv) == 3 else None

    # Get the AWS services JSON file.
    # NOTE: This is an undocumented API that is subject to change.
    url = 'https://api.regional-table.region-services.aws.a2z.com/index.json'
    response = json.load(urlopen(url))
    metadata = response['metadata']
    service_data = response['prices']

    all_services = []
    region1_services = []
    region2_services = []

    # Loop through the services and add them to the correct list.
    for service_instance in service_data:
    service_name = service_instance['attributes']['aws:serviceName']
    service_region = service_instance['attributes']['aws:region']

    service_name = service_name.replace('AWS ', '').replace('Amazon ', '')

    if service_name not in all_services:
    all_services.append(service_name)

    if service_region == region1 and service_name not in region1_services:
    region1_services.append(service_name)

    if region2 is not None:
    if service_region == region2 and service_name not in region2_services:
    region2_services.append(service_name)

    if len(region1_services) == 0:
    print(f'No services found in region {yellow(region1)}. Is the region name correct?')
    raise Exception

    if region2 is not None and len(region2_services) == 0:
    print(f'No services found in region {yellow(region2)}. Is the region name correct?')
    raise Exception

    # Sort services alphabetically.
    all_services.sort()

    # Single region mode.
    if region2 is None:
    print(f'Services available in {yellow(region1)} are shown in {green("green")}:\n')
    for service_name in all_services:
    if service_name in region1_services:
    print(green(service_name))
    else:
    print(red(service_name))

    print(f'\n{len(region1_services)} (out of {len(all_services)}) services available in '
    f'{yellow(region1)}.')
    print(f'Last updated {metadata["source:version"]}.')

    return

    # Multi region mode.
    print(f'Comparing services in {yellow(region1)} with {yellow(region2)}:')
    print(f'- Services available in both regions are in {green("green")}.')
    print(f'- Services available in only one region are in {orange("orange")}.')
    print(f'- Services not available in either region are in {red("red")}.\n')

    for service_name in all_services:
    if service_name in region1_services and service_name in region2_services:
    print(green(service_name))
    elif service_name in region1_services or service_name in region2_services:
    print(orange(service_name), end=' ')
    print(f'({region1} only)' if service_name in region1_services else
    f'({region2} only)')
    else:
    print(red(service_name))

    print()
    print(f'{len(region1_services)} (out of {len(all_services)}) services available in '
    f'{yellow(region1)}.')
    print(f'{len(region2_services)} (out of {len(all_services)}) services available in '
    f'{yellow(region2)}.')
    print(f'Last updated {metadata["source:version"]}.')


    try:
    main()
    except Exception:
    exit(1)

    exit(0)