# Optional Attributes in For Loop of For\_each

**URL:** <https://discuss.hashicorp.com/t/optional-attributes-in-for-loop-of-for-each/5565>\
**Category:** Terraform\
**Created:** [January 31, 2020, 11:24pm UTC](https://discuss.hashicorp.com/t/optional-attributes-in-for-loop-of-for-each/5565 "2020-01-31T23:24:12Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![jmgreg31](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jmgreg31/32/3915_2.png) [@jmgreg31](https://discuss.hashicorp.com/u/jmgreg31)\
**Post date:** [February 1, 2020, 1:21am UTC](https://discuss.hashicorp.com/t/optional-attributes-in-for-loop-of-for-each/5565/2 "2020-02-01T01:21:30Z")

</div>

Ok, so searching through a combination of [this](https://discuss.hashicorp.com/t/pattern-to-handle-optional-dynamic-blocks/2384/2) post and [this](https://github.com/hashicorp/terraform/issues/19853) post, I got to a solution that solves both issues. Hope this helps others who may come across this:

```auto
dynamic "origin" {
    for_each = [for i in "${var.dynamic_custom_origin_config}" : {
      name = i.domain_name
      id = i.origin_id
      path = i.origin_path
      http_port = i.http_port
      https_port = i.https_port
      origin_keepalive_timeout = i.origin_keepalive_timeout
      origin_read_timeout = i.origin_read_timeout
      origin_protocol_policy = i.origin_protocol_policy
      origin_ssl_protocols = i.origin_ssl_protocols
      custom_header = lookup(i, "custom_header", null)
    }]
    content {
      domain_name = origin.value.name
      origin_id = origin.value.id
      origin_path = origin.value.path
      dynamic "custom_header" {
        for_each = origin.value.custom_header == null ? [] : [ for i in origin.value.custom_header : {
          name = i.name
          value = i.value
        }]

```

---

_[View the full topic](https://discuss.hashicorp.com/t/optional-attributes-in-for-loop-of-for-each/5565)._
