|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +EC2 Pricing Map Generator |
| 4 | +
|
| 5 | +Get pricing info for EC2 instances by reading .github/scale-config.yml and |
| 6 | +fetching current AWS pricing data. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +from functools import lru_cache |
| 11 | +from typing import Optional |
| 12 | + |
| 13 | +import requests |
| 14 | +import yaml |
| 15 | + |
| 16 | + |
| 17 | +@lru_cache |
| 18 | +def _get_scale_config() -> dict: |
| 19 | + """Load scale-config.yml and return as a dictionary.""" |
| 20 | + with open(".github/scale-config.yml", "r") as f: |
| 21 | + config = yaml.safe_load(f) |
| 22 | + return config |
| 23 | + |
| 24 | + |
| 25 | +def get_ec2_instance_for_label(label: str) -> dict[str, Optional[str]]: |
| 26 | + """Get EC2 instance type for a given GitHub Actions runner label from scale-config.yml.""" |
| 27 | + config = _get_scale_config() |
| 28 | + |
| 29 | + runner_info = config.get("runner_types", {}) |
| 30 | + |
| 31 | + if label in runner_info: |
| 32 | + return { |
| 33 | + "ec2_instance": runner_info[label].get("instance_type", None), |
| 34 | + "os": runner_info[label].get("os", "linux"), |
| 35 | + } # Default to linux if not specified |
| 36 | + return {"ec2_instance": None, "os": None} |
| 37 | + |
| 38 | + |
| 39 | +@lru_cache |
| 40 | +def get_all_pricing_data() -> dict: |
| 41 | + """Fetch the entire EC2 pricing data from AWS pricing API. Cached for efficiency.""" |
| 42 | + price_list_url = "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/us-east-1/index.json" |
| 43 | + response = requests.get(price_list_url) |
| 44 | + response.raise_for_status() |
| 45 | + return response.json() |
| 46 | + |
| 47 | + |
| 48 | +@lru_cache |
| 49 | +def get_price_for_ec2_instance(instance_type, os_type="linux") -> Optional[float]: |
| 50 | + """Fetch on-demand price for EC2 instance type using AWS public pricing data. Returns None if not found.""" |
| 51 | + |
| 52 | + # Map os_type to AWS pricing API values |
| 53 | + operating_system = "Windows" if os_type.lower() == "windows" else "Linux" |
| 54 | + |
| 55 | + # Get the cached pricing data |
| 56 | + pricing_data = get_all_pricing_data() |
| 57 | + |
| 58 | + # Search through the products to find matching instance |
| 59 | + for product_sku, product_data in pricing_data.get("products", {}).items(): |
| 60 | + attributes = product_data.get("attributes", {}) |
| 61 | + |
| 62 | + if ( |
| 63 | + attributes.get("instanceType") == instance_type |
| 64 | + and attributes.get("location") == "US East (N. Virginia)" |
| 65 | + and attributes.get("operatingSystem") == operating_system |
| 66 | + and attributes.get("preInstalledSw") == "NA" |
| 67 | + and attributes.get("tenancy") == "Shared" |
| 68 | + and attributes.get("usagetype", "").startswith("BoxUsage") |
| 69 | + ): |
| 70 | + # Found the product, now get the pricing terms |
| 71 | + terms = ( |
| 72 | + pricing_data.get("terms", {}).get("OnDemand", {}).get(product_sku, {}) |
| 73 | + ) |
| 74 | + |
| 75 | + for term_data in terms.values(): |
| 76 | + price_dimensions = term_data.get("priceDimensions", {}) |
| 77 | + for price_data in price_dimensions.values(): |
| 78 | + price_per_unit = price_data.get("pricePerUnit", {}).get("USD") |
| 79 | + if price_per_unit: |
| 80 | + return float(price_per_unit) |
| 81 | + |
| 82 | + print(f"No pricing found for {instance_type} ({operating_system})") |
| 83 | + return None |
| 84 | + |
| 85 | + |
| 86 | +@lru_cache |
| 87 | +def get_price_for_label(label: str) -> Optional[float]: |
| 88 | + """Get the on-demand price for the EC2 instance type associated with the given GitHub Actions runner label.""" |
| 89 | + instance_info = get_ec2_instance_for_label(label) |
| 90 | + instance_type = instance_info["ec2_instance"] |
| 91 | + os_type = instance_info["os"] |
| 92 | + if instance_type is not None: |
| 93 | + return get_price_for_ec2_instance(instance_type, os_type) |
| 94 | + return None |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + # Example usage |
| 99 | + info = [] |
| 100 | + scale_config = _get_scale_config() |
| 101 | + for runner_label in scale_config.get("runner_types", {}): |
| 102 | + price = get_price_for_label(runner_label) |
| 103 | + info.append( |
| 104 | + { |
| 105 | + "label": runner_label, |
| 106 | + "price_per_hour": price, |
| 107 | + "instance_type": get_ec2_instance_for_label(runner_label)[ |
| 108 | + "ec2_instance" |
| 109 | + ], |
| 110 | + } |
| 111 | + ) |
| 112 | + with open("ec2_pricing.json", "w") as f: |
| 113 | + for line in info: |
| 114 | + json.dump(line, f) |
| 115 | + f.write("\n") |
0 commit comments