使用 Azure 负载均衡器在 terraform 上使用 for_each

问题描述

我正在尝试创建一个 Azure 负载均衡器,以在 az 负载均衡器中构建多个前端 ip,该负载均衡器将公共 ip 用于 Azure 可用性区域:

这是我的文件结构:

terraform-azurem-loadbalncer/
┣ locals.tf
┣ output.tf
┣ tlz-lb.tf
┣ tlz-pip.tf
┗ variables.tf

我想要做的很像帖子over here: 这是我的 tlz-pip.tf

resource "azurerm_public_ip" "tlz_public_ip" {
  name                    = "${local.prefix}-${local.resource_type}-${var.pip_name}-${var.environment}"
  resource_group_name     = var.resource_group_name
  location                = var.location
  allocation_method       = var.allocation_method
  sku                     = var.pip_sku
  ip_version              = var.ip_version
  idle_timeout_in_minutes = try(var.idle_timeout_in_minutes,30)
  domain_name_label       = var.generate_domain_name_label ? "${local.prefix}-${local.resource_type}-${var.pip_name}-${var.environment}" : var.domain_name_label
  reverse_fqdn            = try(var.reverse_fqdn,null)
  zones                   = try(var.zones,null)
  tags = merge(
    {
      "Name" = "${local.prefix}-${local.resource_type}-${var.pip_name}-${var.environment}"
    },var.tags
  )
}

这是我的 tlz-lb.tf:

resource "azurerm_lb" "tlz-lb-navigator-dev" {
  name                = "${local.prefix}-${local.resource_type}-${var.environment}-${var.location}"
  location            = var.location
  resource_group_name = var.resource_group_name
  sku                 = var.lb_sku

  frontend_ip_configuration {
    name                 = var.frontend_ip_name
    public_ip_address_id = azurerm_public_ip.tlz_public_ip.id
  }

  dynamic "frontend_ip_configuration" {
     for_each = azurerm_public_ip.tlz_public_ip
     content {
       name                 = "config_${each.value.name}" 
       public_ip_address_id = each.value.id
    }
  }
}
resource "azurerm_lb_backend_address_pool" "tlz-lb-backendpool-dev" {
  name            = var.backend_address_pool
  loadbalancer_id = azurerm_lb.tlz-lb-navigator-dev.id
}
resource "azurerm_lb_rule" "tlz-lb-rule" {
  resource_group_name            = var.resource_group_name
  loadbalancer_id                = azurerm_lb.tlz-lb-navigator-dev.id
  name                           = var.load_balancing_rule
  protocol                       = "Tcp"
  frontend_port                  = 443
  backend_port                   = 443
  frontend_ip_configuration_name = var.frontend_ip_name
}
resource "azurerm_lb_nat_rule" "tlz-nat-rule" {
  count                          = var.nblinuxvms
  resource_group_name            = var.resource_group_name
  loadbalancer_id                = azurerm_lb.tlz-lb-navigator-dev.id
  name                           = var.nat_rule_name
  protocol                       = "Tcp"
  frontend_port                  = 80
  backend_port                   = 80
  frontend_ip_configuration_name = "config_${azurerm_public_ip[count.index].tlz_public_ip.name}"
}
resource "azurerm_lb_probe" "tlz-lb-probe" {
  resource_group_name = var.resource_group_name
  loadbalancer_id     = azurerm_lb.tlz-lb-navigator-dev.id
  name                = var.health_probe_name
  port                = 22
}

这是我的 variables.tf:

