Search code examples
terraformterraform-provider-awsterraform0.12+terraform-template-file

Terraform: nested for loop from yaml


I am trying to run nested for loop on terraform.
I have the following Yaml file:

Employees:
  - Department:
    - Dev:
      - name: "danielf"
        role: developer
        details:
          email  : [email protected]
          firstname  : daniel
          lastname   : folsik
      - name: "johnb"
        role: developer
        details:
          email  : [email protected]
          firstname  : john
          lastname   : belk
    - Ops:
      - name: "benol"
        role: devops
        details:
          email  : [email protected]
          firstname  : ben
          lastname   : olkin
      - name: "pauld"
        role: devops
        details:
          email  : [email protected]
          firstname  : paul
          lastname   : dempler

I am using locals to get the yaml data:

locals {
  ou_config = yamldecode(file("employees.yaml"))
}

I want to run into the list of objects on "Dev" and "Ops" lists using for_each.
for example, I want to run on the "Dev" list to get the following list of objects in the first iteration:

[
   {
      key   = "email"
      value = "[email protected]"
    },
    {
      key   = "firstname"
      value = "daniel"
    },
    {
      key   = "lastname"
      value = "folskin"
    }
  ]

The next run on the for_each will be:

[
   {
      key   = "email"
      value = "[email protected]"
    },
    {
      key   = "firstname"
      value = "john"
    },
    {
      key   = "lastname"
      value = "belk"
    }
  ]

etc.
How can I do it on terraform?


Solution

  • If I understand correctly, all you are trying to extract is the details portion of that yaml file ...

    Here is what I would do to get all:

    locals {
      ou_config = yamldecode(file("employees.yaml"))
    
      expanded_names = flatten([
        for e in local.ou_config.Employees : [
            for d in e.Department : [
                for key, person in d : [
                    for key, value in person : [
                        value.details
                    ]
                ]
            ]
        ]
      ])
    }
    
    output "test" {
        value = local.expanded_names
    }
    

    And if we want to filter we add an if key == "Dev"

    locals {
      ou_config = yamldecode(file("employees.yaml"))
    
      expanded_names = flatten([
        for e in local.ou_config.Employees : [
            for d in e.Department : [
                for key, person in d : [
                    for key, value in person : [
                        value.details
                    ]
                ] if key == "Dev"
            ]
        ]
      ])
    }
    
    output "test" {
        value = local.expanded_names
    }
    

    A terraform plan on that will look like:

    Changes to Outputs:
      + test = [
          + {
              + email     = "[email protected]"
              + firstname = "daniel"
              + lastname  = "folsik"
            },
          + {
              + email     = "[email protected]"
              + firstname = "john"
              + lastname  = "belk"
            },
        ]
    

    That format should be easier to loop in the final resource than the key value you suggested