# Nested dynamic blocks

**URL:** <https://discuss.hashicorp.com/t/nested-dynamic-blocks/31559>\
**Category:** Terraform\
**Created:** [November 5, 2021, 6:44pm UTC](https://discuss.hashicorp.com/t/nested-dynamic-blocks/31559 "2021-11-05T18:44:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![throwaway8787](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/throwaway8787/32/1378_2.png) [@throwaway8787](https://discuss.hashicorp.com/u/throwaway8787)\
**Post date:** [November 5, 2021, 6:44pm UTC](https://discuss.hashicorp.com/t/nested-dynamic-blocks/31559/1 "2021-11-05T18:44:35Z")

</div>

I have a simple data structure:

```auto
{   
      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:

```auto
  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?

---

<div class="post-metadata">

**Author:** ![throwaway8787](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/throwaway8787/32/1378_2.png) [@throwaway8787](https://discuss.hashicorp.com/u/throwaway8787)\
**Post date:** [November 5, 2021, 6:47pm UTC](https://discuss.hashicorp.com/t/nested-dynamic-blocks/31559/2 "2021-11-05T18:47:01Z")

</div>

I believe [Looping through nested list - #3 by caldwell2000](https://discuss.hashicorp.com/t/looping-through-nested-list/13457/3) is similar

---

<div class="post-metadata">

**Author:** ![throwaway8787](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/throwaway8787/32/1378_2.png) [@throwaway8787](https://discuss.hashicorp.com/u/throwaway8787)\
**Post date:** [November 5, 2021, 7:45pm UTC](https://discuss.hashicorp.com/t/nested-dynamic-blocks/31559/3 "2021-11-05T19:45:15Z")

</div>

If I change the ds to:

```auto
    {
      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:

```auto
      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?
