Skip to main content
length(collection) returns the number of elements in a list, tuple, set, or map.

Signature

length(collection list | set | map | tuple) number

Example

Guard a deployment task so it only runs when at least one target service is specified:
variable "services" {
  description = "Services to deploy"
  type        = list(string)
  default     = []
}

task "deploy" {
  description = "Deploy the selected services"
  condition   = length(var.services) > 0
  commands = [
    "for svc in ${join(" ", var.services)}; do kubectl rollout restart deployment/$svc; done",
  ]
}
When --var 'services=[]' is passed or the default is used, the task is skipped. When services are provided, the deployment proceeds.

Notes

  • length works on lists, sets, maps, and tuples. It does not work on strings, numbers, or booleans.
  • For maps, length returns the number of keys.