How to deal with “Error: Duplicate object key” in Terraform

I am working on a scripts for creating a site hierarchy in Cisco DNA/Catalyst Center. I used an existing repo, that I’ve modified. I have structure that looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>├── main.tf
├── variables.tf
├── modules
│ └── site_settings
│ └── site.tf
│ └── variables.tf
├── Site-1
│ └── site-1.tfvars
</code>
<code>├── main.tf ├── variables.tf ├── modules │ └── site_settings │ └── site.tf │ └── variables.tf ├── Site-1 │ └── site-1.tfvars </code>
├── main.tf
├── variables.tf
├── modules 
│ └── site_settings 
│   └── site.tf 
│   └── variables.tf
├── Site-1
│ └── site-1.tfvars

In ./main.tf I have:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>provider "dnacenter" {
username = var.dnac_username
password = var.dnac_password
base_url = var.dnac_url
debug = "true"
ssl_verify = "false"
}
module "site_settings" {
# Using m0_general_settings module to configure general settings
source = "../modules/site_settings"
global_name = var.global_name
region_name = var.region_name
country_name = var.country_name
city_name = var.city_name
site_name = var.site_name
buildings = var.buildings
building_name = var.building_name
}
</code>
<code>provider "dnacenter" { username = var.dnac_username password = var.dnac_password base_url = var.dnac_url debug = "true" ssl_verify = "false" } module "site_settings" { # Using m0_general_settings module to configure general settings source = "../modules/site_settings" global_name = var.global_name region_name = var.region_name country_name = var.country_name city_name = var.city_name site_name = var.site_name buildings = var.buildings building_name = var.building_name } </code>
provider "dnacenter" {

    username = var.dnac_username
    password = var.dnac_password
    base_url = var.dnac_url
    debug = "true"
    ssl_verify = "false"
}

module "site_settings" {
  # Using m0_general_settings module to configure general settings
  source = "../modules/site_settings"

  global_name = var.global_name
  region_name = var.region_name
  country_name = var.country_name
  city_name = var.city_name
  site_name = var.site_name
  buildings = var.buildings
  building_name = var.building_name
}

The ./modules/site_settings/site.tf has:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> resource "dnacenter_area" "site" {
depends_on = [ dnacenter_area.city ]
provider = dnacenter
parameters {
site {
area {
name = var.site_name
parent_name = "${var.parent_name}/${var.global_name}/${var.country_name}/${var.city_name}"
}
}
type = "area"
}
}
resource "dnacenter_building" "building" {
depends_on = [ dnacenter_area.site ]
for_each = { for building in var.buildings : building.name => building }
provider = dnacenter
parameters {
site {
building {
name = "${each.value.name}"
parent_name = "${var.global_name}/${var.region_name}/${var.country_name}/${var.city_name}/${each.value.parent_name}"
latitude = "${each.value.latitude}"
longitude = "${each.value.longitude}"
}
}
type = "building"
}
}
resource "dnacenter_floor" "floor" {
depends_on = [ dnacenter_building.building ]
for_each = { for floor in var.floors : floor.name => floor }
provider = dnacenter
parameters {
site {
floor {
name = "${each.value.name}"
parent_name = "${var.global_name}/${var.region_name}/${var.country_name}/${var.city_name}/${each.value.parent_name}/${each.value.parent_building_name}"
}
}
type = "floor"
}
}
</code>
<code> resource "dnacenter_area" "site" { depends_on = [ dnacenter_area.city ] provider = dnacenter parameters { site { area { name = var.site_name parent_name = "${var.parent_name}/${var.global_name}/${var.country_name}/${var.city_name}" } } type = "area" } } resource "dnacenter_building" "building" { depends_on = [ dnacenter_area.site ] for_each = { for building in var.buildings : building.name => building } provider = dnacenter parameters { site { building { name = "${each.value.name}" parent_name = "${var.global_name}/${var.region_name}/${var.country_name}/${var.city_name}/${each.value.parent_name}" latitude = "${each.value.latitude}" longitude = "${each.value.longitude}" } } type = "building" } } resource "dnacenter_floor" "floor" { depends_on = [ dnacenter_building.building ] for_each = { for floor in var.floors : floor.name => floor } provider = dnacenter parameters { site { floor { name = "${each.value.name}" parent_name = "${var.global_name}/${var.region_name}/${var.country_name}/${var.city_name}/${each.value.parent_name}/${each.value.parent_building_name}" } } type = "floor" } } </code>
  resource "dnacenter_area" "site" {
    depends_on = [ dnacenter_area.city ]
    provider = dnacenter
    parameters {
      site {
        area {
          name = var.site_name
          parent_name = "${var.parent_name}/${var.global_name}/${var.country_name}/${var.city_name}"
        }
      }
    type = "area"
    }
  }
  
resource "dnacenter_building" "building" {
  depends_on = [ dnacenter_area.site ]
  for_each = { for building in var.buildings : building.name => building }
  provider = dnacenter
  parameters {
    site {
      building {
        name        = "${each.value.name}"
        parent_name = "${var.global_name}/${var.region_name}/${var.country_name}/${var.city_name}/${each.value.parent_name}"
        latitude    = "${each.value.latitude}"
        longitude   = "${each.value.longitude}"
      }
    }
  type = "building"
  }
}

resource "dnacenter_floor" "floor" {
  depends_on = [ dnacenter_building.building ]
  for_each = { for floor in var.floors : floor.name => floor }
  provider = dnacenter
  parameters {
    site {
      floor {
        name        = "${each.value.name}"
        parent_name = "${var.global_name}/${var.region_name}/${var.country_name}/${var.city_name}/${each.value.parent_name}/${each.value.parent_building_name}"
      }
    }
  type = "floor"
  }
}

These are the varialbe values in ./site-1.tfvars :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>dnac_username = "Cisco"
dnac_password = "Cisco123"
dnac_url = "https://my-dna-lab.test.net"
site_name = "Site-1"
buildings = [
{
building_name = "B01"
parent_site_name = "Site-1"
latitude = "xx.xxxx"
longitude = "xx.xxxx"
},
{
building_name = "B02"
parent_site_name = "Site-1"
latitude = "xx.xxxx"
longitude = "xx.xxxx"
},
]
floors = [
{
name = "F02"
parent_name = "Site-1"
parent_building_name = "B01"
},
{
name = "F03"
parent_name = "Site-1"
parent_building_name = "B01"
},
{
name = "F04"
parent_name = "Site-1"
parent_building_name = "B01"
},
{
name = "F03"
parent_name = "Site-1"
parent_building_name = "B02"
},
]
</code>
<code>dnac_username = "Cisco" dnac_password = "Cisco123" dnac_url = "https://my-dna-lab.test.net" site_name = "Site-1" buildings = [ { building_name = "B01" parent_site_name = "Site-1" latitude = "xx.xxxx" longitude = "xx.xxxx" }, { building_name = "B02" parent_site_name = "Site-1" latitude = "xx.xxxx" longitude = "xx.xxxx" }, ] floors = [ { name = "F02" parent_name = "Site-1" parent_building_name = "B01" }, { name = "F03" parent_name = "Site-1" parent_building_name = "B01" }, { name = "F04" parent_name = "Site-1" parent_building_name = "B01" }, { name = "F03" parent_name = "Site-1" parent_building_name = "B02" }, ] </code>
dnac_username = "Cisco"
dnac_password = "Cisco123"
dnac_url = "https://my-dna-lab.test.net"
site_name = "Site-1"
buildings = [
{
building_name = "B01"
parent_site_name = "Site-1"
latitude = "xx.xxxx"
longitude = "xx.xxxx"
},
{
building_name = "B02"
parent_site_name = "Site-1"
latitude = "xx.xxxx"
longitude = "xx.xxxx"
},
]
floors = [
{
name = "F02"
parent_name = "Site-1"
parent_building_name = "B01"
},
{
name = "F03"
parent_name = "Site-1"
parent_building_name = "B01"
},
{
name = "F04"
parent_name = "Site-1"
parent_building_name = "B01"
},
{
name = "F03"
parent_name = "Site-1"
parent_building_name = "B02"
},
]

These are the variables defined in ./variables.tf

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>variable "dnac_username" {
sensitive = true
}
variable "dnac_password" {
sensitive = true
}
variable "dnac_url" {
type = string
}
variable "site_name" {
type = string
}
variable "global_name" {}
variable "buildings" {
description = "List of buildings"
type = list(object({
name = string
parent_name = string
latitude = number
longitude = number
}))
}
variable "floors" {
description = "List of Floors"
type = list(object({
name = string
parent_name = string
parent_building_name = string
}))
}
</code>
<code>variable "dnac_username" { sensitive = true } variable "dnac_password" { sensitive = true } variable "dnac_url" { type = string } variable "site_name" { type = string } variable "global_name" {} variable "buildings" { description = "List of buildings" type = list(object({ name = string parent_name = string latitude = number longitude = number })) } variable "floors" { description = "List of Floors" type = list(object({ name = string parent_name = string parent_building_name = string })) } </code>
variable "dnac_username" {
    sensitive = true
}
variable "dnac_password" {
    sensitive = true
}
variable "dnac_url" {
  type = string
}
variable "site_name" {
  type = string
}
variable "global_name" {}
variable "buildings" {
  description = "List of buildings"
  type = list(object({
    name    = string
    parent_name = string
    latitude         = number
    longitude        = number
  }))
}
variable "floors" {
  description = "List of Floors"
  type = list(object({
    name    = string
    parent_name = string
    parent_building_name = string
  }))
}

And here are the vars in ./modules/site_settings/variables.tf:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>variable "region_name" {
type = string
}
variable "country_name" {
type = string
}
variable "city_name" {
type = string
}
variable "site_name" {
type = string
}
variable "global_name" {}
variable "ip_pools" {}
variable "floors" {}
variable "buildings" {}
</code>
<code>variable "region_name" { type = string } variable "country_name" { type = string } variable "city_name" { type = string } variable "site_name" { type = string } variable "global_name" {} variable "ip_pools" {} variable "floors" {} variable "buildings" {} </code>
variable "region_name" {
  type = string
}
variable "country_name" {
  type = string
}
variable "city_name" {
  type = string
}
variable "site_name" {
  type = string
}
variable "global_name" {}
variable "ip_pools" {}
variable "floors" {}
variable "buildings" {}

When I try to run the plan command, I get an error for duplicate values, since I have the same Floor name, which is in two different buildings.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Error: Duplicate object key
│ on modules/site_settings/site.tf line 90, in resource "dnacenter_floor" "floor":
90: for_each = { for floor in var.floors : floor.name => floor }
│ ├────────────────
│ │ floor.name is "F03"
│ Two different items produced the key "F03" in this 'for' expression. If duplicates are expected, use the ellipsis (...) after the value expression to enable grouping by key.
</code>
<code>Error: Duplicate object key │ │ on modules/site_settings/site.tf line 90, in resource "dnacenter_floor" "floor": │ 90: for_each = { for floor in var.floors : floor.name => floor } │ ├──────────────── │ │ floor.name is "F03" │ │ Two different items produced the key "F03" in this 'for' expression. If duplicates are expected, use the ellipsis (...) after the value expression to enable grouping by key. </code>
Error: Duplicate object key
│ 
│   on modules/site_settings/site.tf line 90, in resource "dnacenter_floor" "floor":
│   90:   for_each = { for floor in var.floors : floor.name => floor }
│     ├────────────────
│     │ floor.name is "F03"
│ 
│ Two different items produced the key "F03" in this 'for' expression. If duplicates are expected, use the ellipsis (...) after the value expression to enable grouping by key.

How can I make this work, and create the floors as below? Can I somehow reference also the building in the floors for loop, or refence floor by ID (which is still not known)?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> # module.site_settings.dnacenter_building.building["B01"] will be created
+ resource "dnacenter_building" "building" {
+ id = (known after apply)
+ item = (known after apply)
+ last_updated = (known after apply)
+ parameters {
+ type = "building"
+ site {
+ building {
+ address = (known after apply)
+ country = (known after apply)
+ latitude = xx.xxxx
+ longitude = xx.xxxx
+ name = "B01"
+ parent_name = "Global/EMEA/Spain/Madrid/Site-1"
}
}
}
}
# module.site_settings.dnacenter_building.building["B02"] will be created
+ resource "dnacenter_building" "building" {
+ id = (known after apply)
+ item = (known after apply)
+ last_updated = (known after apply)
+ parameters {
+ type = "building"
+ site {
+ building {
+ address = (known after apply)
+ country = (known after apply)
+ latitude = xx.xxxx
+ longitude = xx.xxxx
+ name = "B02"
+ parent_name = "Global/EMEA/Spain/Madrid/Site-1"
}
}
}
}
# module.site_settings.dnacenter_floor.floor["F02"] will be created
+ resource "dnacenter_floor" "floor" {
+ id = (known after apply)
+ item = (known after apply)
+ last_updated = (known after apply)
+ parameters {
+ type = "floor"
+ site {
+ floor {
+ floor_number = (known after apply)
+ height = (known after apply)
+ length = (known after apply)
+ name = "F02"
+ parent_name = "Global/EMEA/Spain/Madrid/Site-1/B01"
+ rf_model = (known after apply)
+ width = (known after apply)
}
}
}
}
# module.site_settings.dnacenter_floor.floor["F03"] will be created
+ resource "dnacenter_floor" "floor" {
+ id = (known after apply)
+ item = (known after apply)
+ last_updated = (known after apply)
+ parameters {
+ type = "floor"
+ site {
+ floor {
+ floor_number = (known after apply)
+ height = (known after apply)
+ length = (known after apply)
+ name = "F03"
+ parent_name = "Global/EMEA/Spain/Madrid/Site-1/B01"
+ rf_model = (known after apply)
+ width = (known after apply)
}
}
}
}
# module.site_settings.dnacenter_floor.floor["F03"] will be created
+ resource "dnacenter_floor" "floor" {
+ id = (known after apply)
+ item = (known after apply)
+ last_updated = (known after apply)
+ parameters {
+ type = "floor"
+ site {
+ floor {
+ floor_number = (known after apply)
+ height = (known after apply)
+ length = (known after apply)
** + name = "F03"
+ parent_name = "Global/EMEA/Spain/Madrid/Site-1/B02"**
+ rf_model = (known after apply)
+ width = (known after apply)
}
}
}
}
</code>
<code> # module.site_settings.dnacenter_building.building["B01"] will be created + resource "dnacenter_building" "building" { + id = (known after apply) + item = (known after apply) + last_updated = (known after apply) + parameters { + type = "building" + site { + building { + address = (known after apply) + country = (known after apply) + latitude = xx.xxxx + longitude = xx.xxxx + name = "B01" + parent_name = "Global/EMEA/Spain/Madrid/Site-1" } } } } # module.site_settings.dnacenter_building.building["B02"] will be created + resource "dnacenter_building" "building" { + id = (known after apply) + item = (known after apply) + last_updated = (known after apply) + parameters { + type = "building" + site { + building { + address = (known after apply) + country = (known after apply) + latitude = xx.xxxx + longitude = xx.xxxx + name = "B02" + parent_name = "Global/EMEA/Spain/Madrid/Site-1" } } } } # module.site_settings.dnacenter_floor.floor["F02"] will be created + resource "dnacenter_floor" "floor" { + id = (known after apply) + item = (known after apply) + last_updated = (known after apply) + parameters { + type = "floor" + site { + floor { + floor_number = (known after apply) + height = (known after apply) + length = (known after apply) + name = "F02" + parent_name = "Global/EMEA/Spain/Madrid/Site-1/B01" + rf_model = (known after apply) + width = (known after apply) } } } } # module.site_settings.dnacenter_floor.floor["F03"] will be created + resource "dnacenter_floor" "floor" { + id = (known after apply) + item = (known after apply) + last_updated = (known after apply) + parameters { + type = "floor" + site { + floor { + floor_number = (known after apply) + height = (known after apply) + length = (known after apply) + name = "F03" + parent_name = "Global/EMEA/Spain/Madrid/Site-1/B01" + rf_model = (known after apply) + width = (known after apply) } } } } # module.site_settings.dnacenter_floor.floor["F03"] will be created + resource "dnacenter_floor" "floor" { + id = (known after apply) + item = (known after apply) + last_updated = (known after apply) + parameters { + type = "floor" + site { + floor { + floor_number = (known after apply) + height = (known after apply) + length = (known after apply) ** + name = "F03" + parent_name = "Global/EMEA/Spain/Madrid/Site-1/B02"** + rf_model = (known after apply) + width = (known after apply) } } } } </code>
  # module.site_settings.dnacenter_building.building["B01"] will be created
  + resource "dnacenter_building" "building" {
      + id           = (known after apply)
      + item         = (known after apply)
      + last_updated = (known after apply)

      + parameters {
          + type = "building"

          + site {
              + building {
                  + address     = (known after apply)
                  + country     = (known after apply)
                  + latitude    = xx.xxxx
                  + longitude   = xx.xxxx
                  + name        = "B01"
                  + parent_name = "Global/EMEA/Spain/Madrid/Site-1"
                }
            }
        }
    }

  # module.site_settings.dnacenter_building.building["B02"] will be created
  + resource "dnacenter_building" "building" {
      + id           = (known after apply)
      + item         = (known after apply)
      + last_updated = (known after apply)

      + parameters {
          + type = "building"

          + site {
              + building {
                  + address     = (known after apply)
                  + country     = (known after apply)
                  + latitude    = xx.xxxx
                  + longitude   = xx.xxxx
                  + name        = "B02"
                  + parent_name = "Global/EMEA/Spain/Madrid/Site-1"
                }
            }
        }
    }



  # module.site_settings.dnacenter_floor.floor["F02"] will be created
  + resource "dnacenter_floor" "floor" {
      + id           = (known after apply)
      + item         = (known after apply)
      + last_updated = (known after apply)

      + parameters {
          + type = "floor"

          + site {
              + floor {
                  + floor_number = (known after apply)
                  + height       = (known after apply)
                  + length       = (known after apply)
                  + name         = "F02"
                  + parent_name  = "Global/EMEA/Spain/Madrid/Site-1/B01"
                  + rf_model     = (known after apply)
                  + width        = (known after apply)
                }
            }
        }
    }

  # module.site_settings.dnacenter_floor.floor["F03"] will be created
  + resource "dnacenter_floor" "floor" {
      + id           = (known after apply)
      + item         = (known after apply)
      + last_updated = (known after apply)

      + parameters {
          + type = "floor"

          + site {
              + floor {
                  + floor_number = (known after apply)
                  + height       = (known after apply)
                  + length       = (known after apply)
                  + name         = "F03"
                  + parent_name  = "Global/EMEA/Spain/Madrid/Site-1/B01"
                  + rf_model     = (known after apply)
                  + width        = (known after apply)
                }
            }
        }
    }

  # module.site_settings.dnacenter_floor.floor["F03"] will be created
  + resource "dnacenter_floor" "floor" {
      + id           = (known after apply)
      + item         = (known after apply)
      + last_updated = (known after apply)

      + parameters {
          + type = "floor"

          + site {
              + floor {
                  + floor_number = (known after apply)
                  + height       = (known after apply)
                  + length       = (known after apply)
**                  + name         = "F03"
                  + parent_name  = "Global/EMEA/Spain/Madrid/Site-1/B02"**
                  + rf_model     = (known after apply)
                  + width        = (known after apply)
                }
            }
        }
    }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật