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

Call to function "element" failed: cannot read elements from set of string #22392

Closed
pradeep151287 opened this issue Aug 8, 2019 · 2 comments

Comments

@pradeep151287
Copy link

pradeep151287 commented Aug 8, 2019

Terraform Version

Terraform v0.12.5
+ provider.aws v2.23.0

Terraform Configuration Files

data "aws_subnet_ids" "subnet" {
  vpc_id = ""
}

output "sub2" {
  value = element(data.aws_subnet_ids.subnet1.ids,0)
}

Debug Output

Crash Output

Expected Behavior

element would have shown index 0 value

Actual Behavior

data.aws_subnet_ids.subnet1: Refreshing state...
data.aws_subnet_ids.subnet: Refreshing state...

Error: Error in function call

on output.tf line 10, in output "sub2":
10: value = element(data.aws_subnet_ids.subnet1.ids,0)
|----------------
| data.aws_subnet_ids.subnet1.ids is set of string with 6 elements

Call to function "element" failed: cannot read elements from set of string.

Steps to Reproduce

  1. terraform init
  2. terraform apply

Additional Context

References

@apparentlymart apparentlymart changed the title Terraform v0.12 element issue Call to function "element" failed: cannot read elements from set of string Aug 9, 2019
@apparentlymart
Copy link
Member

Hi @pradeep151287,

As the error message mentioned, data.aws_subnet_ids.subnet1.ids is a set value, which means it's an unordered collection of unique strings.

The aws_subnet_ids data source uses a set type for this result because the ids it's returning are not in any specific order, and so without additional information there is no way to decide which one is "first" and thus which one should be returned at index zero.

To select a single "first" element from this set requires the items to be placed in some order. If you don't actually care which of the six subnet ids you get and are just looking for any single subnet in a particular VPC then a straightforward answer is to first sort the subnet ids lexically and then take the first one from that sorted result:

output "sub2" {
  value = sort(data.aws_subnet_ids.subnet1.ids)[0]
}

(I also switched to using the index syntax with [0] rather than the element function, because using the element function to index a list is a deprecated form kept only for compatibility with Terraform 0.11 and earlier.)

If sorting and taking the first result is not precise enough for what you need here then you may need to impose some additional filters on the aws_subnet_ids data source to select only the single subnet you want to use, or at least to reduce the resultset further such that sort is a suitable solution.

Terraform does not perform this conversion automatically because it can't be sure that sorting is the desired behavior, and because writing sort(...) explicitly in your configuration will make it clearer to future readers of this configuration that it's taking the first result after lexical sorting. With that said, Terraform is working as intended here, and so I'm going to close this issue.

If you have any follow-up questions, feel free to ask in the community forum where there are more people ready to help. The GitHub issues here are generally monitored only by our few core maintainers.

@ghost
Copy link

ghost commented Sep 8, 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.

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

No branches or pull requests

3 participants