Home > Back-end >  list of map required for loabdbalancer rules specs
list of map required for loabdbalancer rules specs

Time:04-04

I have a terraform tfvars.json file as like below:

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

The main.tf is like below:

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
  
}

The main.tf of module itself is like below:

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
}

And Variables.tf like below:

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

I did try to remove the { braces but honestly I couldn't figure it out what is the issue. If the tfvars file was in proper .tf format things would have been little better, with json I get totally confused.

I am getting error like below:

│ 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.

Need some help to resolve the error.

CodePudding user response:

Your lb_rule_specs is a list(map(string)) but you are just passing a map(string).

Assuming that everything else works, to address your error it should be:

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