def get_pricing()

in right_size_your_sagemaker_endpoints/load_test_helper.py [0:0]


def get_pricing(instance_type, duration='hour'):
    """
    Return current SageMaker pricing for a given instance.
    
    Using the AWS PriceList API, this function returns the current 
    pricing for a given instance type. This function is called to plot
    the performance vs. price plots.
    
    Inputs:
    instance_type: SageMaker instance type, e.g., ml.m5.xlarge
    duration: time for pricing - default at hour. Valid options are 
        hour, day or month. 
        
    Output:
    Price of the instance in USD.
    """
    
    flag = False
    
    # Validate 'duration'
    if duration not in ['hour', 'day', 'month']:
        print("Duration should be one of hour, day or month. Exiting..")
        return 0
    
    # Get pricing
    resp = pricing_client.get_products(
        ServiceCode='AmazonSageMaker',
        Filters=[
            {
                'Type': 'TERM_MATCH',
                'Field': 'instanceType',
                'Value': f"{instance_type}-Hosting"
            }
        ]
    )
    for price_list in resp['PriceList']:
        # Get pricing for us-east-1
        if json.loads(price_list)['product']['attributes']['location'] == "US East (N. Virginia)":
            x1 = json.loads(price_list)['terms']['OnDemand']
            x1_key1 = list(x1.keys())[0]
            x2 = x1[x1_key1]['priceDimensions']
            x2_key2 = list(x2.keys())[0]
            x3 = x2[x2_key2]['pricePerUnit']['USD']
            flag = True
    
    if flag == False:
        print("Could not find instance in us-east-1. Exiting..")
        return 0
    
    if duration=='hour':
        return round(float(x3), 3)
    elif duration=='day':
        return round(float(x3), 3) * 24
    elif duration=='month':
        return round(float(x3), 3) * 24 * 30