Dynamic Nested Block Question: How to iterate a map / make block optional?

Hi,

I’m trying to configure a dynamic access block for the “access_logs” block in the resource “aws_lb” (an application load balancer). My goal is to make the block optional, so that if no parameters are defined for the block, the inline block is omitted completely from the resource “aws_lb” block.

My configuration is currently as follows:

child module

alb resource

resource "aws_lb" "application_loadbalancer" {
  name               = "${var.prefix}${var.lb_name}"
  internal           = var.lb_internal
  load_balancer_type = "application"
...

  dynamic "access_logs" {
    for_each = var.access_logs
    content {
      enabled = lookup(var.access_logs, "enabled", true)
      bucket  = lookup(var.access_logs, "bucket", null)
      prefix  = lookup(var.access_logs, "prefix", null)
    }
  }

child module variable declaration

variable "access_logs" {
  description = "An access logs block"
  type        = map(any)
  default     = {}
}

root module

module "alb_int" {
  source = "../../terraform-library/module-alb"

   access_logs = {
    enabled = true
    bucket  = "my-bucket"
    prefix  = "my-loadbalancer"
  }

But this throws an error:

Error: Too many access_logs blocks

  on  line 0:
  (source code not available)

No more than 1 "access_logs" blocks are allowed

I also tried with:

 dynamic "access_logs" {
    for_each = var.access_logs
    content {
      enabled = lookup(access_logs.value, "enabled", true)
      bucket  = lookup(access_logs.value, "bucket", null)
      prefix  = lookup(access_logs.value, "prefix", null)
    }
  }

But this throws an Error: Invalid Function Argument “Invalid value for “inputMap” parameter: lookup() requires a map as the first
argument.”

How would it be possible to iterate with the map itself as the “argument” and not the keys/values?

Many thanks in advance!
Graham

I had a similar challenge yesterday, and got a response here:

Hi Svenie,

thanks for the tip, I did read that post and try with splat, but both variations of dynamic configuration block result in the same error:

Error: Error in function call

  on ..\..\terraform-library\module-alb\alb.tf line 20, in resource "aws_lb" "application_loadbalancer":
  20:       enabled = lookup(var.access_logs, "enabled")
    |----------------
    | var.access_logs is empty map of dynamic

Call to function "lookup" failed: lookup failed to find 'enabled'.


Error: Error in function call

  on ..\..\terraform-library\module-alb\alb.tf line 21, in resource "aws_lb" "application_loadbalancer":
  21:       bucket  = lookup(var.access_logs, "bucket")
    |----------------
    | var.access_logs is empty map of dynamic

Call to function "lookup" failed: lookup failed to find 'bucket'.


Error: Error in function call

  on ..\..\terraform-library\module-alb\alb.tf line 22, in resource "aws_lb" "application_loadbalancer":
  22:       prefix  = lookup(var.access_logs, "prefix")
    |----------------
    | var.access_logs is empty map of dynamic

Call to function "lookup" failed: lookup failed to find 'prefix'.