使用 json 格式的 azure terraform 创建 azure 操作组

Create azure Action group with azure terraform with json format

您好,我正在尝试根据我拥有的 json 格式创建“azurerm_monitor_action_group” github_link 请仔细阅读 github link 并更新我错过的更改..

尝试使用您的代码出现同样的错误。

解决方案:

您看到错误的原因是 jsonencode returns 一个字符串,而 for_each 需要一个映射或集合。不用jsonencode,直接用Terraform的地图

您可以使用下面的代码 terraform plan 成功,而 terraform apply 出错,因为 email 对我无效。您可以在您的环境中进行相同的测试,它将成功地为您申请。

main.tf

##############  azurerm provider  ######################################
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=2.99.0"
    }
  }
}


provider "azurerm" {
  subscription_id = var.subscription_id
  tenant_id       = var.tenant_id
  #   client_id       = var.client_id
  #   client_secret   = var.client_secret

  features {
  }
}
###################

locals {
  our_widgets = {  
   "dashboard1" ={
        "type": "sbns",
        "sme": ["sme1@bechtel.com","sme2@bechtel.com"],
        "tech_support": ["tech1@bechtel.com","tech2@bechtel.com"],
        "primary_support": ["primary@bechtel.com","primary2@bechtel.com"],
        "secondary_support": ["secondary1@bechtel.com","secondary2@bechtel.com"],
        "all": ["all1@bechtel.com","all2@bechtel.com"]
    },

    "dashboard2" ={
        "type": "sta",
        "sme": ["sme1@bechtel.com","sme2@bechtel.com"],
        "tech_support": ["tech1@bechtel.com","tech2@bechtel.com"],
        "primary_support": ["primary@bechtel.com","primary2@bechtel.com"],
        "secondary_support": ["secondary1@bechtel.com","secondary2@bechtel.com"]
    },
    "dashboard3" ={
        "type": "kv",
        "sme": ["sme1@bechtel.com","sme2@bechtel.com"],
        "tech_support": ["tech1@bechtel.com","tech2@bechtel.com"],
        "primary_support": ["primary@bechtel.com","primary2@bechtel.com"],
        "secondary_support": ["secondary1@bechtel.com","secondary2@bechtel.com"]
    },
    "dashboard3"={
        "type": "redis",
        "sme": ["sme1@bechtel.com","sme2@bechtel.com"],
        "tech_support": ["tech1@bechtel.com","tech2@bechtel.com"],
        "primary_support": ["primary@bechtel.com","primary2@bechtel.com"],
        "secondary_support": ["secondary1@bechtel.com","secondary2@bechtel.com"]
    },
    "dashboard5"={
        "type": "postgres",
        "sme": ["sme1@bechtel.com","sme2@bechtel.com"],
        "tech_support": ["tech1@bechtel.com","tech2@bechtel.com"],
        "primary_support": ["primary@bechtel.com","primary2@bechtel.com"],
        "secondary_support": ["secondary1@bechtel.com","secondary2@bechtel.com"]
    }
}
}

############################
##########  Creating ResourceGroup   #######################
data "azurerm_resource_group" "rg" {
  name     = "v-rXXXXXe"
  #location = "westus"

}
##################################
resource "azurerm_monitor_action_group" "monitor_action_group" {
  #for_each            = local.resource
  name                = "test4574365"
  resource_group_name = data.azurerm_resource_group.rg.name
  short_name          = "short-alert"
  dynamic "email_receiver" {
  for_each = local.our_widgets
    content {
      name                    = "Email"
      email_address           = "email_receiver.value.primary_support" # here it is not working 
      use_common_alert_schema = true #Enables or disables the common alert schema
    }
  }
}

更多信息你可以参考这个link