I am trying to write a variable type for below configuration
virtual_machines = {
nodes = {
app1_node1 = {
"vm_name" = "app"
"vm_num" = "1"
networks = {
nic1 = {
"nic_name" = "app-1"
"subnet" = "****"
},
}
},
service1_node1 = {
"vm_name" = "service"
"vm_num" = "1"
networks = {
nic1 = {
"nic_name" = "service-1"
"subnet" = "****"
},
}
},
db1_node1 = {
"vm_name" = "db"
"vm_num" = "1"
"as_name" = "app-avset"
networks = {
nic1 = {
"nic_name" = "db-1-1"
"subnet" = "****"
},
nic2 = {
"nic_name" = "db-1-2"
"subnet" = "/subscriptions/************/resourceGroups/pluto/providers/Microsoft.Network/virtualNetworks/pluto-infra/subnets/db-subnet"
}
}
},
db2_node2 = {
"vm_name" = "db"
"vm_num" = "2"
"as_name" = "app-avset"
networks = {
nic1 = {
"nic_name" = "db-2-1"
"subnet" = "****"
},
nic2 = {
"nic_name" = "db-2-2"
"subnet" = "/subscriptions/************/resourceGroups/pluto/providers/Microsoft.Network/virtualNetworks/pluto-infra/subnets/db-subnet"
},
}
},
}
}
I have tried with below variable type, however it is not working as expected.
variable "virtual_machines" {
type = map(object({
nodes = map(object({
vm_name = string
vm_num = string
as_name = optional(string)
networks = map(object({
nic_name = string
subnet = string
}))
}))
}))
default = {}
}
Error :
│ Error: Invalid value for input variable
│
│ on main.tf line 4, in module "virtualmachine":
│ 4: virtual_machines = var.virtual_machines
│
│ The given value is not suitable for module.virtualmachine.var.virtual_machines declared at virtualmachine\variables.tf:6,1-28: element "nodes": attribute
│ "nodes" is required.
Could someone assist on how to write the correct type for the given configuration ? Note : The default should be empty if don't have requirement to create virtual machines.
I do agree with
Olakunle Abiola
The error you encountered was caused by an invalid structure in both tfvars and variable.
Here is the updated terraform code to create VMs using the variable and tfvar file.
terraform.tfvars
variables.tf
Main.tf
Terraform apply:
After running the code, the
VMs
were successfully created in the Azure portal