opticyclic
opticyclic

Reputation: 8106

How Do I Avoid Repeating A Variable In Terraform?

Terraform doesn't allow you to interpolate variables within the variables file otherwise you get the error:

Error: Variables not allowed

on variables.tf line 9, in variable "resource_group_name": 9:
default = "${var.prefix}-terraform-dev_rg"

Variables may not be used here.

This then means I end up duplicating the value of the prefix in my variables.tf file when I try to create the name for the resource group.

Is there a nice way around this to avoid duplicating the value of the variable?

variables.tf

variable "prefix" {
  description = "The prefix used for all resources in this plan"
  default     = "terraform-dev"
}

variable resource_group_name {
  type = "string"
  default = "terraform-dev_rg"
}

variable resource_group_location {
  type = "string"
  default = "eastus"
}

main.tf

# Configure the Microsoft Azure Provider
provider "azurerm" {
  version         = "=1.28.0"
}

# Create a resource group
resource "azurerm_resource_group" "resource-group" {
  name     = var.resource_group_name
  location = var.resource_group_location
}

#Create an application gateway with web app firewall
module "firewall" {
  source                  = "./firewall"
  resource_group_name     = var.resource_group_name
  resource_group_location = var.resource_group_location
}

./firewall/variables.tf

#Passed down from the root variables.tf
variable "prefix" {}
variable "resource_group_name" {}
variable "resource_group_location" {}

./firewall/main.tf

# Create a virtual network for the firewall
resource "azurerm_virtual_network" "firewall-vnet" {
  name = "${var.prefix}-waf-vnet"
  address_space = ["10.0.0.0/16"]
  resource_group_name = var.resource_group_name
  location            = var.resource_group_location
}

Upvotes: 1

Views: 2846

Answers (2)

Pradeep Bhadani
Pradeep Bhadani

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

Locals should help you here to achieve goal.

something like

variables.tf

variable "prefix" {
  description = "The prefix used for all resources in this plan"
  default     = "terraform-dev"
}

variable resource_group_location {
  type = "string"
  default = "eastus"
}

main.tf


locals {
  resource_group_name = "${var.prefix}_rg"
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
  version         = "=1.28.0"
}

# Create a resource group
resource "azurerm_resource_group" "resource-group" {
  name     = local.resource_group_name
  location = var.resource_group_location
}

Hope this helps.

Please read similar discussion here -https://stackoverflow.com/questions/58841060/terraform-variables-within-variables/58841360?noredirect=1#comment129460631_58841360

Upvotes: 0

RyanKim
RyanKim

Reputation: 1775

Try to use local values, https://www.terraform.io/docs/configuration/locals.html

variable "prefix" {
  description = "The prefix used for all resources in this plan"
  default     = "terraform-dev"
}

variable resource_group_location {
  type    = "string"
  default = "eastus"
}

locals {
  resource_group_name = "${var.prefix}_rg"
}

resource "azurerm_resource_group" "resource-group" {
  name     = local.resource_group_name
  location = var.resource_group_location
}

Upvotes: 3

Related Questions