Duplicate object key - Using a datasource of AzureDevops Groups - How to use ellipsis (…)

Hi am getting this error while creating a datasource of AzureDevops Groups using for loop. How to use ellipsis (…) in this case.

Error: Duplicate object key

│ on data.tf line 22, in data “azuredevops_group” “security_groups”:
│ 20: for_each = {
│ 21: for index, r in var.ado_repositories_details :
│ 22: r.ado_security_group => r
│ 23: }
│ ├────────────────
│ │ r.ado_security_group is “devops-team”

│ Two different items produced the key “devops-team” in this ‘for’
│ expression. If duplicates are expected, use the ellipsis (…) after the
│ value expression to enable grouping by key.

My tf code - data.tf

data "azuredevops_group" "security_groups" {
  for_each = {
    for index, r in var.ado_repositories_details :
    r.ado_security_group => r
  }
  project_id = data.azuredevops_project.my_project.id
  name       = each.value.ado_security__group
}

vars.tf

variable "ado_repositories_details" {
  description = "Details of Azure DevOps Git repos to create"
  type = list(object({
    name              = string
    ado_security_group = string
  }))
}

tfvars

ado_repositories_details = [
  {
    name              = "test_repo_1"
    ado_security_group = "devops-team"
  },
  {
    name              = "test_repo_2"
    ado_security_group = "devops-team"
  }

Use of ellipsis here would mean creating a complicated data structure, only to ignore most of it entirely.

I think something like

data "azuredevops_group" "security_groups" {
  for_each = toset(var.ado_repositories_details[*].ado_security_group)

  project_id = data.azuredevops_project.my_project.id
  name       = each.value
}

would be more appropriate.

1 Like

thanks @maxb - works fine!