#Public IP configuration
variable "location" {
  description = "(required) The location/region where the virtual network is created"
  default     = "centralus"
}
variable "environment" {
  description = "(required) The environment platform in which resources will be deployed."
  default     = "stage"
}
variable "public_ip_address_id" {
  default     = ""
  description = "public ip address id"
}
variable "resource_group_name" {

  description = "resource group name"
}
variable "tags" {
  description = "(required) Map of tags to be applied to the resource"
  type        = map(any)
}
variable "pip_name" {
  description = "(required) The name for public ip address."
}
variable "allocation_method" {
  default     = "Dynamic"
  description = "(required) Defines the allocation method for this IP address. Possible values are Static or Dynamic."
}
variable "ip_version" {
  description = "The IP Version to use,IPv6 or IPv4."
  default     = "IPv4"
}
variable "idle_timeout_in_minutes" {
  description = "Specifies the timeout for the TCP idle connection. The value can be set between 4 and 30 minutes."
  default     = 30
}
variable "generate_domain_name_label" {
  description = "The flag to control creation of domain label."
  default     = false
}
variable "domain_name_label" {
  description = "If a domain name label is specified,an A DNS record is created for the public IP in the Microsoft Azure DNS system."
  default     = null
}
variable "reverse_fqdn" {
  description = " A fully qualified domain name that resolves to this public IP address."
  default     = ""
}
variable "zones" {
  description = "A collection containing the availability zone to allocate the Public IP in."
  default     = null
}


#Load Balancer

variable "lb_sku" {
  type        = string
  default     = "Basic"
  description = "(Optional) The SKU of the Azure Load Balancer. Accepted values are Basic and Standard. Defaults to Basic."
}
variable "pip_sku" {
  type        = string
  default     = "Basic"
  description = "(Optional) The SKU of the Public IP. Accepted values are Basic and Standard. Defaults to Basic."
}
variable "frontend_ip_name" {
  type        = string
  default     = ""
  description = "(required) Specifies the name of the frontend ip configuration."
}
variable "backend_address_pool" {
  type        = string
  default     = ""
  description = "(required) Specifies the name of the Backend Address Pool"
}
variable "load_balancing_rule" {
  type        = string
  default     = ""
  description = "(required) Specifies the name of the LB Rule."
}
variable "nat_rule_name" {
  type        = string
  default     = ""
  description = "(required) Specifies the name of the NAT Rule."
}
variable "health_probe_name" {
  type        = string
  default     = ""
  description = "(required) Specifies the name of the Probe."
}
variable "nblinuxvms" {
  type        = number
  default     = "2"
  description = "NUmber of VMs to be attached."
}

这是我为 tlz-lb.tf 得到的错误错误:无效参考

  on FARMERS-TLZ-TFE-PMR/terraform-azurerm-stage-lb/terraform-azurem-loadbalncer/tlz-lb.tf line 41,in resource "azurerm_lb_nat_rule" "tlz-nat-rule":
  41:   frontend_ip_configuration_name = "config_${azurerm_public_ip[count.index].name}"

A reference to a resource type must be followed by at least one attribute
access,specifying the resource name.

我不知道是什么原因导致了这个错误,因为我已经关注了之前提到的帖子,请帮助我。谢谢。

解决方法

如果您在此处的代码格式与您在 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim lst As New List(Of String) lst.Add("C:\Users\..\Documents\abc_1.1.pdf") '->file name abc lst.Add("C:\Users\..\Documents\abc_1.2.pdf") lst.Add("C:\Users\..\Documents\abc_2.1.pdf") lst.Add("C:\Users\..\Documents\xxxxxx_1.1.pdf") '->file name xxxxxx lst.Add("C:\Users\..\Documents\xxxxxx_2.1.pdf") Dim newList = (From s In lst Select s.Split("_"c)(0)).ToList.Distinct For Each s In newList Debug.Print(s) Next End Sub 'Prints 'C:\Users\..\Documents\abc 'C:\Users\..\Documents\xxxxxx 中配置的类似,那么这是由于在资源块之外创建了动态块。

我建议清理您的代码,使其类似于下面动态块位于资源块内的内容。根据需要调整间距。

tlz-lb.tf

编辑:

也在动态块中进行迭代时,您很可能需要使用 resource "azurerm_lb" "tlz-lb-navigator-dev" { name = "${local.prefix}-${local.resource_type}-${var.environment}-${var.location}" location = var.location resource_group_name = var.resource_group_name sku = var.lb_sku frontend_ip_configuration { name = var.frontend_ip_name public_ip_address_id = azurerm_public_ip.tlz_public_ip.id } } dynamic "frontend_ip_configuration" { for_each = azurerm_public_ip.tlz_public_ip content { name = "config_${each.value.name}" public_ip_address_id = each.value.id } } 而不是 frontend_ip_configuration.value.<ATTRIBUTE>