azure 无法将使用for each in terraform创建的资源ID引用到其他资源中

jtw3ybtb  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(106)

我正在尝试创建一个Azure负载平衡器模块,其中包含所有其他属性,例如backendaddress pool等。下面是我的代码
resource azure load balancerresource azure backendaddrespool的数据库

resource "azurerm_lb" "lb-test" {
 for_each           = { for lb in var.load_balancers : lb.name => lb }
 name                = each.value.name
 resource_group_name = each.value.resource_group_name
 location            = each.value.location
 sku = each.value.sku
 edge_zone = each.value.edge_zone
 sku_tier = each.value.sku_tier
 frontend_ip_configuration {
   name = each.value.name
   private_ip_address_allocation = each.value.frontend_ip_configuration.private_ip_address_allocation
   private_ip_address_version = each.value.frontend_ip_configuration.private_ip_address_version
   public_ip_address_id = var.public_ip_address_id
   subnet_id = var.subnet_id
   zones = each.value.frontend_ip_configuration.zones
 }
 
}

resource "azurerm_lb_backend_address_pool" "bckaddresspool" {
 for_each         = { for bckaddresspool in var.backend_address_pool : bckaddresspool.name => bckaddresspool }
  name            = each.value.name
  loadbalancer_id = azurerm_lb.lb-test[each.key].id
  virtual_network_id = var.virtual_network_id
  depends_on = [ azurerm_lb.lb-test ]
}

字符串
现在,当我获取在其他2个资源(backendaddressspool和loadbalancer探测器)中创建的LB的ID时,它会给我以下错误
在资源“azurerm_lb_probe”“azlb_probe”中:loadbalancer_id = azurerm_lb.lb-test[each.key].id
azurerm_lb.lb-test是每个属性为“LB 1”的对象。key是“Probe 1”
给定的键不标识此集合值中的元素。
任何人都可以帮助这个错误,因为我尝试了每一种可能的方式来采取的id作为输入,但仍然给我的问题。
在资源“azurerm_lb_probe”“azlb_probe”中:错误45:loadbalancer_id = azurerm_lb.lb-test[each.key].id不存在azurerm_lb.lb-test是每个具有1个属性“LB 1”的对象。key是“Probe 1”不存在给定的键不标识此集合值中的元素。
我也通过tfvars文件传递我所有的值。

epggiuax

epggiuax1#

我尝试将terraform中使用for_each创建的资源ID引用到其他资源中,并且我能够成功地提供需求。
您看到的错误消息表明您在for_each循环中使用的键与您定义的实际数据结构中存在的键之间不匹配。该错误特别指出azurerm_lb.lb-test集合中不存在键"Probe1"
导致错误的主要问题是:

  • azurerm_lb_probe中的for_each循环似乎使用了与azurerm_lb.lb-test中的键不匹配的键。for_each中使用的键必须与您引用的资源中的键完全匹配。
  • 如果负载均衡器和探测器之间的键不需要对齐,则引用azurerm_lb.lb-test[each.key].id可能不正确。

为了克服这个问题,我尝试了一个符合要求的地形配置。

我的Terraform配置:

provider "azurerm" {
    features {}
}

resource "azurerm_resource_group" "rg" {
  name     = var.resource_group_name
  location = var.location
}

resource "azurerm_virtual_network" "vnet" {
  name                = var.vnet_name
  address_space       = var.vnet_address_space
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

resource "azurerm_subnet" "subnet" {
  name                 = var.subnet_name
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = var.subnet_address_prefixes
}

resource "azurerm_public_ip" "publicip" {
  name                = "vksb-publicip"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Dynamic"
}

resource "azurerm_lb" "lb" {
  for_each            = var.load_balancers
  name                = each.value.name
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  sku                 = each.value.sku
  
  frontend_ip_configuration {
    name                          = each.value.frontend_ip_configuration.name
    private_ip_address_allocation = each.value.frontend_ip_configuration.private_ip_address_allocation // Can be "Static" or "Dynamic"
    private_ip_address            = each.value.frontend_ip_configuration.private_ip_address
    subnet_id                     = azurerm_subnet.subnet.id
  }
}

resource "azurerm_lb_backend_address_pool" "bckaddresspool" {
  for_each         = var.backend_address_pools
  name             = each.value.name
  loadbalancer_id  = azurerm_lb.lb[each.value.load_balancer_name].id
}

resource "azurerm_lb_probe" "probe" {
  for_each         = var.probes
  name             = each.value.name
  loadbalancer_id  = azurerm_lb.lb[each.value.load_balancer_name].id
  protocol         = each.value.protocol
  port             = each.value.port
  request_path     = each.value.request_path
  interval_in_seconds = each.value.interval_in_seconds
  number_of_probes = each.value.number_of_probes
}

字符串

variable.tf:

variable "resource_group_name" {
  description = "The name of the resource group"
  type        = string
}

variable "location" {
  description = "The Azure region to deploy the resources"
  type        = string
}

variable "vnet_name" {
  description = "The name of the virtual network"
  type        = string
}

variable "vnet_address_space" {
  description = "The address space for the virtual network"
  type        = list(string)
}

variable "subnet_name" {
  description = "The name of the subnet"
  type        = string
}

variable "subnet_address_prefixes" {
  description = "The address prefixes for the subnet"
  type        = list(string)
}

variable "load_balancers" {
  description = "A map of load balancer configurations"
  type        = map(object({
    name                        = string
    sku                         = string
    frontend_ip_configuration = object({
      name                                  = string
      private_ip_address_allocation         = string
      private_ip_address                  = string
    })
  }))
}

variable "backend_address_pools" {
  description = "A map of backend address pool configurations"
  type        = map(object({
    name                = string
    load_balancer_name  = string
  }))
}

variable "probes" {
  description = "A map of load balancer probe configurations"
  type        = map(object({
    name                  = string
    load_balancer_name    = string
    protocol              = string
    port                  = number
    request_path          = string
    interval_in_seconds   = number
    number_of_probes      = number
  }))
}

terrafrom.tfvars:

resource_group_name = "vksb-resources"
location            = "West Europe"
vnet_name           = "vksb-vnet"
vnet_address_space  = ["10.0.0.0/16"]
subnet_name         = "vksb-subnet"
subnet_address_prefixes = ["10.0.1.0/24"]

load_balancers = {
  "lb1" = {
    name = "vksb-lb1"
    sku  = "Standard"
    frontend_ip_configuration = {
      name                          = "vksb-fe-config"
      private_ip_address_allocation = "Static"
      private_ip_address            = "10.0.1.4"
    }
  }
}

backend_address_pools = {
  "pool1" = {
    name               = "vksb-bap1"
    load_balancer_name = "lb1"
  }
}

probes = {
  "probe1" = {
    name                  = "vksb-probe1"
    load_balancer_name    = "lb1"
    protocol              = "Http"
    port                  = 80
    request_path          = "/"
    interval_in_seconds   = 15
    number_of_probes      = 2
  }
}

输出:


的数据



相关问题