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

如何通过Terraform调用REST API创建Azure成本管理视图及邮件订阅

Terraform实现Azure成本分析视图与定时邮件订阅

方案说明

通过Terraform的AzAPI Provider直接调用Azure Cost Management的REST API,完成成本分析视图创建和定时邮件订阅配置,以下是完整实现代码:

1. 配置Providers

先配置AzureRM和AzAPI Provider,确保认证正常:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 3.0.0"
    }
    azapi = {
      source  = "azure/azapi"
      version = ">= 1.0.0"
    }
  }
}

provider "azurerm" {
  features {}
}

provider "azapi" {
  # 继承azurerm的认证配置,无需额外配置
}

2. 创建成本分析视图

调用Cost Management视图的CreateOrUpdate API,自定义视图的维度、指标等参数:

resource "azapi_resource" "cost_analysis_view" {
  type      = "Microsoft.CostManagement/views@2023-11-01"
  name      = "weekly-cost-view" # 自定义视图名称
  parent_id = "/providers/Microsoft.Billing/billingAccounts/{BillingAccountID}" # 替换为你的计费账户ID

  body = jsonencode({
    properties = {
      displayName = "每周成本分析视图"
      scope       = "/providers/Microsoft.Billing/billingAccounts/{BillingAccountID}"
      query = {
        type = "Usage"
        timeframe = "WeekToDate"
        dataset = {
          granularity = "Daily"
          aggregation = {
            totalCost = {
              name = "PreTaxCost"
              function = "Sum"
            }
          }
          grouping = [
            {
              type = "Dimension"
              name = "ResourceGroup"
            },
            {
              type = "Dimension"
              name = "ResourceName"
            }
          ]
        }
      }
      accumulated = false
      chart       = "Table"
    }
  })
}

3. 创建定时邮件订阅

基于上面创建的视图,配置每周定时发送成本分析邮件:

resource "azapi_resource" "cost_scheduled_email" {
  type      = "Microsoft.CostManagement/scheduledActions@2023-11-01"
  name      = "weekly-cost-email-alert" # 自定义订阅名称
  parent_id = "/providers/Microsoft.Billing/billingAccounts/{BillingAccountID}" # 替换为你的计费账户ID
  depends_on = [azapi_resource.cost_analysis_view]

  body = jsonencode({
    kind = "Email"
    properties = {
      displayName = "每周成本报告邮件"
      status      = "Enabled"
      viewId      = azapi_resource.cost_analysis_view.id
      schedule = {
        frequency   = "Weekly"
        startDate   = "2024-01-01T08:00:00.000Z" # 自定义开始时间(UTC时区)
        endDate     = "2025-01-01T08:00:00.000Z" # 自定义结束时间(UTC时区)
        daysOfWeek  = ["Monday"] # 自定义发送周几
      }
      notification = {
        to      = ["your-email@example.com"] # 替换为收件邮箱
        subject = "每周Azure成本分析报告"
        message = "附件为最新的Azure成本分析数据,请查收。"
      }
      fileDestination = {
        fileFormats = ["Csv"]
      }
      scope = "/providers/Microsoft.Billing/billingAccounts/{BillingAccountID}"
    }
  })
}

关键注意事项

  • 替换所有{BillingAccountID}为实际的Azure计费账户ID
  • 调整视图的query参数以匹配你的成本分析需求(比如时间范围、聚合维度等)
  • 邮件订阅的startDate必须是未来时间,且采用UTC时区格式
  • 确保执行Terraform的账号拥有CostManagementReader或更高权限(如CostManagementContributor)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 19:50:29