Evandro Pomatti
Evandro Pomatti

Reputation: 15094

How to dynamically generate a new variable in Terraform when using "random" resources (integer/string)?

How to dynamically generate a new variable in Terraform when using random_integer?

I have the following code in my main.tf file.

How can I concatenate random_integer.ri.result + local.project into a new variable, so I can use the calculated value in other resources?

locals {
  project_name = "myproject"
}

resource "random_integer" "ri" {
  min = 10000
  max = 99999
}

resource "azurerm_resource_group" "default" {
  name     = "rg-${local.project_name}-${random_integer.ri.result}" // <-- avoid this
  location = "eastus"
}

Upvotes: 0

Views: 1906

Answers (1)

Mark B
Mark B

Reputation: 200486

Just create another local like:

locals {
  project_name = "myproject",
  random_project_name = "${local.project_name}-${random_integer.ri.result}"
}

Or just add the random integer right into the project name like:

locals {
  project_name = "myproject-${random_integer.ri.result}"
}

Upvotes: 2

Related Questions