Iteration over a map of map to create resources

I am trying to create env variables on terraform cloud by looping through the map of variables for different accounts. I am stuck how to loop through the second map and create the variables. I expect it creates 10 variables byt appending the account id in the variables.
I tried to convert the map to a list but not able to convert the internal map
Is there any other simpler method ?


variable customSettings {
    default =  [
    {
      "awsAccountId" : "0123456789",
      "roleName" : "role-tfc"
    },
    {
      "awsAccountId" : "9876543210",
      "roleName" : "role-tfe"
    },
]
}
locals { // Locals for thee variables in the variable sets

  Envvars = {
    TFC_VAULT_RUN_ROLE = {
      envvalue    = "tfc-role"
      description = "The name of the Vault role to authenticate against. Created role, with jwt auth"
    }
    TFC_VAULT_PROVIDER_AUTH = {
      envvalue    = true
      description = "used  by Aws Vault provider"
    }
    TFC_VAULT_BACKED_AWS_MOUNT_PATH = {
      envvalue    = "aws-tfc"
      description = "mountpath in vault"
    }
    TFC_VAULT_BACKED_AWS_AUTH = {
      envvalue    = true
      description = "Using aws vault backend"
    }
   
    TFC_VAULT_BACKED_AWS_AUTH_TYPE = {
      envvalue    = "assumed_role"
      description = "how vault gets the aws creds"

    }

    

  }
}
locals {
    getv = length(var.customSettings) > 1 ? {
        for idx, acct  in var.customSettings: 
         idx =>  { for r, i in local.Envvars:
               r =>"${r}_tfc_${acct.awsAccountId}"
         }
    }:{}

        
}
 resource "tfe_variable" "VaultBackedawsRole" {
#   #count     = length((local.getv))
 for_each = {for idx,s in local.getv : idx => s}
   
 }

Was able to solve by havin the following code

locals {
  getv = length(var.customDeploymentSettings)>1 ? flatten ([
    for idx, acct in var.customDeploymentSettings :
    [
       for r, items in local.Envvars :
            { "${r}_tfe_${acct.awsAccountId}" = items.envvalue }            
    ]
  ]) :[]
 resource "tfe_variable" "VaultBackedawsRole" {

count     = length(local.getv)
   key          = element([for map in local.getv : keys(map)[0]], count.index)
    value        = element([for map in local.getv : values(map)[0]], count.index)
   category        = "env"
   workspace_id ="ws-CPLn6Zrc"
 }

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