'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?
Solution 1:[1]
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)}"]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Matt Schuchard |
