Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform variables within variables

Tags:

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.

like image 538
Fazer87 Avatar asked Nov 13 '19 16:11

Fazer87


People also ask

How do you use variables in variables in Terraform?

All files in your Terraform directory using the . tf file format will be automatically loaded during operations. Create a variables file, for example, variables.tf and open the file for edit. Add the below variable declarations to the variables file.

How do you pass multiple variables in Terraform?

There are two methods supported for doing this using the Terraform CLI when running Terraform commands. -var flag enables a single input variable value to be passed in at the command-line. -var-file flag enables multiple input variable values to be passed in by referencing a file that contains the values.

What are three types of variables that exist within Terraform?

Lists, maps, and objects are the three most common complex variable types.

What is Terraform parallelism?

Parallelism is one of the environment variable with a number flag range between 1 and 256 , the default value is 10 . Parallelism is used to fix infrastructure providers that error on concurrent operations or use during non-standard rate limiting, when you execute terraform plan and terraform apply at runtime.


Video Answer


1 Answers

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 (https://www.terraform.io/docs/configuration/locals.html).

Locals should help you here to achieve goal.

something like

locals { tags = {         Environment = "${var.EnvironmentName}"         CostCentre = "C1234"         Project = "TerraformTest"         Department = "Systems"        } } 

And then you can use as local.tags

resource “azurerm_resource_group” “TestAppRG” {     name = “EUW-RGs-${var.EnvShortName}”     location = “${var.Location}”     tags = “${local.tags}” } 

Hope this helps

like image 148
pradeep Avatar answered Oct 01 '22 03:10

pradeep