Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"is empty tuple" error #22480

Closed
designermonkey opened this issue Aug 15, 2019 · 3 comments
Closed

"is empty tuple" error #22480

designermonkey opened this issue Aug 15, 2019 · 3 comments

Comments

@designermonkey
Copy link

designermonkey commented Aug 15, 2019

Terraform Version

Terraform v0.12.5
+ provider.ansible v1.0.0
+ provider.digitalocean v1.4.0

Expected Behavior

I expect that having an empty array in a local defined in a .tf.json file is treated as a list.

Actual Behavior

Error: Invalid index

  on ***/***.tf line 18, in resource "digitalocean_record" "services":
  18:   value = local.services[0].public_address
    |----------------
    | local.services is empty tuple

The given key does not identify an element in this collection value.

Apparently my list is a tuple. The resource is defined similar to:

resource "digitalocean_record" "legacy" {
  count = length(local.services) >= 1 ? 1 : 0
  name = "something"

  domain = digitalocean_domain.production.name
  type = "A"
  value = local.services[0].public_address
}

With that being said, I also don't understand why it is even processing that to an error when the count is clearly 0 as defined above :/

Steps to Reproduce

Use the following .tf.json definition:

{
    "locals": {
        "services": []
    }
}

Additional Context

I can't provide more details as to what is being run where as we have very tight contracts here.

@teamterraform
Copy link
Collaborator

Hi @designermonkey,

This expression is failing static validation because it's referring to an element of a list that isn't there. The static validation happens before count is resolved (because count is itself an expression) and so all expressions in the configuration must be valid.

The good news is that there is a straightforward way around this: use count.index instead of 0:

  value = local.services[count.index].public_address

Since this doesn't refer to zero specifically, it can now pass even if there isn't a zeroth element in the list or tuple. Terraform's validator understands that count.index is controlled by the count argument and so Terraform will just check that it makes sense to be used in this context, without checking if the specific value of it is reasonable here.

As well as avoiding the need for any indexing at all (each.key is automatically the key from the for_each map, and each.value its corresponding value), this also has the advantage that Terraform will track these instances using their public address as the instance identifier, rather than their position in the local.services list.

Terraform is working as designed here -- this is static validation for correctness of expressions regardless of the current count value -- so we're going to close this. If you have any further questions about this, please feel free to start a new topic in the community forum. Thanks!

@designermonkey
Copy link
Author

Thanks for the feedback. Of course it will always only be count.index as I am making it a 1 item list.

@ghost
Copy link

ghost commented Sep 15, 2019

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.

@hashicorp hashicorp locked and limited conversation to collaborators Sep 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants