Terraform : Invalid value for "inputMap" parameter: lookup() requires a map as the first argument

Viewed 2291

I try to set up lb for alertmanager via terraform and have some difficulties with code. Could you please help with it? There you can find a code from terraform : I use my code before for creation lb without cognito module, but after i added cognito auth i can't use it any more.

Lb with alertmanager:

  source                    = "../../modules/aws-lb"
  vpc_id                    = module.vpc.id
  default_security_group_id = module.vpc.default_security_group_id
  tags                      = local.tags
  prefix                    = "${var.environment}-euc1"
  target_groups = {
    alertmanager = {
      connect     = "HTTP:9093"
      target_type = "ip"
      health_check = {
        path    = "/-/healthy"
        matcher = 200
      }
    }
  }
  listeners = {
    "HTTP:80" = {
      actions = {
        default = {
          type = "redirect"
        }
      }
    }
    "HTTPS:443" = {
      certificates_arns = [module.certificate_lb.arn]
      actions = {
        default = [{
          type  = "forward"
          group = "alertmanager"
        },
        {
          type = "authenticate-cognito"
          authenticate_cognito = {
            user_pool_arn       = module.aws_cognito_user_pool.arn
            user_pool_client_id = module.aws_cognito_user_pool.id["alertmanager"]
            user_pool_domain    = module.aws_cognito_user_pool.domain
          }
        }]
      }
    }
  }
}  ```


Error from terraform output:
│ Error: Invalid function argument
│ 
│   on ../../modules/aws-lb/lb.tf line 25, in resource "aws_lb_listener" "listener":
│   25:       type             = lookup(default_action.value, "type", "forward") 
│     ├────────────────
│     │ default_action.value is tuple with 2 elements
│ 
│ Invalid value for "inputMap" parameter: lookup() requires a map as the first argument.
on ../../modules/aws-lb/lb.tf line 26, in resource "aws_lb_listener" "listener":
│   26:       target_group_arn = lookup(default_action.value, "type", "forward") == "forward" ? aws_lb_target_group.tg[default_action.value.group].id : null
│     ├────────────────
│     │ default_action.value is tuple with 2 elements
│ 
│ Invalid value for "inputMap" parameter: lookup() requires a map as the first argument.

If you need more details, I can add.
1 Answers

Listener:

resource "aws_lb_listener" "listener" {
  for_each          = var.listeners
  load_balancer_arn = aws_lb.lb.id
  port              = split(":", each.key)[1]
  protocol          = split(":", each.key)[0]
  ssl_policy        = lookup(each.value, "certificates_arns", null) == null ? null : lookup(each.value, "ssl_policy", "ELBSecurityPolicy-FS-1-2-Res-2019-08")
  certificate_arn   = length(lookup(each.value, "certificates_arns", [])) > 0 ? each.value["certificates_arns"][0] : null
  dynamic "default_action" {
    for_each = { "default" = each.value["actions"]["default"] }
    content {
      type             = lookup(default_action.value, "type", "forward") 
      target_group_arn = lookup(default_action.value, "type", "forward") == "forward" ? aws_lb_target_group.tg[default_action.value.group].id : null
      dynamic "redirect" {
        for_each = toset(lookup(default_action.value, "type", "forward") == "redirect" ? ["default"] : [])
        content {
          port        = "443"
          protocol    = "HTTPS"
          status_code = "HTTP_301"
        }
      }
      dynamic "authenticate_cognito" {
        for_each = toset(lookup(default_action.value, "type", "forward") == "authenticate-cognito" ?  each.value["authenticate_cognito"] : []) 
        content {
          user_pool_arn = authenticate_cognito.value["user_pool_arn"]
          user_pool_client_id = authenticate_cognito.value["user_pool_client_id"]
          user_pool_domain = authenticate_cognito.value["user_pool_domain"]
        }
      }
    }
  }
}
Related