Skip to content

Commit

Permalink
fix: Check Ohio has a backup for certain instance types.
Browse files Browse the repository at this point in the history
  • Loading branch information
brookemckim committed Apr 30, 2024
1 parent 2e6d0bf commit 9ab540f
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,47 +100,49 @@ def get_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 returns the name without the .metal suffix
instance_type = instance_type + ".metal"

if instance_type in instances:
continue

new_inst = parse_instance(
instance_type, product_attributes, instance_types.get(instance_type)
)
# Not all instances are in US-EAST-1 any longer.
# Check Ohio as well.
for region in ["US East (Ohio)", "US East (N. Virginia)"]:
product_iterator = product_pager.paginate(
ServiceCode="AmazonEC2",
Filters=[
{
"Type": "TERM_MATCH",
"Field": "location",
"Value": region,
}
],
)
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 returns the name without the .metal suffix
instance_type = instance_type + ".metal"

if instance_type in instances:
continue

new_inst = parse_instance(
instance_type, product_attributes, instance_types.get(instance_type)
)

# Some instanced may be dedicated hosts instead
if new_inst is not None:
instances[instance_type] = new_inst
# Some instanced may be dedicated hosts instead
if new_inst is not None:
instances[instance_type] = new_inst

print(f"Found data for instance types: {', '.join(sorted(instances.keys()))}")
return list(instances.values())
Expand Down

0 comments on commit 9ab540f

Please sign in to comment.