0
votes

Now I am in process of migrating to Terraform 12. One of the main feature here is more strict HCL2 and I like it.

For example if I have list of maps:

  elb_listeners = [
     {
       instance_port     = 80
       instance_protocol = "http"
       lb_port           = 80
       lb_protocol       = "http"
     },
     {
       instance_port      = 443
       instance_protocol  = "http"
       lb_port            = 443
       lb_protocol        = "https"
       ssl_certificate_id = ARN certificate
     }

I can spin it with dynamic block like:

  dynamic "listener" {
    for_each = [for listener in var.elb_listeners : {
      instance_port     = listener.instance_port
      instance_protocol = listener.instance_protocol
      lb_port           = listener.lb_port
      lb_protocol       = listener.lb_protocol
    }]

    content {
      instance_port     = listener.value.instance_port
      instance_protocol = listener.value.instance_protocol
      lb_port           = listener.value.lb_port
      lb_protocol       = listener.value.lb_protocol
    }
  }

But lets suppose I have only one block and only one block possible:

  health_check {
    healthy_threshold   = 2
    unhealthy_threshold = 2
    timeout             = 3
    target              = "HTTP:8000/"
    interval            = 30
  }

I don't wish to declare all of these variables, I also want to describe this as block once in settings. Yes, here I also can use dynamic_block with one element only but it will look as an overkill here. Is it possible to do that? Like

  health_check {
    var.health_check
  }

or something.

1

1 Answers

1
votes

I get where you are going. But I don't think it is currently possible to "expand" a map inside a resource for example. You could define a map variable with default values, although this is more lines of code.

variable health_check {
    type = "map"
    default = {
        healthy_threshold   = 2
        unhealthy_threshold = 2
        timeout             = 3
        target              = "HTTP:8000/"
        interval            = 30
    }
}

health_check {
    healthy_threshold   = var.health_check.healthy_threshold
    unhealthy_threshold = var.health_check.unhealthy_threshold
    timeout             = var.health_check.timeout
    target              = var.health_check.target
    interval            = var.health_check.interval
}

On a second thought, I don't see why you would need it. It makes your code structure less readable and less explicit. So the options are to define it explicitly with or without default values or use for_each when you have multiple resources to create from a list/map.