Raxso
Raxso

Reputation: 35

Terraform: Combine Variables to make a list

locals{
instance_name = "TESTWINDOWSVM"
instance_count = 4
vm_instances = format("%s%s", local.instance_name,local.instance_count)
}

I am creating a windows VM via terraform azure, I wanted to combine instance_name and instance_count and be able to create a new list variable. output should be [ TESTWINDOWSVM001, TESTWINDOWSVM002, TESTWINDOWSVM003, TESTWINDOWSVM004]. is there a way to do this in terraform?

Upvotes: 0

Views: 126

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28739

You can do this with a straightforward for expression iterating from a range function inside a list constructor, and some string interpolation with a format function to ensure three numbers, and both on the return:

[for idx in range(local.instance_count) : "${local.instance_name}${format("%03d", idx + 1)}"]

Upvotes: 1

Related Questions