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

Adding new team to teams.csv returns error with terraform plan #1

Open
claflico opened this issue Dec 15, 2021 · 5 comments
Open

Adding new team to teams.csv returns error with terraform plan #1

claflico opened this issue Dec 15, 2021 · 5 comments

Comments

@claflico
Copy link

When trying to add a new team to the org by adding it to the CSV file I am getting the following error:

│ Error: Invalid for_each argument
│ 
│   on teams.tf line 15, in resource "github_team_membership" "members":
│   15:   for_each = { for tm in local.team_members : tm.name => tm }
│     ├────────────────
│     │ local.team_members will be known only after apply
│ 
│ The "for_each" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the
│ resources that the for_each depends on.

I get the error with or without the respective team csv file existing in the team-members folder.

If I add the team via the gui first and then import the team I'm able to get the plan and apply to work.

@grantbevis
Copy link

You can solve this by running terraform apply -target github_team.all first as per the documentation

https://learn.hashicorp.com/tutorials/terraform/github-user-teams?in=terraform/it-saas#apply-configuration

@PrinceAgrawal89
Copy link

I am also facing the same issue and terraform apply -target is definitely not a recommended way. Is there any other way to achieve this as it needs to be run through pipelines.

@devfalcon3
Copy link

Use count instead for_each working.

@bctosc
Copy link

bctosc commented Jun 19, 2023

@PrinceAgrawal89 @grantbevis I solved this, by creating a module, that creates the team and adds the members. This way it's easier to access the team ids.

A quick and dirty example :

# modules/team/main.tf
terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 5.0"
    }
  }
}

variable "name" {}
variable "description" {}
variable "privacy" {}
variable "create_default_maintainer" {
  type = bool
}
variable "members" {
  type = list(object({
    username = string
    role     = string
  }))
}

resource "github_team" "team" {
  name                      = var.name
  description               = var.description
  privacy                   = var.privacy
  create_default_maintainer = var.create_default_maintainer
}


resource "github_team_membership" "members" {
  count = length(var.members)

  team_id  = github_team.team.id
  username = element(var.members.*.username, count.index )
  role     = element(var.members.*.role, count.index )

}

And the main script:

# main file

module "dev_team" {
  source                    = "./modules/team"
  create_default_maintainer = false
  description               = "Developers"
  name                      = "devs"
  privacy                   = "closed"
  members = [
    {
      username = "user_a",
      role     = "maintainer"
    },
    {
      username = "user_b",
      role     = "member"
    }
  ]
}

all you have to do, is to change the locals so they're not using resources directly and then use them instead of the hardcoded values

@pmcpg
Copy link

pmcpg commented Jun 27, 2023

Use count instead for_each working.

@devfalcon3 I tried with following code which didnt help and I got the same error message as before.

# teams.tf
...
resource "github_team_membership" "members" {

  count = { for tm in local.team_members : tm.name => tm }

  team_id = local.team_members.team_id[count.index]
  username = local.team_members.username[count.index]
  role = local.team_members.role[count.index]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants