Reputation: 1242
First off - apologies - I’m extremely new (3 hours in!) to using terraform.
I am looking to try and use the value of a variable inside the declaration of another variable.
Below is my code - what am I doing wrong?
variables.tf:
variable "EnvironmentName" {
type = "string"
}
variable "tags" {
type = "map"
default = {
Environment = "${var.EnvironmentName}"
CostCentre = "C1234"
Project = "TerraformTest"
Department = "Systems"
}
}
Variables-dev.tfvars:
EnvShortName = "Dev"
EnvironmentName = "Development1"
#Location
Location = "westeurope"
main.tf:
resource “azurerm_resource_group” “TestAppRG” {
name = “EUW-RGs-${var.EnvShortName}”
location = “${var.Location}”
tags = “${var.tags}”
}
I am getting the following error:
Error: Variables not allowed on variables.tf line 18, in variable
“tags”: 18: Environment = “${var.EnvironmentName}”
Variables may not be used here.
I understand that the error message is fairly self explanatory and it is probably my approach that is wrong - but how do I use a variable in the definition of another variable map? is this even possible?
I will be standing up multiple resources - so want the tags to be built as a map and be passed into each resource - but I also want to recycle the map with other tfvars files to deploy multiple instances for different teams to work on.
Upvotes: 74
Views: 92480
Reputation: 1795
In addition to this question, as it often referred from a google request for a similar questions about how to "terraform local value based on another variable", here is the way which can be useful and related for such requests.
In case when you have one constant var which you can't change for a particular module (let's say var.env
) and you need to have another local value either 0 or 1 based on that variable value, then you need kind of switch based on another value. To achieve so in place:
env_prefix = var.env == "dev" ? "np" : "pd"
In this case env_prefix
will be np
if var.env
has value equal dev
. Otherwise env_prefix
will be pd
Upvotes: 2
Reputation: 4721
Terraform does not support variables inside a variable. If you want to generate a value based on two or more variables then you can try Terraform locals.
You can define the locals
like this:
locals {
tags = {
Environment = "${var.EnvironmentName}"
CostCentre = "C1234"
Project = "TerraformTest"
Department = "Systems"
}
}
And then you can access them using local.tags
:
resource “azurerm_resource_group” “TestAppRG” {
name = “EUW-RGs-${var.EnvShortName}”
location = “${var.Location}”
tags = “${local.tags}”
}
Upvotes: 77
Reputation: 811
You need to use locals to do the sort of transform you are after
variables.tf:
variable "EnvironmentName" {
type = "string"
}
locals.tf
locals {
tags = {
Environment = var.EnvironmentName
CostCentre = "C1234"
Project = "TerraformTest"
Department = "Systems"
}
}
Variables-dev.tfvars:
EnvShortName = "Dev"
EnvironmentName = "Development1"
#Location
Location = "westeurope"
main.tf:
resource “azurerm_resource_group” “TestAppRG” {
name = “EUW-RGs-${var.EnvShortName}”
location = var.Location
tags = local.tags
}
You can do things like `tags = merge(local.tags,{"key"="value"}) to extend your tags.
Upvotes: 8