admin管理员组

文章数量:1122846

I am getting the below error while using flatten function

Error: Invalid index
│
│   on helpers.tf line 11, in locals:
│   11:   ]) : "${x.app_region}_${x.app_environment}" => x }["${var.APP_REGION}_${var.APP_ENVIRONMENT}"]
│     ├────────────────
│     │ local.regions_defaults is object with 2 attributes
│     │ local.regions_manifest is object with 2 attributes
│     │ var.APP_ENVIRONMENT is "dv"
│     │ var.APP_REGION is "eastus"

My Code :

region_manifest.tf

locals {
  regions_defaults = {
    env_short_name = "uat"
    instance_number = "001"
  }
}

locals {
  regions_manifest = {
    euz = {
      azure_region        = "eastus"
      environments = {
        dv = {
            env_short_name = "dev"
            instance_number = "001"
        }
        tst = {
          env_short_name = "tst"
          instance_number = "002"
         }

      }

    }
    wuz = {
      azure_region        = "westus"
      environments = {
        dv = {
            env_short_name = "dev"
            instance_number = "001"
        }
        tst = {
          env_short_name = "tst"
          instance_number = "002"
         }

      }

    }

  }
}

helpers.tf file

locals {
  region = { for x in flatten([
    for region_key, region in local.regions_manifest : [
      for environment_key, environment in region.environments : merge(local.regions_defaults, region, environment, {
        app_region                           = region_key
        app_environment                      = environment_key
        environments                         = null
        regions                              = null
      })
    ]
  ]) : "${x.app_region}_${x.app_environment}" => x }["${var.APP_REGION}_${var.APP_ENVIRONMENT}"]
}

providers.tf

terraform {
  required_version = "~> 1.0"
  
  required_providers {
    azuread = "~> 3.0"  
    azurerm = "~> 4.0"
  }
  }



  provider "azurerm" {
  tenant_id       = var.tenant_id
  client_id       = var.client_id
  client_secret   = var.client_secret
  subscription_id = var.subscription_id
  features {}
}

variables.tf

variable "APP_ENVIRONMENT" {
  description = "Environment: dev, qa,test, prod,hub,spoke,stage"
  default = "dv" #remove these when configured at Space-lift
  validation {
    condition = contains(["dv","tst","qa","uat","prod","dr"],var.APP_ENVIRONMENT)
    error_message = "Valid values for ENVIRONMENT are dv,tst,qa,uat,prod,dr"
  }
}

variable "APP_REGION" {
  description = "Azure Location (see: )"
  default = "eastus"  #remove these when configured at Space-lift
}


variable "client_id" {
  default = "12344"
}

variable "client_secret" {
  default = "abcde"
}

variable "tenant_id" {
  default = "2341-4569-09439-0987"
}

variable "subscription_id" {
  default = "456hfy8792"
}

application.tf

module "resource_group" {
  source   = "./modules/resource_group"
  for_each = toset(module.naming.rg_name_patterns)
  rg_name  = each.value
  location = local.region.azure_region
  tags     = local.tags
}

module

Service Abbreviation

variable "abbreviation" {
  default = "rg"
}

variable "location" {
  default = ""
}

variable "rg_name" {}

Optional

variable "tags" {}

resource "azurerm_resource_group" "resource_group" {
  name     = var.rg_name
  location = var.location
  tags     = var.tags

lifecycle {
    ignore_changes = [
      tags["CreatedOnDate"]
    ]
  }
}

I am getting the below error while using flatten function

Error: Invalid index
│
│   on helpers.tf line 11, in locals:
│   11:   ]) : "${x.app_region}_${x.app_environment}" => x }["${var.APP_REGION}_${var.APP_ENVIRONMENT}"]
│     ├────────────────
│     │ local.regions_defaults is object with 2 attributes
│     │ local.regions_manifest is object with 2 attributes
│     │ var.APP_ENVIRONMENT is "dv"
│     │ var.APP_REGION is "eastus"

My Code :

region_manifest.tf

locals {
  regions_defaults = {
    env_short_name = "uat"
    instance_number = "001"
  }
}

