Iterating through a list but already using for_each (0.12)

Hello,
I’m currently trying to use TF 0.12 to create AWS Organizations accounts. Right now I have a map of accounts with applicable info, here is an example where “Services” is the account name:

accountMap = {
…
  Services = {
    OU = [“Development”, “Production”]
  },
…
}

OU refers to the org units the account should be a part of. I’m currently already using for_each to loop through this map of account names, but I’m stuck on how to use the OUs as a suffix, so the org account name would become “Services-Development” and “Services-Production”. I have tried similar to the following:

resource “aws_organizations_account” “main” {
for_each = var.ouMap

  name     = "${each.key}-${var.accountMap["${each.value[*]}"]}"
  ...
}

However, “name” requires a string and I get an error since I am providing a list of the OUs. So, how can I either convert the list to a string one at a time, while in the same for_each iteration (but for my differing OUs)?

I’m open to other suggestions on best practice to map AWS Org accounts to multiple OUs as I’m still rather new to Terraform.

1 Like