I have a nested map defined as follows:
variable "node_hosts" {
type = map(map(object({
name = string
compute_node = bool
etcd_instance = string
ipv4_address = string
})))
}
The above definition is contained in a variables.tf file, in a terraform.tfvars file in the same directory I assign a value to this:
node_hosts = {
arc = {
"control1" = {
name = "z-ca-arc-control1"
compute_node = false
etcd_instance = "etcd1"
ipv4_address = "12.345.678.90"
},
"control2" = {
name = "z-ca-arc-control2"
compute_node = false
etcd_instance = "etcd2"
ipv4_address = "12.345.678.91"
},
"compute1" = {
name = "z-ca-arc-compute1"
compute_node = true
etcd_instance = "etcd3"
ipv4_address = "12.345.678.92"
},
"compute2" = {
name = "z-ca-arc-compute2"
compute_node = true
etcd_instance = ""
ipv4_address = "12.345.678.93"
},
"compute3" = {
name = "z-ca-arc-compute3"
compute_node = true
etcd_instance = ""
ipv4_address = "12.345.678.94"
}
}
}
The code that dereferences this is contained in a main.tf file in the same directory:
locals {
all_nodes_verbose_etcd = [for k, v in var.node_hosts[terraform.workspace]:
format("%s ip=%s etcd_instance=%s", v.name, v.ipv4_address, v.etcd_instance)
if length(v.etcd_instance) > 0]
all_nodes_verbose = [for k, v in var.node_hosts[terraform.workspace]:
format("%s ip=%s", v.name, v.ipv4_address)
if length(v.etcd_instance) == 0]
master_nodes = [for k, v in var.node_hosts[terraform.workspace]:
v.name
if v.compute_node != true]
etcd_nodes = [for k, v in var.node_hosts[terraform.workspace]:
v.name
if length(v.etcd_instance) > 0]
all_nodes = values(var.node_hosts[terraform.workspace])[*].name
kubernetes_conf_file = format("%s/kubespray/inventory/%s/group_vars/k8s_cluster/k8s-cluster.yml", pathexpand("~"), terraform.workspace)
all_conf_file = format("%s/kubespray/inventory/%s/group_vars/all/all.yml", pathexpand("~"), terraform.workspace)
context_artifact = format("%s/kubespray/inventory/%s/artifacts/admin.conf", pathexpand("~"), terraform.workspace)
kubespray_inv_file = format("%s/kubespray/inventory/%s/inventory.ini", pathexpand("~"), terraform.workspace)
}
When I issue the commands:
> terraform workspace select arc
> terraform apply -auto-approve
In the same directory as the code, everything works fine, however when I invoke this from within a module
> terraform workspace select arc
> terraform apply -target=module.kubernetes_cluster -auto-approve
i.e. the code in the root module main.tf points to the main.tf with the locals declaration in it, I get a number of invalid index error message, which look like this:
Error: Invalid index
│
│ on modules/kubernetes_cluster/main.tf line 2, in locals:
│ 2: all_nodes_verbose_etcd = [for k, v in var.node_hosts[terraform.workspace]:
│ ├────────────────
│ │ terraform.workspace is "arc"
│ │ var.node_hosts is map of map of object with 1 element
Can someone please advise as to what I am doing wrong.