gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:27:03 25 4
gpt4 key购买 nike

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

这是我的文件结构:

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
}

这是我的变量.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 中配置的类似,那么这是由于动态 block 是在资源 block 之外创建的。

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

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
}
}

编辑:

此外,在迭代时的动态 block 中,您很可能需要使用 frontend_ip_configuration.value.<ATTRIBUTE>而不是each.value.<ATTRIBUTE> .

关于azure - 使用 Azure 负载均衡器在 terraform 上使用 for_each,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68048575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com