Skip to content

Instantly share code, notes, and snippets.

@ScriptAutomate
Created August 30, 2019 19:18
Show Gist options
  • Save ScriptAutomate/80824527e72272f771dce2188f85f627 to your computer and use it in GitHub Desktop.
Save ScriptAutomate/80824527e72272f771dce2188f85f627 to your computer and use it in GitHub Desktop.
[Python 3] Boto3 and Webscrape for List of AWS Instances Based on The Nitro System
import requests
import re
import json
import boto3
def get_all_ec2_types():
'''
Modified version of get_instances() from
https://github.com/powdahound/ec2instances.info/blob/master/ec2.py
AWS provides no simple API for retrieving all instance types
'''
all_instances = []
pricing_client = boto3.client('pricing', region_name='us-east-1')
product_pager = pricing_client.get_paginator('get_products')
product_iterator = product_pager.paginate(
ServiceCode='AmazonEC2', Filters=[
# We're gonna assume N. Virginia has all the available types
{'Type': 'TERM_MATCH', 'Field': 'location',
'Value': 'US East (N. Virginia)'},
]
)
for product_item in product_iterator:
for offer_string in product_item.get('PriceList'):
offer = json.loads(offer_string)
product = offer.get('product')
# Check if it's an instance
if product.get('productFamily') not in ['Compute Instance', 'Compute Instance (bare metal)', 'Dedicated Host']:
continue
product_attributes = product.get('attributes')
instance_type = product_attributes.get('instanceType')
if instance_type in ['u-6tb1', 'u-9tb1', 'u-12tb1']:
# API return the name without the .metal part
instance_type = instance_type + '.metal'
all_instances.append(instance_type)
return set(all_instances)
def get_nitro_prefix_list():
'''
Webscrape of latest AWS docs source seems to be only
way to retrieve a list of Nitro system instance types.
WARNING: Will break if formatting of target page is
changed enough to break the prefix_list string parse
'''
scrape_raw = requests.get(
'https://raw.githubusercontent.com/awsdocs/amazon-ec2-user-guide/master/doc_source/instance-types.md')
prefix_list = re.sub(
r"(\n)|(and)|(Bare metal:)|`|\ |\+",
"",
scrape_raw.text.split(
"based on the Nitro system:\n+ ")[1].split("\n\n**Resources")[0]
).split(',')
return prefix_list
def filter_nitro_ec2_types(ec2_instances, prefix_list):
nitro_types = []
for nitro in prefix_list:
if '.' in nitro:
if nitro.lower() in ec2_instances:
nitro_types.append(nitro)
else:
for instance_type in ec2_instances:
if f"{nitro.lower()}." in instance_type:
nitro_types.append(instance_type)
return nitro_types
def get_all_nitro():
return filter_nitro_ec2_types(
get_all_ec2_types(),
get_nitro_prefix_list()
)
if __name__ == "__main__":
for instance_type in get_all_nitro():
print(instance_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment