loabdbalancer 规则规范所需的地图列表

list of map required for loabdbalancer rules specs

我有一个 terraform tfvars.json 文件,如下所示:

{
  "loadbalancer_rule": {
    "patterns_default_loadbalancer_rule": {
      "backend_address_pool_id": null,
      "lb_rule_specs" : {
        
          "name" : "test2",
          "protocol": "tcp",
          "frontend_port": "8080",
          "backend_port": "8081",
          "frontend_ip_configuration_name": "projectname-lb-nic"
      
      },
      "load_distribution": "",
      "loadbalancer_id": null,
      "probe_id": "",
      "resource_group_name": null
    }
  }
}

main.tf如下:

variable "loadbalancer_rule" {
  description = "Map of loadbalancer-rule objects"
  type        = any
  default     = null
}

module "loadbalancer_rule" {
  for_each            = coalesce(var.loadbalancer_rule, {})
  source              = "../loadbalancer-rule/azurerm"
  version             = "7.0.0-2-1.0"

  backend_address_pool_id = try(each.value.backend_address_pool_id, null)
  lb_rule_specs = try(each.value.lb_rule_specs, null)
  load_distribution = try(each.value.load_distribution, "")
  loadbalancer_id = try(each.value.loadbalancer_id, null)
  probe_id = try(each.value.probe_id, "")
  resource_group_name = var.environment_resource_groups
  
}

模块本身的main.tf如下:

resource "azurerm_lb_rule" "lb_rule" {
  count                          = length(var.lb_rule_specs)
  name                           = var.lb_rule_specs[count.index]["name"]
  resource_group_name            = var.resource_group_name
  loadbalancer_id                = var.loadbalancer_id
  frontend_ip_configuration_name = var.lb_rule_specs[count.index]["frontend_ip_configuration_name"]
  protocol                       = var.lb_rule_specs[count.index]["protocol"]
  frontend_port                  = var.lb_rule_specs[count.index]["frontend_port"]
  backend_port                   = var.lb_rule_specs[count.index]["backend_port"]
  probe_id                       = var.probe_id
  load_distribution              = var.load_distribution
  backend_address_pool_id        = var.backend_address_pool_id
}

和 Variables.tf 如下所示:

variable "lb_rule_specs" {
  description = "Load balancer rules specifications"
  type        = list(map(string))
}

variable "resource_group_name" {
  description = "Name of the resource group"
  type        = string
}

variable "loadbalancer_id" {
  description = "ID of the load balancer"
  type        = string
}

variable "backend_address_pool_id" {
  description = "Backend address pool id for the load balancer"
  type        = string
}

variable "probe_id" {
  description = "ID of the loadbalancer probe"
  type        = string
  default     = ""
}

variable "load_distribution" {
  description = "Specifies the load balancing distribution type to be used by the Load Balancer. Possible values are: Default – The load balancer is configured to use a 5 tuple hash to map traffic to available servers. SourceIP – The load balancer is configured to use a 2 tuple hash to map traffic to available servers. SourceIPProtocol – The load balancer is configured to use a 3 tuple hash to map traffic to available servers. Also known as Session Persistence, where the options are called None, Client IP and Client IP and Protocol respectively."
  type        = string
  default     = ""
}

我确实尝试移除 { 大括号,但老实说我无法弄清楚问题出在哪里。 如果 tfvars 文件是正确的 .tf 格式,情况会好一些,json 我完全糊涂了。

我收到如下错误:

│ Error: Invalid value for module argument
│
│   on loadbalancer_rule.tf line 13, in module "loadbalancer_rule":
│   13:   lb_rule_specs = try(each.value.lb_rule_specs, null)
│
│ The given value is not suitable for child module variable "lb_rule_specs"
│ defined at .terraform/modules/loadbalancer_rule/variables.tf:1,1-25: list
│ of map of string required.

需要一些帮助来解决错误。

你的 lb_rule_specslist(map(string)) 但你只是传递 map(string).

假设其他一切正常,要解决您的错误应该是:

lb_rule_specs = [try(each.value.lb_rule_specs, null)]