locals {
  regions_manifest = {
    euz = {
      azure_region        = "eastus"
      environments = {
        dv = {
            env_short_name = "dev"
            instance_number = "001"
        }
        tst = {
          env_short_name = "tst"
          instance_number = "002"
         }

      }

    }
    wuz = {
      azure_region        = "westus"
      environments = {
        dv = {
            env_short_name = "dev"
            instance_number = "001"
        }
        tst = {
          env_short_name = "tst"
          instance_number = "002"
         }

      }

    }

  }
}

helpers.tf file

locals {
  region = { for x in flatten([
    for region_key, region in local.regions_manifest : [
      for environment_key, environment in region.environments : merge(local.regions_defaults, region, environment, {
        app_region                           = region_key
        app_environment                      = environment_key
        environments                         = null
        regions                              = null
      })
    ]
  ]) : "${x.app_region}_${x.app_environment}" => x }["${var.APP_REGION}_${var.APP_ENVIRONMENT}"]
}

providers.tf

terraform {
  required_version = "~> 1.0"
  
  required_providers {
    azuread = "~> 3.0"  
    azurerm = "~> 4.0"
  }
  }



  provider "azurerm" {
  tenant_id       = var.tenant_id
  client_id       = var.client_id
  client_secret   = var.client_secret
  subscription_id = var.subscription_id
  features {}
}

variables.tf

variable "APP_ENVIRONMENT" {
  description = "Environment: dev, qa,test, prod,hub,spoke,stage"
  default = "dv" #remove these when configured at Space-lift
  validation {
    condition = contains(["dv","tst","qa","uat","prod","dr"],var.APP_ENVIRONMENT)
    error_message = "Valid values for ENVIRONMENT are dv,tst,qa,uat,prod,dr"
  }
}

variable "APP_REGION" {
  description = "Azure Location (see: https://azure.microsoft.com/en-us/explore/global-infrastructure/geographies/#overview)"
  default = "eastus"  #remove these when configured at Space-lift
}


variable "client_id" {
  default = "12344"
}

variable "client_secret" {
  default = "abcde"
}

variable "tenant_id" {
  default = "2341-4569-09439-0987"
}

variable "subscription_id" {
  default = "456hfy8792"
}

application.tf

module "resource_group" {
  source   = "./modules/resource_group"
  for_each = toset(module.naming.rg_name_patterns)
  rg_name  = each.value
  location = local.region.azure_region
  tags     = local.tags
}

module

Service Abbreviation

variable "abbreviation" {
  default = "rg"
}

variable "location" {
  default = ""
}

variable "rg_name" {}

Optional

variable "tags" {}

resource "azurerm_resource_group" "resource_group" {
  name     = var.rg_name
  location = var.location
  tags     = var.tags

lifecycle {
    ignore_changes = [
      tags["CreatedOnDate"]
    ]
  }
}
Share Improve this question edited Nov 22, 2024 at 12:48 Pallab asked Nov 21, 2024 at 22:14 PallabPallab 2,2994 gold badges34 silver badges52 bronze badges 1
  • Have you tried printing the output when using the flatten function? @Pallab – Jahnavi Commented Nov 22, 2024 at 3:56
Add a comment  | 

1 Answer 1

Reset to default 0

In your scenario, you can use lookup() function available in terraform to retrieve a specific value from a Terraform map based on a specific given key.

Syntax: lookup(map, key, default_value)

lookup retrieves the value of a single element from a map, given its key. If the given key does not exist, the given default value is returned instead.

Below is the helpers.tf code after few modifications:

locals {
  region = { for x in flatten([
    for region_key, region in local.regions_manifest : [
      for environment_key, environment in region.environments : merge(local.regions_defaults, region, environment, {
        app_region                           = region_key
        app_environment                      = environment_key
        environments                         = null
        regions                              = null
      })
    ]
  ]) : "${x.app_region}_${x.app_environment}" => x
  }
picked_region = lookup(local.region, "${var.APP_REGION}_${var.APP_ENVIRONMENT}", null)
}

Output:

You can also refer here for more Terraform usecases using lookup() function.

本文标签: azureError with flatten function while using TerraformStack Overflow