Terraform accessing list elements from module output

terraform

Let's say I have a module, which generates some ids:
module.tf:

resource "random_id" "etcdapiserver-id" {
  byte_length         = 4
  count               = "${var.etcd_apiserver_count}"
}

module_output.tf:

output "etcdapiserver_hostname_list" {
  value = ["${random_id.etcdapiserver-id.*.hex}"]
}

It seems to work fine and the list ends up in the output successfully:

terraform output --module=module

etcdapiserver_hostname_list = [
    751adf6a,
    9e573ee7,
    edb94de3
]

Now I want to use elements from this list in main terraform config. Let's say I'm creating several compute instances on openstack:
main.tf:

resource "openstack_compute_instance_v2" "etcdapiserver" {
  count   = "3"
  name    = "etcdapi-node-${element(module.ignition.etcdapiserver_hostname_list.*, count.index)}"

But it would fail with

Error: resource 'openstack_compute_instance_v2.etcdapiserver' config:
"etcdapiserver_hostname_list.*" is not a valid output for module
"ignition"

Is there any way to do it? Thanks!

Best Answer

Terraform contributor answered my question on GitHub.

The general syntax for accessing the element of a list is list[index]. In your case, that would be something like module.ignition.etcdapiserver_hostname_list[count.index].

Related Topic