I am having a " Can't access attributes on a list of objects" error

I have a working module where it will create Azure Storage account and another module will assign a role for the Azure Storage Account scope.
Here is my code:

> module "tf-state-storage" {
>   source                   = "../terraform-resources/azure-tfsate-storage-account"
>   storage_account_name     = var.storage_account_name
>   resource_group_name      = module.tf-resource-group.name
>   location                 = var.location
>   tags                     = var.tags
>   account_replication_type = var.account_replication_type
>   account_tier             = var.account_tier
>   container_name           = var.container_name
> }
> 

> module "tfstate_role_assignment" {
>   source         = "../terraform-resources/azure-role-assignment"
>   principal_id   = module.user-assigned-identity.user_assinged_identity_principal_id
>   role_name      = "storage"
>   scope_id       = module.tf-state-storage.id
>   principal_type = var.principal_type
> }

This was working fine, until I added count for the module tf-state-storage
Which is as shown below:

> module "tf-state-storage" {
>   source                   = "../terraform-resources/azure-tfsate-storage-account"
>   count                    = 5
>   storage_account_name     = element(var.storage_account_name, count.index)
>   resource_group_name      = module.tf-resource-group.name
>   location                 = var.location
>   tags                     = var.tags
>   account_replication_type = var.account_replication_type
>   account_tier             = var.account_tier
>   container_name           = var.container_name
> }
> 
> module "tfstate_role_assignment" {
>   source         = "../terraform-resources/azure-role-assignment"
>   principal_id   = module.user-assigned-identity.user_assinged_identity_principal_id
>   role_name      = "Storage"
>   scope_id       = module.tf-state-storage.id
>   principal_type = var.principal_type
> }

Now I am having this attributes error where the count of total no of storage accounts needs to be passed for the proper or appropriate scope_id that’s being referred from the module tfstate_role_assignment

The error is:

â•·
│ Error: Unsupported attribute
│
│ on main.tf line 78, in module “tfstate_role_assignment”:
│ 78: scope_id = module.tf-state-storage.id
│ ├────────────────
│ │ module.tf-state-storage is a list of object
│
│ Can’t access attributes on a list of objects. Did you mean to access attribute “id” for a specific element of the list, or across all elements of the list?
╵

How can I fix this weird relationship here? Appreciate the help

In your module “tfstate_role_assignment”, add count = 5 and then change the scope to scope_id = module.tf-state-storage[count.index].id

Didn’t worked mate. It throwed an Unresolved reference tf-state-storage

â•·
│ Error: Invalid reference
│ 
│   on main.tf line 79, in module "tfstate_role_assignment":
│   79:   scope_id       = module[count.index].tf-state-storage.id
│ 
│ The "module" object cannot be accessed directly. Instead, access one of its attributes.

First of all, it’s giving you error because you cannot access the attribute of a module in array. So you need to loop to get that attribute.

You need to output storage account id first in your module tf-state-storage. Then in the role assignment module, you use the count which is the loop and then access the scope_id with module.tf-state-storage[count.index].storage_account_id

I can write what it would look like in full and send it to you.

Oh no my bad, it should be module.tf-state-storage[count.index].id

Giving that id is the storage account resource id outputted from the storage account kodule

1 Like

Yup, I didn’t realized that (rookie here). This worked mate.

We’re all once a rookie at some point. Glad it worked for you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.