Terraform: Unable to use an individual element from an output list with element or []

Created on 4 Nov 2016  ยท  6Comments  ยท  Source: hashicorp/terraform

Terraform Version 0.7.7

Affected Resource(s)

output

Terraform Configuration Files

I outputted the value like so

data "aws_availability_zones" "available" {}
output "az_names" {
  value = ["${data.aws_availability_zones.available.*.names}"]
}

I expected to be able to use it as either

resource "aws_subnet" "web-a" {
    vpc_id                  = "${data.terraform_remote_state.vpc.vpc_id}"
    cidr_block              = "10.0.1.0/24"
    availability_zone       = "${element(data.terraform_remote_state.vpc.az_names, 0)}"
    map_public_ip_on_launch = false

    tags {
        "Name" = "web-a"
    }
}

I instead get an error

* element: element() may not be used with an empty list in:

${element(data.terraform_remote_state.vpc.az_names, 0)}

When I tried doing

resource "aws_subnet" "web-a" {
    vpc_id                  = "${data.terraform_remote_state.vpc.vpc_id}"
    cidr_block              = "10.0.1.0/24"
    availability_zone       = "${data.terraform_remote_state.vpc.az_names[0]}"
    map_public_ip_on_launch = false

    tags {
        "Name" = "web-a"
    }
}

I get this error

* At column 3, line 1: list "data.terraform_remote_state.vpc.az_names" does not have any elements so cannot determine type. in:

${data.terraform_remote_state.vpc.az_names[0]}

My remote state seems to be correct so I expect to be able to use as a normal list data

                "az_names": {
                    "sensitive": false,
                    "type": "list",
                    "value": [
                        [
                            "us-east-1a",
                            "us-east-1b",
                            "us-east-1d",
                            "us-east-1e"
                        ]
                    ]
                },
bug core

Most helpful comment

What about an example of how you workaround this? How can I add an element to the result of a data source defined by a provider object to avoid this?

All 6 comments

Yes this is an issue right now that is very, very core to Terraform. To fix it we need to fix the representation of state and diffs to encode types so that we can hint the types of empty lists. That is... a lot of work. We're going to do it, but its not a short term thing.

In the mean time, this is going to be REALLY sad but you can avoid this by putting a "dummy" element at the head (or tail) of the list and just incrementing the index by 1 for everything.

We have other issues covering this so I'm going to close this, but at least there is a workaround!

thanks for the workaround! I will do that for now.

Does anyone have a link to the root ticket for this?

What about an example of how you workaround this? How can I add an element to the result of a data source defined by a provider object to avoid this?

@jobmiller I think you can workaround it by using a local as an intermediary to collect the data output, and appending the dummy value there... then you would reference the local rather than the data output directly...

```
locals {
foo = "${concat(data...*., list("DUMMY"))}"
}

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