Error: Duplicate object key when create set

Hello! I have the code

module "github_team" {
  source = "../../modules/github-team"

  name         = "tr-${local.name}"
  description  = "Custom role for team ${local.name} "
  users        = { for each in var.members : local.iam_users[each].github_account => "member" if local.iam_users[each].github_account != "" }
  repositories = var.github_repositories
}

When i merge 2 account without github account, then i get error “Error: Duplicate object key”

Two different items produced the key " # doesn't have github account" in this
'for' expression. If duplicates are expected, use the ellipsis (...) after
the value expression to enable grouping by key.

I try rewrite code:

module "github_team" {
  foreach      = length(var.github_repositories) > 0 ? { enabled = true } : {}
  source       = "../../modules/github-team"
  name         = "tr-${local.name}"
  description  = "Custom role for team ${local.name} "
  users        = { for each in var.members : local.iam_users[each].github_account => "member" if local.iam_users[each].github_account != "" }
  repositories = var.github_repositories
}

moved {
  from = module.github_team
  to   = module.github_team["enabled"]
}

But error

Error: Unsupported argument

  on z-shared-iam-team-main.tf line 94, in module "github_team":
  94:   foreach      = length(var.github_repositories) > 0 ? { enabled = true } : 

An argument named "foreach" is not expected here.

Hi @patsevanton,

The last error is because of a typo in the module call, you have foreach, where as the argument is named for_each.

Regarding the first part, the key value of " # doesn't have github account" looks like an error, or at least is not useful. I would expect that it’s easier to filter that out beforehand, or within the for expression, and not have it in the data at all.

1 Like