Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use for_each with list of objects

I’m trying to create azure key vault access policies to multiple users. I have tried the following way. But I’m getting an error.

This is how my vars.tf looks like

variable "mycustomvar" {
  type = list(object({
    description        = string
    object_id          = string
    permissions        = list(string)
  }))
  default = [
    {
       description        = "One"
       object_id          = "abc123"
       secret_permissions = ["Get", "List"]
    },
    {
       description        = "Two"
       object_id          = "def345"
       secret_permissions = ["Get"]
    }
  ]
}

And my resource.tf

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

resource "azurerm_key_vault_access_policy" "policy" {
  key_vault_id        = azurerm_key_vault.test.id
  tenant_id           = "123"
   
  for_each            = var.mycustomvar
    
  object_id           = each.value
  secret_permissions  = each.value
}

And getting the below error.

The given "for_each" argument value is unsuitable: the "for_each" argument must be a map, or set of strings, and you have provided a value of type list of object.

>Solution :

Even though I think this is a duplicate, the way you can achieve this is:

resource "azurerm_key_vault_access_policy" "policy" {
  for_each            = {for i in var.mycustomvar: i.description => i}
  key_vault_id        = azurerm_key_vault.test.id
  tenant_id           = "123"
  object_id           = each.value.object_id
  secret_permissions  = each.value.secret_permissions
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading