# Iterate ove yaml

**URL:** <https://discuss.hashicorp.com/t/iterate-ove-yaml/36752>\
**Category:** Terraform\
**Created:** [March 8, 2022, 2:42pm UTC](https://discuss.hashicorp.com/t/iterate-ove-yaml/36752 "2022-03-08T14:42:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexanderWiechert](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/alexanderwiechert/32/25220_2.png) [@AlexanderWiechert](https://discuss.hashicorp.com/u/AlexanderWiechert)\
**Post date:** [March 8, 2022, 2:42pm UTC](https://discuss.hashicorp.com/t/iterate-ove-yaml/36752/1 "2022-03-08T14:42:13Z")

</div>

Hi,

I have the folowing yaml structure given and need to iterate over it vie some nested for loops. How can I achive this.

```auto
aws:
  ciss-goesaws-shared-services:
    name: "ciss-goesaws-shared-services"
    id: "277015281744"
    roles:
      AdministratorAccess:
        name: "AdministratorAccess"
        pipelines:
          custom-labor:
            name: "custom-labor"
            namespace: all
            project: all
          vault-auth01-labor:
            namespace: 110
            project: 207
          pipeline-includes-ubuntu-aws:
            namespace: 107
          project: 232
      Billing:
        name: "Billing"
      ViewOnlyAccess:
        name: "ViewOnlyAccess"

```

I tried with this. But how can I access the 3rd level values?

```auto
locals {
  roles = flatten([
    for a, roles in local.aws_accounts : [
      for r, pipeline in roles.roles : [
        for pl in pipeline.pipelines : {
          account_name = a.name
          account_id = a.id
          role_name = r
          pipeline_name = pl
        }
      ]
    ]
  ])
}

```

```auto
for_each = {
for s in local.roles : "custom--auth01--${s.account_id}-${s.account_name}--${s.role_name}--${s.pipeline_name}" => s
}

```

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [March 8, 2022, 4:33pm UTC](https://discuss.hashicorp.com/t/iterate-ove-yaml/36752/2 "2022-03-08T16:33:08Z")

</div>

Hi @AlexanderWiechert,

Inside a `flatten` argument like that you can use as many nested levels of list as you need to traverse through levels of the data structure. `flatten` will stop flattening at the first level where it encounters a non-list, like the nested objects in the example you shared.

You can then include all of the various `for` expression iterator values inside the nested objects in order to track what each leaf item was nested inside.

---

<div class="post-metadata">

**Author:** ![AlexanderWiechert](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/alexanderwiechert/32/25220_2.png) [@AlexanderWiechert](https://discuss.hashicorp.com/u/AlexanderWiechert)\
**Post date:** [March 9, 2022, 12:26pm UTC](https://discuss.hashicorp.com/t/iterate-ove-yaml/36752/3 "2022-03-09T12:26:04Z")

</div>

I’ve changed a a bit the yaml structure, but I’m totaly stucked on how o acess this nested values.

---

<div class="post-metadata">

**Author:** ![AlexanderWiechert](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/alexanderwiechert/32/25220_2.png) [@AlexanderWiechert](https://discuss.hashicorp.com/u/AlexanderWiechert)\
**Post date:** [March 10, 2022, 11:19am UTC](https://discuss.hashicorp.com/t/iterate-ove-yaml/36752/4 "2022-03-10T11:19:31Z")

</div>

I now run into “A null value cannot be used as the collection in a ‘for’ expression.” if there are now values. How can I achive reading the pipeline names if there are some and ignore if there are no values? I just want to create those who have values and ignore the others.

```auto
│ on modules/vault/aws-ciss.tf line 7, in locals:
│ 6: for role, pl in id.roles : [
│ 7: for p in pl.pipelines : {
│ 8: account_name = name
│ 9: account_id = id.accountid
│ 10: role_name = role
│ 11: pipeline_name = p
│ 12: }
│ 13: ]
│ ├────────────────
│ │ pl.pipelines is null

```
