rdagumampan
rdagumampan

Reputation: 519

Combine terraform locals variables

Can you guide me on how to combine locals? This is not working though some online docs advise to frame this way. Appreciate your help, thanks!

# see README.md for developer guide
# prepare subscription where resources are created
locals {
  location_code           = "weu"
  environment_code        = "test"
}

locals {
  kv_name = "oamp-kv-${local.environment_code}-${location_code}"
  ai_name = "oamp-ai-${local.environment_code}-${location_code}"
}

# prepare azure rm configuration
provider "azurerm" {
    version = "~>2.15.0"
    use_msi = true
    features {}
}

Validation

Error: Invalid reference

  on main.tf line 20, in locals:
  20:   kv_name = "oamp-kv-${local.environment_code}-${location_code}"

A reference to a resource type must be followed by at least one attribute
access, specifying the resource name.

Upvotes: 9

Views: 14700

Answers (1)

Thomas Betous
Thomas Betous

Reputation: 5123

You can reference local variables into locals as it is written in the documentation.

As shown above, local values can be referenced from elsewhere in the module with an expression like local.common_tags, and locals can reference each other in order to build more complex values from simpler ones.

The error come from the fact you need to prefix with an attribute access your resource. However you did not prefix location_code.

In you code you miss to prefix access attribute local before location_code.

What you need to do is prefix correctly your variables :

# see README.md for developer guide
# prepare subscription where resources are created
locals {
  location_code           = "weu"
  environment_code        = "test"
}

locals {
  kv_name = "oamp-kv-${local.environment_code}-${local.location_code}"
  ai_name = "oamp-ai-${local.environment_code}-${local.location_code}"
}

# prepare azure rm configuration
provider "azurerm" {
    version = "~>2.15.0"
    use_msi = true
    features {}
}

Upvotes: 14

Related Questions