如何使用 Terraform 更新 Azure 订阅标签?

问题描述 投票:0回答:1

我正在尝试在订阅级别(而不是资源或资源组)管理 Azure 标签。我们正在使用 Terraform

not
创建订阅,但我们不能。创建订阅后,我们为该订阅创建配置模板。在那里,我们管理访问控制和所有权记录等内容。这是订阅的标准配置模板的样子。

module "subscription-project-pegasus" {
  source                     = "./modules/subscription/"

  subscription_access = [
    {
      aad_group = "all-employees-group"
      role      = "Reader"
    },
    {
      spn = "spn-pegasus"
      role = "Contributor"
    }
  ]

  ownership = {
    team = "Team Pegasus",
    pagerduty_id = "pegasus"
  }
}

我想在此处添加另一个对象

tags
我可以在订阅级别应用它。

tags = {
    owner = "pegasus"
    environment = "nonprod"
  }

该自定义模块

./modules/subscriptions
使用
azurerm_role_assignment
模块来配置角色分配。但是,除了主要用于提供订阅的主要
azurerm_subscription
模块之外,我找不到可以进行标记的模块。如果我使用它,我相信 Terraform 将开始跟踪订阅的状态,但这并不理想。

我正在寻找一种在订阅级别添加这些标签而不使用

azurerm_subscription
模块的方法。请指教!

azure terraform tags terraform-provider-azure azure-subscription
1个回答
0
投票

我尝试使用 Terraform 添加/更新 Azure 订阅标签,而不使用

azurerm_subscription
模块,并成功配置了要求。

如果由于其状态跟踪而希望在不使用

azurerm_subscription
模块的情况下管理订阅级别的标签,则可以使用 local-exec 配置程序来运行 Azure CLI 命令以将标签添加到订阅中来实现此目的。

我的地形配置

main.tf

variable "subscription_id" {
  description = "The ID of the Azure subscription."
  type        = string
}

variable "tags" {
  description = "A map of tags to assign to the resource."
  type        = map(string)
  default     = {}
}


resource "null_resource" "subscription_tags" {
  provisioner "local-exec" {
    command = <<EOT
      az account set --subscription "${var.subscription_id}"
      az tag create --resource-id "/subscriptions/${var.subscription_id}" --tags ${join(" ", [for k, v in var.tags : "${k}=${v}"])}
    EOT
  }

  triggers = {
    tags = jsonencode(var.tags)
  }
}

输出:

Here I was using the vinay as owner name and env as non prod

这里我使用

vinay
作为所有者名称,使用 env 作为
non-prod

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.