cost explorer ec2 usage data filter conditions causing botocore error

77 views Asked by At

Ive been trying to get usage data for Ec2 instances for all user values tagged as 'User'. Idk what im doing wrong but i keep getting some type of botocore error. I can't figure out what is wrong with the filter. Appreciate any help

for value in tag_values:
    response = ce_client.get_cost_and_usage(
        TimePeriod={'Start': '2023-05-05', 'End': '2023-10-10'},
        Granularity='MONTHLY',
        Metrics=['UsageQuantity'],  # blendedcost
        Filter={
            'Tags': {
                'Key': 'User',
                'Values': [value]
            },
            'Dimensions': {'Key': 'SERVICE', 
             'Values': ['Amazon Elastic Compute Cloud - Compute']
             }
        }    
    )

error in this line->

response = ce_client.get_cost_and_usage(
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the GetCostAndUsage operation: Expression has more than one roots. Only one root operator is allowed for each expression: And, Or, Not, Dimensions, Tags, CostCategories
1

There are 1 answers

0
Paolo On

You need to combine your filters with an operator, e.g. using And:

ce_client = boto3.client("ce")
for value in tag_values:
    response = ce_client.get_cost_and_usage(
        TimePeriod={"Start": "2023-05-05", "End": "2023-10-10"},
        Granularity="MONTHLY",
        Metrics=["UsageQuantity"],  # blendedcost
        Filter={
            "And": [
                {
                    "Tags": {"Key": "User", "Values": [value]},
                },
                {
                    "Dimensions": {
                        "Key": "SERVICE",
                        "Values": ["Amazon Elastic Compute Cloud - Compute"],
                    },
                },
            ]
        },
    )