You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Terraform的azurerm_subscription_cost_management_view添加过滤器?

为azurerm_subscription_cost_management_view添加过滤规则

要给Azure Cost Management视图添加过滤器,需在dataset块内部嵌套filter配置块(grouping仅用于数据分组,不具备过滤功能)。以下是具体实现示例:

基础过滤示例(按资源组过滤)

修改你的Terraform代码,新增filter块:

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"
    }

    # 新增过滤器:仅包含指定资源组的成本数据
    filter {
      dimension {
        name     = "ResourceGroupName"
        operator = "In"
        values   = ["rg-prod", "rg-staging"]
      }
    }
  }
  
  pivot {
    name = "ServiceName"
    type = "Dimension"
  }

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

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

其他过滤场景

按标签过滤

如果你需要按资源标签过滤,可使用tag类型的过滤规则:

filter {
  tag {
    name     = "Department"
    operator = "Equals"
    values   = ["Engineering"]
  }
}

多条件组合过滤

通过and/or块实现多条件过滤逻辑:

filter {
  and {
    # 条件1:服务名称包含"Virtual Machines"
    dimension {
      name     = "ServiceName"
      operator = "Contains"
      values   = ["Virtual Machines"]
    }

    # 条件2:环境标签为生产环境
    tag {
      name     = "Environment"
      operator = "Equals"
      values   = ["Production"]
    }
  }
}

支持的操作符

常用的过滤操作符包括:

  • Equals:精确匹配单个值
  • In:匹配多个值中的任意一个
  • Contains:值包含指定字符串
  • NotEquals:排除指定值
  • NotIn:排除多个值

内容的提问来源于stack exchange,提问作者Tony

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 22:20:09