没有名为“var”的变量:使用“each”时出现 terraform 错误

问题描述 投票:0回答:1

我在尝试使用 terraform 和 azure 创建应用程序网关时遇到此错误,所以这里是错误:


│ Error: Unknown variable
│ 
│   on ../../../../../sca-terraform-modules/modules/ApplicationGateway/main.tf line 157, in resource "azurerm_application_gateway" "AG":
│  157:     for_each = var.IS_CUSTOM_NAMING ? {} : var.BACKEND_CONFIGURATION
│ 
│ There is no variable named "var".

│ Error: Unknown variable
│ 
│   on ../../../../../sca-terraform-modules/modules/ApplicationGateway/main.tf line 148, in resource "azurerm_application_gateway" "AG":
│  148:     for_each = var.IS_CUSTOM_NAMING ? var.BACKEND_ADDRESS_POOL_CONFIGURATION : []
│ 
│ There is no variable named "var".

│ Error: Insufficient backend_address_pool blocks
│ 
│   on ../../../../../sca-terraform-modules/modules/ApplicationGateway/main.tf line 32, in resource "azurerm_application_gateway" "AG":
│   32: resource "azurerm_application_gateway" "AG" {
│ 
│ At least 1 "backend_address_pool" blocks are require

这是我的代码:

dynamic "backend_address_pool" {
    for_each = var.IS_CUSTOM_NAMING ? var.BACKEND_ADDRESS_POOL_CONFIGURATION : []
    content {
      name         = backend_address_pool.value.name
      ip_addresses = backend_address_pool.value.backend_address_pool_ips
      fqdns        = backend_address_pool.value.backend_address_pool_fqdns
    }
  }

dynamic "backend_address_pool" {
    for_each = var.IS_CUSTOM_NAMING ? {} : var.BACKEND_CONFIGURATION
    content {
      name         = "beap-agw-${backend_address_pool.key}"
      ip_addresses = backend_address_pool.value.backend_pool_ips
      fqdns        = backend_address_pool.value.backend_pool_fqdns

    }
  }
variable "BACKEND_ADDRESS_POOL_CONFIGURATION" {
  type = list(object({
    name                       = string
    backend_address_pool_ips   = optional(list(string))
    backend_address_pool_fqdns = optional(list(string))
  }))
  default     = []
  description = "Definition of Backend Address Pools."
}
variable "BACKEND_CONFIGURATION" {
  type = map(object({
    backend_pool_ips                                = optional(list(string))
    backend_pool_fqdns                              = optional(list(string))
    backend_cookie_affinity                         = string
    backend_path                                    = optional(string)
    backend_port                                    = number
    backend_protocol                                = string
    backend_request_timeout                         = optional(number)
    trusted_root_certificate_names                  = optional(list(string), [])
    pick_host_name_from_backend_address             = optional(bool)
    probe_interval                                  = optional(number)
    probe_protocol                                  = optional(string)
    probe_path                                      = optional(string)
    probe_timeout                                   = optional(number)
    probe_unhealthy_threshold                       = optional(number)
    probe_port                                      = optional(number)
    probe_host                                      = optional(string)
    probe_match_body                                = optional(string)
    probe_match_status_code                         = optional(list(string))
    probe_pick_host_name_from_backend_http_settings = optional(bool)

  }))
  default     = {}
  description = "Provide settings for Application Gateway backend pool."
}

我是 terraform 新手

terraform-provider-azure azure-application-gateway
1个回答
0
投票

使用静态定义设置一个后端池,以确保使用 terraform 的 for 循环功能

要解决使用 Terraform 创建 Azure 应用程序网关时遇到的问题,请按照以下步骤操作:

  1. 检查变量声明: 确保变量

    IS_CUSTOM_NAMING
    BACKEND_ADDRESS_POOL_CONFIGURATION
    BACKEND_CONFIGURATION
    已正确声明并正确传递到模块。

  2. 简化配置:首先创建一个具有本地静态定义的后端地址池,以确保

    for_each
    循环正常运行。

正如 Christian Pearce 建议的那样,最好采取第二步,因为你是 terraform 的新手,并避免这些不必要的障碍。

我的示例地形配置:

provider "azurerm" {
    features { }
  subscription_id = var.subscription_id
  client_id       = var.client_id
  client_secret   = var.client_secret
  tenant_id       = var.tenant_id
  skip_provider_registration = true
}


variable "subscription_id" {
  description = "Subscription ID"
  type        = string
  
}

variable "client_id" {
  description = "Client ID"
  type        = string
  
}

variable "client_secret" {
  description = "Client Secret"
  type        = string
  sensitive   = true
}

variable "tenant_id" {
  description = "Tenant ID"
  type        = string
 
}



resource "azurerm_resource_group" "rg" {
  name     = "testvkrg-resources"
  location = "East US"
}

resource "azurerm_virtual_network" "vnet" {
  name                = "testvk-vnet"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  address_space       = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "subnet" {
  name                 = "testvk-subnet"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_public_ip" "pip" {
  name                = "testvk-pip"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Static"
  sku                 = "Standard"
}

resource "azurerm_application_gateway" "ag" {
  name                = "testvk-ag"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  sku {
    name     = "Standard_v2"
    tier     = "Standard_v2"
    capacity = 2
  }

  gateway_ip_configuration {
    name      = "my-gateway-ip-configuration"
    subnet_id = azurerm_subnet.subnet.id
  }

  frontend_port {
    name = "frontendport"
    port = 80
  }

  frontend_ip_configuration {
    name                 = "frontendipconfiguration"
    public_ip_address_id = azurerm_public_ip.pip.id
  }

  backend_address_pool {
    name         = "example-pool"
    ip_addresses = ["10.0.1.10"]
  }

  backend_http_settings {
    name                  = "example-http-setting"
    cookie_based_affinity = "Disabled"
    port                  = 80
    protocol              = "Http"
    request_timeout       = 20
  }

  http_listener {
    name                           = "example-listener"
    frontend_ip_configuration_name = "frontendipconfiguration"
    frontend_port_name             = "frontendport"
    protocol                       = "Http"
  }

  request_routing_rule {
    name                       = "example-routing-rule"
    rule_type                  = "Basic"
    http_listener_name         = "example-listener"
    backend_address_pool_name  = "example-pool"
    backend_http_settings_name = "example-http-setting"
    priority                   = 100
  }
}

variable "IS_CUSTOM_NAMING" {
  description = "Flag to indicate custom naming"
  type        = bool
  default     = false
}

variable "BACKEND_ADDRESS_POOL_CONFIGURATION" {
  description = "Definition of Backend Address Pools."
  type = list(object({
    name                       = string
    backend_address_pool_ips   = optional(list(string))
    backend_address_pool_fqdns = optional(list(string))
  }))
  default = []
}

variable "BACKEND_CONFIGURATION" {
  description = "Provide settings for Application Gateway backend pool."
  type = map(object({
    backend_pool_ips                                = optional(list(string))
    backend_pool_fqdns                              = optional(list(string))
    backend_cookie_affinity                         = string
    backend_path                                    = optional(string)
    backend_port                                    = number
    backend_protocol                                = string
    backend_request_timeout                         = optional(number)
    trusted_root_certificate_names                  = optional(list(string), [])
    pick_host_name_from_backend_address             = optional(bool)
    probe_interval                                  = optional(number)
    probe_protocol                                  = optional(string)
    probe_path                                      = optional(string)
    probe_timeout                                   = optional(number)
    probe_unhealthy_threshold                       = optional(number)
    probe_port                                      = optional(number)
    probe_host                                      = optional(string)
    probe_match_body                                = optional(string)
    probe_match_status_code                         = optional(list(string))
    probe_pick_host_name_from_backend_http_settings = optional(bool)
  }))
  default = {}
}


output "application_gateway_id" {
  value = azurerm_application_gateway.ag.id
}

部署成功:

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.