Skip to main content
element(list, index) returns the element at position index in list. The index wraps around: if index is greater than the list length, it cycles back to the beginning.

Signature

element(list list, index number) any

Example

Assign each build to one of several artifact storage buckets in a round-robin fashion using the build number:
variable "build_number" {
  description = "CI build number"
  type        = number
}

computed "bucket" {
  description = "Storage bucket for this build's artifacts"
  expression  = element(["artifacts-a", "artifacts-b", "artifacts-c"], var.build_number)
}

task "upload" {
  description = "Upload build artifacts to the assigned bucket"
  commands    = ["aws s3 cp dist/ s3://${computed.bucket}/build-${var.build_number}/ --recursive"]
}
Build 0 goes to artifacts-a, build 1 to artifacts-b, build 3 back to artifacts-a, and so on.

Notes

  • index wraps using modulo arithmetic, so it never produces an out-of-bounds error.
  • To get the last element without knowing the length, use element(list, length(list) - 1).