使用Azure负载均衡器在地形上执行for_each



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

这是我的文件结构:

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

我想做的很像这里的帖子:这是我的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
}

这是我的变量.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.

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

如果这里的代码格式与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>而不是each.value.<ATTRIBUTE>

最新更新