learner
learner

Reputation: 2860

Terraform reporting error in locals and variables in an attribute

I am having lookups.tf file with the below content

locals {
  roles_mapping = {
    web    = "we"
    app    = "as"
    db     = "db"
    queue  = "qu"
    stream = "st"
  }

  environment_lookup = {
    dev  = "d"
    test = "t"
    int  = "i"
    prod = "p"
    prd  = "p"
    uat  = "a"
    poc  = "d"
    dr   = "r"
    lab  = "l"
  }

  region_lookup = {
    "Australia East"      = "aae"
    "Australia Southeast" = "aas"
  }

  region_pairs = {
    "Australia East"      = "Australia Southeast"
    "Australia Southeast" = "Australia East"
  }

  lookup_result = lookup(var.environment_lookup, var.environment)

  tags = merge(
    data.azurerm_resource_group.tf-azrg.tags, {
      Directory      = "bento.com",
      PrivateDNSZone = var.private_dns_zone,
      Immutable      = "False",
      ManagedOS      = "True",
    }
  )
}

data "azurerm_log_analytics_workspace" "log_analytics" {
  name                = "abc-az-lad1"
  resource_group_name = "abc-dev-aae"
}

Now, I have a file called variables.tf

variable "environment" {
  description = "The name of environment for the AKS Cluster"
  type        = string
  default     = "dev"
}

variable "identifier" {
  description = "The identifier for the AKS Cluster"
  type        = number
  default     = 001
}

Now, I want to refer lookups.tf and variable.tf in name in main.tf as below

resource "azurerm_log_analytics_workspace" "tf-alaw" {
  name                = var.tla-la-local.lookup_result-var.identifier
  location            = data.azurerm_resource_group.tf-azrg.location
  resource_group_name = data.azurerm_resource_group.tf-azrg.name
  sku                 = var.la_sku
  retention_in_days   = 30
}

However, I get an error like the below:

Error: Reference to the undeclared input variable

  on ..\..\..\terraform-azurerm-aks\lookups.tf line 32, in locals:
  32:   lookup_result = lookup(var.environment_lookup, var.environment)

An input variable with the name "environment_lookup" has not been declared.
This variable can be declared with a variable "environment_lookup" {} block.

Error: Reference to the undeclared input variable

  on ..\..\..\terraform-azurerm-aks\main.tf line 2, in resource "azurerm_log_analytics_workspace" "tf-alaw":
   2:   name                = var.tla-la-local.lookup_result-var.identifier

An input variable with the name "tla-la-local" has not been declared. This
variable can be declared with a variable "tla-la-local" {} block.

Do note that the name below, what I expect is var.tla refers to a variable then string/text -la- now local.lookup_result is a value from locals and then again a text - finally referring another variable var.identifier

Upvotes: 1

Views: 1304

Answers (1)

Marko E
Marko E

Reputation: 18203

You have to use a different syntax when locals block is defined. This should be added instead of what you currently have:

lookup_result = lookup(local.environment_lookup, var.environment)

More information and examples here: https://www.terraform.io/docs/language/values/locals.html#using-local-values.

EDIT1: I just saw your note under the error output. So, when you are concatenating multiple variables you still have to use the "old" syntax, i.e., similar to what was used in Terraform 0.11.x. Here's how that value needs to look like:

"${var.tla}-la-${local.lookup_result}-${var.identifier}"

Upvotes: 1

Related Questions