Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for_each errors out for list of maps #22610

Closed
mechastorm opened this issue Aug 27, 2019 · 3 comments
Closed

for_each errors out for list of maps #22610

mechastorm opened this issue Aug 27, 2019 · 3 comments

Comments

@mechastorm
Copy link
Contributor

mechastorm commented Aug 27, 2019

I could not find a specific thread in the Github issue or in the Hashicorp community forum that mentions this issue with for_each and lists of maps. Let me know if this is a possible duplicate.

Terraform Version

Terraform v0.12.7
+ provider.github v2.2.0

Terraform Configuration Files

locals {
teams = [
  {
    "team_id" = "teamOne"
    "team_role" = "member"
    "username" = "abc"
  },
  {
    "team_id" = "teamTwo"
    "team_role" = "member"
    "username" = "xyz"
  }
]
}

resource "github_team_membership" "staff" {
  for_each = toset(local.teams)
  team_id  = each.value["team_id"]
  username = each.value["username"]
  role     = each.value["team_role"]
}

Expected Behavior

Create the resources in a for_each loop with the each.value being the map object of the item. In this case creating Github Team memnerships.

Actual Behavior

When running terraform plan, I am immediately given the error

Error: Invalid for_each argument

  on myfile.tf line ##, in resource "github_team_membership" "staff":
  ##:   for_each = local.teams

The given "for_each" argument value is unsuitable: the "for_each" argument
must be a map, or set of strings, and you have provided a value of type tuple.

Steps to Reproduce

Run plan the terraform code above

@mechastorm mechastorm changed the title for_each errors out with a list of maps for_each errors out for list of maps Aug 27, 2019
@rrijkse
Copy link

rrijkse commented Aug 27, 2019

for_each requires a map or just a list of strings, try this:

locals {
teams = {
  "teamOne" = {
    "team_role" = "member"
    "username" = "abc"
  },
  "teamTwo" = {
    "team_role" = "member"
    "username" = "xyz"
  }
}
}

resource "github_team_membership" "staff" {
  for_each = local.teams
  
  team_id  = each.key
  username = each.value["username"]
  role     = each.value["team_role"]
}

@mildwonkey
Copy link
Contributor

To reinforce @rrijkse's (already correct, thanks!) answer: https://www.terraform.io/docs/configuration/resources.html#for_each-multiple-resource-instances-defined-by-a-map-or-set-of-strings

Please note that we use GitHub issues for tracking bugs and enhancements rather than for questions. While we may be able to help with certain simple problems here it's generally better to use the community forum where there are far more people ready to help, whereas the GitHub issues here are generally monitored only by our few core maintainers.

@ghost
Copy link

ghost commented Sep 27, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Sep 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants