Nested dynamic blocks

I have a simple data structure:

{   
      allowed_methods        = ["GET", "HEAD", "OPTIONS"]
      cached_methods         = ["GET", "HEAD"]
      path_pattern           = "/requestaccount*"
      target_origin_id       = "Custom-www.crossref.org"
      viewer_protocol_policy = "redirect-to-https"
      lambda_function_association = {
        event_type   = "viewer-request"
        lambda_arn   = "${aws_lambda_function.lambda.arn}:${aws_lambda_function.lambda.version}"
        include_body = "false" 
      }
 },  
 {   
      allowed_methods        = ["GET", "HEAD", "OPTIONS"]
      cached_methods         = ["GET", "HEAD"]
      path_pattern           = "*"
      target_origin_id       = "Custom-www-old.crossref.org"
      viewer_protocol_policy = "redirect-to-https"
},

I’m trying to make the lambda arn configuration optional, my resources look like:

  dynamic "ordered_cache_behavior" {
    for_each = var.additional_behaviors

    content {
      allowed_methods             = ordered_cache_behavior.value.allowed_methods
      cached_methods              = ordered_cache_behavior.value.cached_methods
      path_pattern                = ordered_cache_behavior.value.path_pattern
      target_origin_id            = ordered_cache_behavior.value.target_origin_id
      viewer_protocol_policy      = ordered_cache_behavior.value.viewer_protocol_policy


      dynamic "lambda_function_association" {
        for_each = ordered_cache_behavior.value.lambda_function_association

        content {
          event_type   = lambda_function_association.value.event_type
          lambda_arn   = lambda_function_association.value.lambda_arn
          include_body = lambda_function_association.value.include_body
        }
      }
    }
  }

The problem here is that in the inner dynamic block, lambda_function_association.value is the actual value. It’s not an object that I can key into. So values get assigned randomly to the attributes.

I assume I need to adjust the data structure passed into the inner for_each but it’s not entirely clear to me how to do so.

Thoughts?

I believe Looping through nested list - #3 by caldwell2000 is similar

If I change the ds to:

    {
      allowed_methods        = ["GET", "HEAD", "OPTIONS"]
      cached_methods         = ["GET", "HEAD"]
      path_pattern           = "/requestaccount*"
      target_origin_id       = "Custom-www.crossref.org"
      viewer_protocol_policy = "redirect-to-https"
      lambda_function_association = {
        event_type   = "viewer-request"
        lambda_arn   = "${aws_lambda_function.lambda.arn}:${aws_lambda_function.lambda.version}"
        include_body = "false"
      }
    },
    {
      allowed_methods        = ["GET", "HEAD", "OPTIONS"]
      cached_methods         = ["GET", "HEAD"]
      path_pattern           = "*"
      target_origin_id       = "Custom-www-old.crossref.org"
      viewer_protocol_policy = "redirect-to-https"
      lambda_function_association = {}
    },

And resources to:

      dynamic "lambda_function_association" {
        for_each = ordered_cache_behavior.value["lambda_function_association"] != {} ? [1]: []

        content {
          event_type   = ordered_cache_behavior.value.lambda_function_association.event_type
          lambda_arn = ordered_cache_behavior.value.lambda_function_association.lambda_arn
          include_body = ordered_cache_behavior.value.lambda_function_association.include_body
        }
      }

It works, but it would be great to not include the variable at all.

Thoughts?