def get_on_demand_price()

in k8s/pricing.py [0:0]


def get_on_demand_price(region, instance_type):
    """Get on-demand price for a specific instance type in a region."""
    pricing_client = boto3.client("pricing", region_name="us-east-1")

    try:
        response = pricing_client.get_products(
            ServiceCode="AmazonEC2",
            Filters=[
                {
                    "Type": "TERM_MATCH",
                    "Field": "instanceType",
                    "Value": instance_type,
                },
                {
                    "Type": "TERM_MATCH",
                    "Field": "operatingSystem",
                    "Value": "Linux",
                },
                {
                    "Type": "TERM_MATCH",
                    "Field": "preInstalledSw",
                    "Value": "NA",
                },
                {"Type": "TERM_MATCH", "Field": "tenancy", "Value": "Shared"},
                {
                    "Type": "TERM_MATCH",
                    "Field": "capacitystatus",
                    "Value": "Used",
                },
                {
                    "Type": "TERM_MATCH",
                    "Field": "location",
                    "Value": region_mapping[region],
                },
            ],
        )

        if response["PriceList"]:
            price_data = eval(response["PriceList"][0])
            terms = price_data["terms"]["OnDemand"]
            price_dimensions = next(iter(terms.values()))["priceDimensions"]
            price = next(iter(price_dimensions.values()))["pricePerUnit"][
                "USD"
            ]
            return float(price)
    except Exception as e:
        print(
            f"Error getting on-demand price for {instance_type} in {region}: {str(e)}"
        )
    return None