Please how do I simplify this configuration using locals, the code works fine as is but gets complex passing the variables manually each time.
VARIABLES:
variable "vm_all" {
type = list(object({}))
default = [
{
name = "vm1"
rg = "rg1"
sn = "sn1"
sn_prefix = ["10.0.1.0/24"]
},
{
name = "vm2"
rg = "rg2"
sn = "sn2"
sn_prefix = ["10.0.2.0/24"]
},
{
name = "vm3"
rg = "rg3"
sn = "sn3"
sn_prefix = ["10.0.3.0/24"]
}
]
}
CURRENT ITERATION USING LOCALS:(requires manually mapping the variables as shown above)
resource "example_resource" "resource1" {
for_each = {for vm_all in var.vm_all: vm_all.name => vm_all }
name = each.value.name
rg = each.value.rg
sn = each.value.sn
sn_prefix = each.value.sn_prefix
}
DESIRED METHOD OF PASSING VARIABLES:
variable "name" {
default = [
"vm1",
"vm2",
"vm3"
]
}
variable "rg_names" {
default = [
"rg1",
"rg2",
"rg3"
]
}
variable "subnets" {
default = [
"sn1",
"sn2",
"sn3"
]
}
variable "subnet_prefixes" {
default = [
"sn_prefix1",
"sn_prefix2",
"sn_prefix3"
]
}
QUESTION: How can I use locals in a more effective way to allow passing the variables as lists shown above and avoid the need to map manually?
2
Answers
Thank you @Marcin, really helpful but I'm not yet there, I feel so close though, I get the following error when I try to created nics using ids from the created subnets:
(The given key does not identify an element in this collection value.)
see the main.tf below:
You can combine them as follows:
then