问题描述
我正在尝试创建一个 Azure 负载均衡器,以在 az 负载均衡器中构建多个前端 ip,该负载均衡器将公共 ip 用于 Azure 可用性区域:
这是我的文件结构:
terraform-azurem-loadbalncer/
┣ locals.tf
┣ output.tf
┣ tlz-lb.tf
┣ tlz-pip.tf
┗ variables.tf
我想要做的很像帖子[在这里][1]: 这是我的 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_backend_address_pool" "tlz-lb-backendpool-dev" {
count = var.nblinuxvms
name = var.backend_address_pool
loadbalancer_id = azurerm_lb.tlz-lb-navigator-dev[count.index].id
}
resource "azurerm_lb_rule" "tlz-lb-rule" {
count = var.nblinuxvms
resource_group_name = var.resource_group_name
loadbalancer_id = azurerm_lb.tlz-lb-navigator-dev[count.index].id
name = var.load_balancing_rule
protocol = "Tcp"
frontend_port = 443
backend_port = 443
frontend_ip_configuration_name = "config_${azurerm_public_ip.tlz_public_ip[count.index].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[count.index].id
name = var.nat_rule_name
protocol = "Tcp"
frontend_port = 80
backend_port = 80
frontend_ip_configuration_name = "config_${azurerm_public_ip.tlz_public_ip[count.index].name}"
}
resource "azurerm_lb_probe" "tlz-lb-probe" {
count = var.nblinuxvms
resource_group_name = var.resource_group_name
loadbalancer_id = azurerm_lb.tlz-lb-navigator-dev[count.index].id
name = var.health_probe_name
port = 22
}
resource "azurerm_lb" "tlz-lb-navigator-dev" {
count = var.nblinuxvms
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 = "config_${azurerm_public_ip.tlz_public_ip[count.index].name}"
public_ip_address_id = azurerm_public_ip.tlz_public_ip[count.index].id
}
dynamic "frontend_ip_configuration" {
for_each = azurerm_public_ip.tlz_public_ip
content {
name = "config_${frontend_ip_configuration.value.name}"
public_ip_address_id = frontend_ip_configuration.value.id
}
}
}
这是我的 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 "availability_zone" {
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 = "test"
description = "(required) Specifies the name of the LB Rule."
}
variable "nat_rule_name" {
type = string
default = "test"
description = "(required) Specifies the name of the NAT Rule."
}
variable "health_probe_name" {
type = string
default = "test"
description = "(required) Specifies the name of the Probe."
}
variable "nblinuxvms" {
type = number
default = "2"
description = "NUmber of VMs to be."
}
这是我得到的错误:
Error: expected "name" to not be an empty string,got
on FARMERS-TLZ-TFE-PMR/terraform-azurerm-stage-lb/terraform-azurem-loadbalncer/tlz-lb.tf line 1,in resource "azurerm_lb_backend_address_pool" "tlz-lb-backendpool-dev":
1: resource "azurerm_lb_backend_address_pool" "tlz-lb-backendpool-dev" {
Error: expected "name" to not be an empty string,in resource "azurerm_lb_backend_address_pool" "tlz-lb-backendpool-dev":
1: resource "azurerm_lb_backend_address_pool" "tlz-lb-backendpool-dev" {
Error: only word characters,numbers,underscores,periods,and hyphens allowed in "name": ""
on FARMERS-TLZ-TFE-PMR/terraform-azurerm-stage-lb/terraform-azurem-loadbalncer/tlz-lb.tf line 6,in resource "azurerm_lb_rule" "tlz-lb-rule":
6: resource "azurerm_lb_rule" "tlz-lb-rule" {
Error: "name" cannot be an empty string: ""
on FARMERS-TLZ-TFE-PMR/terraform-azurerm-stage-lb/terraform-azurem-loadbalncer/tlz-lb.tf line 6,in resource "azurerm_lb_rule" "tlz-lb-rule":
6: resource "azurerm_lb_rule" "tlz-lb-rule" {
Error: "name" must end with a word character,number,or underscore: ""
on FARMERS-TLZ-TFE-PMR/terraform-azurerm-stage-lb/terraform-azurem-loadbalncer/tlz-lb.tf line 6,in resource "azurerm_lb_rule" "tlz-lb-rule":
6: resource "azurerm_lb_rule" "tlz-lb-rule" {
Error: "name" must start with a word character or number: ""
on FARMERS-TLZ-TFE-PMR/terraform-azurerm-stage-lb/terraform-azurem-loadbalncer/tlz-lb.tf line 6,in resource "azurerm_lb_rule" "tlz-lb-rule":
6: resource "azurerm_lb_rule" "tlz-lb-rule" {
Error: only word characters,in resource "azurerm_lb_rule" "tlz-lb-rule":
6: resource "azurerm_lb_rule" "tlz-lb-rule" {
Error: expected "name" to not be an empty string,got
on FARMERS-TLZ-TFE-PMR/terraform-azurerm-stage-lb/terraform-azurem-loadbalncer/tlz-lb.tf line 16,in resource "azurerm_lb_nat_rule" "tlz-nat-rule":
16: resource "azurerm_lb_nat_rule" "tlz-nat-rule" {
Error: expected "name" to not be an empty string,got
on FARMERS-TLZ-TFE-PMR/terraform-azurerm-stage-lb/terraform-azurem-loadbalncer/tlz-lb.tf line 26,in resource "azurerm_lb_probe" "tlz-lb-probe":
26: resource "azurerm_lb_probe" "tlz-lb-probe" {
Error: expected "name" to not be an empty string,in resource "azurerm_lb_probe" "tlz-lb-probe":
26: resource "azurerm_lb_probe" "tlz-lb-probe" {
不知道为什么我可能会收到那个错误,因为我已经在 variables.tf 中指定了名称
解决方法
如果您查看 azurerm_lb_backend_address_pool (https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/lb_backend_address_pool) 的资源,名称是必填字段,根据您下面的代码,您将传递 var.back_address_pool 和 var.back_address_pool 的默认值为空。
resource "azurerm_lb_backend_address_pool" "tlz-lb-backendpool-dev" {
count = var.nblinuxvms
name = var.backend_address_pool
loadbalancer_id = azurerm_lb.tlz-lb-navigator-dev[count.index].id
}