You're looking at documentation for Terraform v1.1 and earlier. Click here to view the latest content.
A new platform for documentation and tutorials is launching soon.
We are migrating Terraform documentation into HashiCorp Developer, our new developer experience.
element retrieves a single element from a list.
The index is zero-based. This function produces an error if used with an empty list. The index must be a non-negative integer.
Use the built-in index syntax
list[index] in most cases. Use this function
only for the special additional "wrap-around" behavior described below.
> element(["a", "b", "c"], 1) b
If the given index is greater than the length of the list then the index is "wrapped around" by taking the index modulo the length of the list:
> element(["a", "b", "c"], 3) a
To get the last element from the list use
length to find
the size of the list (minus 1 as the list is zero-based) and then pick the
> element(["a", "b", "c"], length(["a", "b", "c"])-1) c