Home > Software design >  Get Dimensions for USAGE_TYPE AWS Boto3 CostExplorer Client
Get Dimensions for USAGE_TYPE AWS Boto3 CostExplorer Client

Time:09-13

I'm trying to get Costs using CostExplorer Client in boto3. But I can't find the values to use as a Dimension filter. The documentation says that we can extract those values from GetDimensionValues but how do I use GetDimensionValues.

enter image description here

response = client.get_cost_and_usage(
        TimePeriod={
            'Start': str(start_time).split()[0],
            'End': str(end_time).split()[0]
        },
        Granularity='DAILY',
        Filter = {
            'Dimensions': {
                'Key':'USAGE_TYPE',
                'Values': [
                    'DataTransfer-In-Bytes'
                ]
            }
        },
        Metrics=[
        'NetUnblendedCost',
        ],
        GroupBy=[
            {
                'Type': 'DIMENSION',
                'Key': 'SERVICE'
            },
        ]
    )

CodePudding user response:

The boto3 reference for GetDimensionValues has a lot of details on how to use that call. Here's some sample code you might use to print out possible dimension values:

    response = client.get_dimension_values(
        TimePeriod={
            'Start': '2022-01-01',
            'End': '2022-06-01'
        },
        Dimension='USAGE_TYPE',
        Context='COST_AND_USAGE',
    )

    for dimension_value in response["DimensionValues"]:
        print(dimension_value["Value"])

Output:

APN1-Catalog-Request
APN1-DataTransfer-Out-Bytes
APN1-Requests-Tier1
APN2-Catalog-Request
APN2-DataTransfer-Out-Bytes
APN2-Requests-Tier1
APS1-Catalog-Request
APS1-DataTransfer-Out-Bytes
.....
  • Related