Terraform Nested maps of different sizes

291 Views Asked by At

I am deploying a number of AWS application load balancers by feeding a nested map from locals.tf to a module configuring the load-balancers.

locals {
   lb_vars = {
      alb1 = {
           load_balancer_type = application
           listener_port  = 443
           listener_protocol = https
           internal = false
           subnets  = var.subnet1
            backends = {
                 backend1 = {
                 port                  = "8080"
                 path                  = ["/endpoint1/backend1*"]
                 protocol              = "http"
                 protocol_version      = "http1"
                 health_check_enabled  = true
                 health_check_interval = 10
                 health_check_port     = 19808
                 health_check_path     = "/health"
                 health_check_protocol = "http"    
          },
                 backend2 = {
                 port                  = "8081"
                 path                  = ["/endpoint1/backend2*"]
                 protocol              = "http"
                 protocol_version      = "http1"
                 health_check_enabled  = true
                 health_check_interval = 10
                 health_check_port     = 19809
                 health_check_path     = "/health"
                 health_check_protocol = "http"    
          },
      alb2 = {
           load_balancer_type = application
           listener_port  = 443
           listener_protocol = https
           internal = false
           subnets = var.subnet1
            backends = {
                 backend1 = {
                 port                  = "8082"
                 path                  = ["/endpoint2/backend1*"]
                 protocol              = "http"
                 protocol_version      = "http1"
                 health_check_enabled  = true
                 health_check_interval = 10
                 health_check_port     = 19810
                 health_check_path     = "/health"
                 health_check_protocol = "http"    
          },             
                 backend2 = {
                 port                  = "8083"
                 path                  = ["/endpoint2/backend2*"]
                 protocol              = "http"
                 protocol_version      = "http1"
                 health_check_enabled  = true
                 health_check_interval = 10
                 health_check_port     = 19811
                 health_check_path     = "/health"
                 health_check_protocol = "http"    
          },
       }
    }
  }
}

Resource in load-balancer module:

resource "aws_lb" "lb" {                                   
  for_each = var.lb_vars                    
                                                       
  name               = "${each.key}-${var.env_name}"       
  internal           = try(each.value.internal, "false")            
  load_balancer_type = try(each.value.load_balancer_type, "application")   
  security_groups    = aws_security_group.lb_security_group[each.key] 
  subnets            = each.value.subnets     
                                                       
  enable_deletion_protection = false                                                                          
                                                      
  tags = "Name" = "${each.key}-${var.env_name}"                                                           
}

As one can see, there are a number of parameters which I would like to not define for each AWS lb because typically they are the defaults, but if I remove one of the parameters I get the following error;

Error: Invalid value for module argument
The given value is not suitable for child module variable "lb_vars" defined
at lb/variables.tf:41,1-21: all map elements must have the same type.

Load-balancer Module

module "lb" {
  source = "./lb"

  env_name         = var.env_name
  full_env_name    = local.full_env_name
  subnet_ids       = local.subnet_ids
  vpc_id           = data.aws_vpc.vpc.id
  external_zone_id = data.aws_route53_zone.external.zone_id
  common_tags      = local.common_tags
  env_cert_arn     = data.aws_acm_certificate.wildcard_cert.arn
  lb_params        = local.lb_params
}

Variables.tf in load-balancer modules (line 41 as per error)

variable "lb_params" {
  type        = map
  description = "LB parameters"
}
0

There are 0 best solutions below