Terraform: formatlist is not working

Created on 12 Jul 2016  ยท  3Comments  ยท  Source: hashicorp/terraform

I have the following code, and I am getting an error regarding formatlist. Why wouldn't this be working?

Terraform: v0.6.16
OS: Mac

Code:

resource "template_file" "etcd-write_files" {
  template = "${file("config/write_files/etcd.yml")}"
}

resource "template_file" "etcd-units" {
  template = "${file("config/units/etcd.yml")}"
}

module "etcd-coreos-user-data" {
  depends_on                        = ["google_compute_address.etcd.*"]
  source                            = "git::https://github.com/brandfolder/terraform-coreos-user-data.git?ref=master"
  etcd2_initial-cluster             = "${join(",", formatlist("%s=https://%s:%s/", google_compute_address.etcd.*.name, google_compute_address.etcd.*.address, "2380"))}"
  etcd2_advertise-client-urls       = "http://var!private_ipv4:2379,http://var!private_ipv4:4001"
  etcd2_initial-advertise-peer-urls = "http://var!private_ipv4:2380,http://var!private_ipv4:7001"
  etcd2_listen-client-urls          = "http://0.0.0.0:2379,http://0.0.0.0:4001"
  etcd2_listen-peer-urls            = "http://var!private_ipv4:2380,http://var!private_ipv4:7001"
  etcd2_debug                       = "true"
  flannel_interface                 = "var!private_ipv4"
  fleet_metadata                    = "role=etcd"
  fleet_public_ip                   = "var!private_ipv4"
  fleet_engine_reconcile_interval   = "10"
  fleet_etcd_request_timeout        = "5.0"
  fleet_agent_ttl                   = "120s"
  write_files                       = "${template_file.etcd-write_files.rendered}"
  units                             = "${template_file.etcd-units.rendered}"
}

resource "google_compute_disk" "etcd" {
  count = "${var.etcd-count}"
  name  = "${replace("${var.prefix}-etcd-${count.index}", "/^-/", "")}"
  zone  = "${element(split(",", var.zones), count.index % length(split(",", var.zones)))}"
  image = "${coalesce(var.etcd-image, var.default-image)}"
  type  = "pd-ssd"
  size  = 100

  lifecycle {
    # Ignore images changes so that we dont ever delete the disk when updating
    # to a later version of CoreOS
    ignore_changes = ["image"]
  }
}

resource "google_compute_address" "etcd" {
  count  = "${var.etcd-count}"
  name   = "${replace("${var.prefix}-etcd-${count.index}", "/^-/", "")}"
  region = "${replace(element(split(",", var.zones), count.index % length(split(",", var.zones))), "/-[a-z]$/", "")}"
}

resource "google_compute_instance" "etcd" {
  count        = "${var.etcd-count}"
  name         = "${replace("${var.prefix}-etcd-${count.index}", "/^-/", "")}"
  description  = "Etcd master"
  machine_type = "${coalesce(var.etcd-instance-type, var.default-instance-type)}"
  zone         = "${element(split(",", var.zones), count.index % length(split(",", var.zones)))}"

  tags = ["etcd"]

  disk {
    disk        = "${element(google_compute_disk.etcd.*.name, count.index)}"
    auto_delete = false
  }

  scheduling {
    automatic_restart   = true
    on_host_maintenance = "MIGRATE"
  }

  network_interface {
    subnetwork = "${element(google_compute_subnetwork.primary.*.name, count.index % length(split(",", var.zones)))}"

    access_config {
      nat_ip = "${element(google_compute_address.etcd.*.address, count.index)}"
    }
  }

  metadata {
    user-data = "${module.etcd-coreos-user-data.user-data}"
  }

  service_account {
    scopes = ["userinfo-email", "compute-ro", "storage-ro"]
  }
}

Error:

* formatlist: no lists in arguments to formatlist in:

${join(",", formatlist("%s=https://%s:%s/", google_compute_address.etcd.*.name, google_compute_address.etcd.*.address, "2380"))}
* formatlist: no lists in arguments to formatlist in:
bug core

Most helpful comment

Just hit this error in Terraform 0.7.2. Here is a tiny example to repro:

data "template_file" "foo" {
  template = "${join(", ", formatlist("%s", var.list))}"
}

variable "list" {
  type = "list"
  default = []
}

output "foo" {
  value = "${data.template_file.foo.rendered}"
}

When you run it, you get:

Errors:

  * formatlist: no lists in arguments to formatlist in:

${formatlist("%s", var.list)}

If you change the default value for list to [1, 2, 3] and run it again, you get:

Outputs:

foo = 1, 2, 3

The cause is logic in this function that counts the number of items in the list(s) passed to formatlist. The problem is that 0 items could mean "you didn't pass in any lists" (which is a legitimate error) or "your lists are empty" (which should NOT be an error).

All 3 comments

Just hit this error in Terraform 0.7.2. Here is a tiny example to repro:

data "template_file" "foo" {
  template = "${join(", ", formatlist("%s", var.list))}"
}

variable "list" {
  type = "list"
  default = []
}

output "foo" {
  value = "${data.template_file.foo.rendered}"
}

When you run it, you get:

Errors:

  * formatlist: no lists in arguments to formatlist in:

${formatlist("%s", var.list)}

If you change the default value for list to [1, 2, 3] and run it again, you get:

Outputs:

foo = 1, 2, 3

The cause is logic in this function that counts the number of items in the list(s) passed to formatlist. The problem is that 0 items could mean "you didn't pass in any lists" (which is a legitimate error) or "your lists are empty" (which should NOT be an error).

Fix in #9795

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings