Getting error when accessing attributes of an object

Hello, I’m fairly new to terraform but learning quickly. Our terraform project passes validation but on “plan” I get the following error:

Error: Attempt to get attribute from null value
on modules/cognitive-account/key-vault-key.tf line 3, in resource “azurerm_key_vault_key” “cognitive_account_key_vault_key”:
3: key_vault_id = local.cognitive_account.cognitive_account_key_vault_key.key_vault_id
├────────────────
│ local.cognitive_account.cognitive_account_key_vault_key is null

This value is null, so it does not have any attributes.

Error: Attempt to get attribute from null value

on modules/cognitive-account/key-vault-key.tf line 4, in resource “azurerm_key_vault_key” “cognitive_account_key_vault_key”:
4: key_type = local.cognitive_account.cognitive_account_key_vault_key.key_type
├────────────────
│ local.cognitive_account.cognitive_account_key_vault_key is null

This value is null, so it does not have any attributes.

cognitive_account_key_vault_key should be an object based on tfvars. I’ve made the modules available on github: GitHub - freyesicp/angry-aardvark-tf-cognitive-account

Why would cognitive_account_key_vault_key be empty?

Hi @watisup,

If your example is the extent of what you are using, cognitive_account_key_vault_key is null because you have not assigned any value to that attribute. You seem to be referencing customer_managed_key attributes which don’t exist, but don’t error because of the use of the try function, so the conditional expression results in null.

@jbardin I am not following. cognitive_account_key_vault_key should not be null, it’s defined in the cognitive-account.auto.tfvars. As for customer_managed_key, this should be getting defined in the root module, ./cognitive-account.tf

I have defined everything in cognitive-account.auto.tfvars, but I don’t understand why the values are not available.

You’re not using the variable value in the module call, you are using local.cognitive_accounts. That value is basing the existence of cognitive_account_key_vault_key on the expression:

try(length(cognitive_value.customer_managed_key) > 0, false)

And since customer_managed_key isn’t a valid attribute, you get false and assign null.

Thank you! Took a moment to realize that.