pkaramol
pkaramol

Reputation: 19422

Attribute of custom module not available in terraform

I have created locally a custom module that just replicates the vault_identity_entity official module.

Here it is more or less

resource "vault_identity_entity" "this" {
  name              = var.name
  policies          = var.policies
  metadata          = var.metadata
  disabled          = var.disabled
  external_policies = var.external_policies
}

In the corresponding outputs.tf file, I have this

output "entity" {
  description = "The entity created"
  value       = vault_identity_entity.this
}

I am now trying to retrieve its id attribute as follows

module "identities_memberships" {

  source   = "../../../../path/to/identity_group_member_entity_ids"
  for_each = {
    for item in local.memberships: item.member_email => {
      group = item.group
    }
 }

  member_entity_ids = [module.identity_entities[each.key].id]
  group_id          = module.identity_groups[each.value.group].id

}

This fails as follows:

│ Error: Unsupported attribute
│
│   on main.tf line 101, in module "identities_memberships":
│  101:   member_entity_ids = module.identity_entities[each.key].id
│     ├────────────────
│     │ each.key is a string
│     │ module.identity_entities is a map of object
│
│ This object does not have an attribute named "id".

The vaule of local_memberships as printed when I set it in the outputs

  + local_memberships = [
      + {
          + group_name   = "admins"
          + member_email = "[email protected]"
        },
      + {
          + group_name   = "admins"
          + member_email = "[email protected]"
        },
      + {
          + group_name   = "operators"
          + member_email = "[email protected]"
        },
      + {
          + group_name   = "viewers"
          + member_email = "[email protected]"
        },
    ]

and the module.identity_identities is the instantiation of the above module

module "identity_entities" {

  source   = "../../../../path/to/identity_entity"
  for_each = local.member_groups

  name = each.key
  depends_on = [
    module.identity_groups
  ]
}

where local.member_groups:

+ local_member_groups = {
  + "[email protected]" = [
      + "viewers",
    ]
  + "[email protected]"    = [
      + "admins",
    ]
  + "[email protected]"            = [
      + "admins",
    ]
  + "[email protected]"        = [
      + "operators",
    ]
}

Why can't I access the id attribute? What am I missing?

If I comment out the section that produces the error, the plan shows me it will create for example (among others) this:

  # module.identity_entities["[email protected]"].vault_identity_entity.this will be created
  + resource "vault_identity_entity" "this" {
      + disabled          = false
      + external_policies = false
      + id                = (known after apply)
      + name              = "[email protected]"
    }

However when also hardcoding values as follows

module "identities_memberships" {

  source = "../../../../modules-terraform/vault/identity_group_member_entity_ids"
  for_each = {
    for item in local.memberships : item.member_email => {
      group = item.group
    }
  }

  member_entity_ids = [module.identity_entities["[email protected]"].id]
  group_id          = module.identity_groups["admins"].id

  depends_on = [
    module.identity_entities
  ]
}

same error, (now for the other module as well)

│
│   on main.tf line 101, in module "identities_memberships":
│  101:   member_entity_ids = [module.identity_entities["[email protected]"].id]
│     ├────────────────
│     │ module.identity_entities["[email protected]"] is a object
│
│ This object does not have an attribute named "id".
╵
╷
│ Error: Unsupported attribute
│
│   on main.tf line 102, in module "identities_memberships":
│  102:   group_id          = module.identity_groups["admins"].id
│     ├────────────────
│     │ module.identity_groups["admins"] is a object
│

Upvotes: 0

Views: 459

Answers (1)

Marcin
Marcin

Reputation: 238867

To access id of the entity, you have to:

module.identity_entities["[email protected]"].entity.id

Upvotes: 1

Related Questions