Problem with terraform host sets

Hello community!

Im trying write base tf module for boundary and i have a problem with host sets.

My tf code:

resource "boundary_host_catalog_static" "host_catalog" {
  for_each    = { for v in local.host_catalogs: join("/", [v.org_key, v.project_key, v.host_catalog_key]) => v }
  name        = each.value.host_catalog_key
  description = each.value.host_catalog_key
  scope_id    = boundary_scope.projects[join("/", [each.value.org_key, each.value.project_key])].id
}

resource "boundary_host_static" "static_host" {
  for_each        = { for v in local.hosts: join("/", [v.org_key, v.project_key, v.host_catalog_key, v.fqdn_key, v.host_set_key, v.ip,]) => v }
  name            = each.value.fqdn_key
  description     = each.value.fqdn_key
  address         = each.value.ip
  host_catalog_id = boundary_host_catalog_static.host_catalog[join("/", [each.value.org_key, each.value.project_key, each.value.host_catalog_key])].id
}

resource "boundary_host_set_static" "host_set" {
  for_each        = { for v in local.host_sets: join("/", [v.org_key, v.project_key, v.host_catalog_key, v.host_set_key]) => v }
  name            = each.value.host_set_key
  description     = each.value.host_set_key
  host_catalog_id = boundary_host_catalog_static.host_catalog[join("/", [each.value.org_key, each.value.project_key, each.value.host_catalog_key])].id
  host_ids        = [ for v in local.hosts: boundary_host_static.static_host[join("/", [v.org_key, v.project_key, v.host_catalog_key, v.fqdn_key, v.host_set_key, v.ip,])].id ]

With static host_catalog data boundary_host_set_static works well, but when i try to make it iterable, i have tf error:

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # boundary_host_set_static.host_set["MY_ORG1/PROJECT1/CATALOG1/HOST_SET1"] will be created
  + resource "boundary_host_set_static" "host_set" {
      + description     = "HOST_SET1"
      + host_catalog_id = "hcst_eJkwlkHzYx"
      + host_ids        = [
          + "hst_l7Le3QUo7Q",
        ]
      + id              = (known after apply)
      + name            = "HOST_SET1"
      + type            = "static"
    }

  # boundary_host_set_static.host_set["MY_ORG1/PROJECT1/CATALOG1/HOST_SET2"] will be created
  + resource "boundary_host_set_static" "host_set" {
      + description     = "HOST_SET2"
      + host_catalog_id = "hcst_yt5Ddq5vye"
      + host_ids        = [
          + "hst_l7Le3QUo7Q",
        ]
      + id              = (known after apply)
      + name            = "HOST_SET2"
      + type            = "static"
    }

Plan: 2 to add, 0 to change, 0 to destroy.

boundary_host_set_static.host_set["MY_ORG1/PROJECT1/CATALOG1/HOST_SET1"]: Creating...
boundary_host_set_static.host_set["MY_ORG1/PROJECT1/CATALOG1/HOST_SET2"]: Creating...
boundary_host_set_static.host_set["MY_ORG1/PROJECT1/CATALOG1/HOST_SET1"]: Creation complete after 0s [id=hsst_0KbkkFDvIb]
╷
│ Error: error setting hosts on host set: {"kind":"Internal", "message":"host_sets.(Service).setInRepo: Unable to set hosts in host set: static.(Repository).SetSetMembers: db.DoTx: static.(Repository).SetSetMembers: static.createMembers: db.CreateItems: insert or update on table \"static_host_set_member\" violates foreign key constraint \"static_host_set_member_catalog_id_host_id_fkey\": integrity violation: error #1003"}
│ 
│   with boundary_host_set_static.host_set["MY_ORG1/PROJECT1/CATALOG1/HOST_SET2",
│   on main.tf line 166, in resource "boundary_host_set_static" "host_set":
│  166: resource "boundary_host_set_static" "host_set" {
│ 

Hey @Qstarnik,

Just took a quick look, if you are still having issues I can dig into the TF a bit later this week. But looking at the plan output, both host_sets you are creating are trying to set the same host_ids

host_ids = [
 "hst_l7Le3QUo7Q",
]

But each host_set has a different host_catalog id. This will cause an error when trying to insert due to the the foreign key in the host set membership table to the static_host

foreign key (catalog_id, host_id)
  references static_host (catalog_id, public_id)
1 Like

Thank you @louisruch for fast reply. I got it. :pray: