azureazure-resource-managercost-managementazure-monitor-workbooks

Azure Resource Manager Query with multiple dynamic tag filters


I'm trying to query the Azure Cost Management API and I want to be able to filter the results based off of 2 different types of resource tags but I am having trouble figuring out the format. I can get the single tag filter working, but I'm blanking on the format for multiple. Can anyone throw in their 2 cents?

Working single filter query:

{
    "type": "Usage",
    "timeframe": "{TimeFrame}",
    "dataset": {
        "granularity": "None",
        "filter": {
            "tags": {
                    "name": "Environment",
                    "operator": "In",
                    "values": [
                        {Environment}
                    ]
                }
        },
        "aggregation": {
            "totalCost": {
                "name": "PreTaxCost",
                "function": "Sum"
            }
        },
        "grouping": [
            {
                "type": "Dimension",
                "name": "{Aggregation}"
            }
        ]
    }
}

My attempt at adding more than one filter:

{
    "type": "Usage",
    "timeframe": "{TimeFrame}",
    "dataset": {
        "granularity": "None",
        "filter": {
            "tags": [
                {
                    "name": "Environment",
                    "operator": "In",
                    "values": [
                        {Environment}
                    ]
                },
                {
                    "name": "Location",
                    "operator": "In",
                    "values": [
                        {Location}
                    ]
                }
            ]
        },
        "aggregation": {
            "totalCost": {
                "name": "PreTaxCost",
                "function": "Sum"
            }
        },
        "grouping": [
            {
                "type": "Dimension",
                "name": "{Aggregation}"
            }
        ]
    }
}

I am very new to Azure so please don't roast me too hard lol.

Thank you to everyone who took a look at my question, much appreciated even if you don't have an answer for me.


Solution

  • There was an issue with the way my parameters were set causing a bad query. Here is the working code with multiple tag attributes for filtering:

    {
    "type": "Usage",
    "timeframe": "{TimeFrame}",
    "dataset": {
        "granularity": "None",
        "filter": {
            "and": [
                {
                    "tags": {
                        "name": "Location",
                        "operator": "In",
                        "values": [{LocationTag}]
                    }
                },
                {
                    "tags": {
                        "name": "Environment",
                        "operator": "In",
                        "Values": [{EnvironmentTag}]
                    }
                },
                {
                    "tags": {
                        "name": "Integrated-System",
                        "operator": "In",
                        "Values": [{IntegratedSystemTag}]
                    }
                }
            ]
        },
        "aggregation": {
            "totalCost": {
                "name": "PreTaxCost",
                "function": "Sum"
            }
        },
        "grouping": [
            {
                "type": "Dimension",
                "name": "{Aggregation}"
            }
        ]
    }
    

    }