Terraform create multiple network interfaces with `for_each` and reference `azurerm_public_ip`

爱⌒轻易说出口 提交于 2021-02-11 17:10:54

问题


I have the following code:

resource "azurerm_public_ip" "imaged_pub_ip" {
  for_each = var.create_vm_images ? var.vms_to_image : {}
  name                = "${var.team_name}_${var.release}_imaged_public_ip_${each.value}"
  location            = var.loc
  resource_group_name = "${var.team_name}_${var.release}_${var.owner}_${var.intention}"
  allocation_method   = "Static"

  tags = {
    team = var.team_name
    environment = var.env_name
    useby = var.useby
    release = var.release
    devops_work_item_id = var.devops_work_item_id
    owner = var.owner
    intention = var.intention
  }
}
resource "azurerm_network_interface" "imaged_net_int" {
  for_each = var.create_vm_images ? var.vms_to_image : {}
  name                = "${var.team_name}_${var.release}_imaged_net_int_${each.value}"
  location            = var.loc
  resource_group_name = "${var.team_name}_${var.release}_${var.owner}_${var.intention}"

  ip_configuration {
    name                          = "eth0"
    private_ip_address_allocation = "Dynamic"
    subnet_id                     = data.azurerm_subnet.subnet.id
    public_ip_address_id          = values(azurerm_public_ip.imaged_pub_ip).*.id
  }

I am unable to reference the azurerm_public_ip.id in public_ip_address_id. It has an error: Inappropriate value for attribute "public_ip_address_id": string required.

I use a value map to say how many instances are required:

variable "vms_to_image" {
  type = map
}
  create_vm_images = "true"
  vms_to_image = {
    vm_id1 = "1"
    vm_id2 = "0"
  }

How do I reference the azurerm_public_ip.id for public_ip_address_id?


回答1:


This worked public_ip_address_id = azurerm_public_ip.imaged_pub_ip[each.key].id



来源:https://stackoverflow.com/questions/60646963/terraform-create-multiple-network-interfaces-with-for-each-and-reference-azur

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!