Adding filter to azurerm_subscription_cost_management_view

158 views Asked by At

I cannot find a way to add a filter creating a view using azurerm_subscription_cost_management_view via Terraform

I tried using the dataset grouping . But it's a way for grouping, not filtering.

resource "azurerm_subscription_cost_management_view" "example" {
  name            = "TerraformCostView"
  display_name    = "Terraform - Cost View"
  subscription_id = "/subscriptions/${var.subscription_id}"
  chart_type      = "Area"
  accumulated     = true

  report_type = "Usage"
  timeframe   = "MonthToDate"

  dataset {
    granularity = "Monthly"

    aggregation {
      name        = "totalCost"
      column_name = "Cost"
    }

    #grouping {
    #  name  = "department"
    #  type  = "TagKey"
    #}
  }
  
  pivot {
    name = "ServiceName"
    type = "Dimension"
  }

  pivot {
    name = "ResourceType"
    type = "Dimension"
  }

  pivot {
    name = "ResourceGroupName"
    type = "Dimension"
  }
}

How can I add a filter to the view?

Thanks!

1

There are 1 answers

0
Tony On BEST ANSWER

Answering mysef. It can be done using azapi_resource. Example:

resource "azapi_resource" "cost_view" {
  name      = "Terraform-example-cost-view"
  parent_id = "/subscriptions/${var.subscription_id}"
  type      = "Microsoft.CostManagement/views@2019-11-01"
    
  body      = jsonencode({
    eTag          = "\"your_etag_value\""
    properties  = {
      displayName = "Terraform-example-cost-view"
      query       = {
        type      = "Usage"
        timeframe = "MonthToDate"
        dataSet   = {
          granularity = "Daily"
          aggregation = {
            totalCost = {
              name     = "PreTaxCost"
              function = "Sum"
            }
          }
          grouping = [
            {
              type = "Dimension"
              name = "ResourceGroupName"
            }
          ]
                    filter = {
                        and = [
                            {
                                tags = {
                                    name = "your_tag_name1"
                                    operator = "In"
                                    values = ["your_tag_value1"]
                                }
                            },
                            {
                                tags = {
                                    name = "your_tag_name2"
                                    operator = "In"
                                    values = ["your_tag_value2"]
                                }
                            }
                        ]

                    }
        }
      }
            pivots = [
                {
                    name = "Benefit"
                    type = "Dimension"
                },{
                    name = "Location"
                    type = "Dimension"
                },{
                    name = "Meter"
                    type = "Dimension"
                }
            ]
    }
  })
}