terraformterraform-provider-azureterragrunt

Not able to pass inputs as array to Terraform


Using the below code, I am able to pass object id as array in terraform but not able to pass from Terragrunt input file and I have the following error .

cloud - Azure , service - Key vault

Main.tf

    dynamic "access_policy" {    
      for_each = toset(flatten([    
        for policy in var.access_policies : [    
          for object_id in policy.object_id : {    
            object_id = object_id    
            secret_permissions = policy.secret_permissions    
            key_permissions = policy.key_permissions    }    ]    ]))
    content {    
    tenant_id = data.azurerm_client_config.current.tenant_id    
    object_id = access_policy.value.object_id    
    secret_permissions = access_policy.value.secret_permissions    
    key_permissions = access_policy.value.key_permissions    }    }
    variable "access_policies" {    
    type = set(object({    
       object_id = set(string),    
       secret_permissions = set(string),    
       key_permissions = set(string)    }))

terragrunt.hcl

    inputs = {    
    access_policies = [    { object_id = ["xyz", "abc"], secret_permissions = ["Get", "Set"], key_permissions = ["Get"] }    
    { object_id = include.env.locals.env_vars.locals.object_id1, secret_permissions = ["Get"], key_permissions = ["Get"]     }    ]    }

env.hcl

    locals    
      { object_id1 = toset(["3db"]) }

current error:

Planning failed. Terraform encountered an error while generating this plan.  
350│ Error: Invalid value for input variable  
351│  
352│ on variables.tf line 28:  
353│ 28: variable "access_policies" {  
354│  
355│ Unsuitable value for var.access_policies set using the  
356│ TF_VAR_access_policies environment variable: element 0: attribute  
357│ "object_id": string required.

I have not defined TF_VAR_access_policies anywhere in my code..

Expected Behavior:

To Pass array value from input file of terragrunt to terraform .


Solution

  • Passing inputs as array to Terraform from Terragrunt file

    The blocker you're facing is because of the way you define object_ids in each policy where it requires to be set of strings but the way you're defining is different i.e., the format or type of the input doesn't match what Terraform expects.

    To overcome this I have a sample configuration which matches your requirement.

    main.tf:

    provider "azurerm" {
      features {}
    }
    
    data "azurerm_client_config" "current" {}
    
    resource "azurerm_key_vault" "example" {
      name                = "vksbbkeyvault"
      location            = "West US2"
      resource_group_name = "vinay-rg"
      tenant_id           = data.azurerm_client_config.current.tenant_id
      sku_name            = "standard"
    
      dynamic "access_policy" {
        for_each = toset(flatten([
          for policy in var.access_policies : [
            for object_id in policy.object_id : {
              object_id = object_id
              secret_permissions = policy.secret_permissions
              key_permissions = policy.key_permissions
            }
          ]
        ]))
    
        content {
          tenant_id        = data.azurerm_client_config.current.tenant_id
          object_id        = access_policy.value.object_id
          secret_permissions = access_policy.value.secret_permissions
          key_permissions    = access_policy.value.key_permissions
        }
      }
    }
    
    variable "access_policies" {
      type = set(object({
        object_id          = set(string)
        secret_permissions = set(string)
        key_permissions    = set(string)
      }))
    }
    

    terragrunt.hcl:

    terraform {
      source = "./" 
    }
    
    inputs = {
      access_policies = [
        {
          object_id         = ["1xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx1", "2xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx2"]
          secret_permissions = ["Get", "Set"]
          key_permissions    = ["Get"]
        },
        {
          object_id         = ["3xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx3"]
          secret_permissions = ["Get"]
          key_permissions    = ["Get"]
        }
      ]
    }
    

    deployment:

    enter image description here

    enter image description here

    Reference:

    https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault

    https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks

    https://developer.hashicorp.com/terraform/tutorials/configuration-language/locals

    https://terragrunt.gruntwork.io/docs/features/inputs/