Skip to main content
keys(inputMap) returns a list of all keys in inputMap, sorted lexicographically.

Signature

keys(inputMap map) list(string)

Example

Iterate over a map of environment-specific replica counts and print a summary before deploying:
variable "replicas" {
  description = "Replica counts per environment"
  type        = map(number)
  default = {
    staging    = 1
    production = 3
  }
}

variable "env" {
  type = string
}

computed "environments" {
  description = "All configured environments"
  expression  = keys(var.replicas)
}

task "show-config" {
  description = "Print the replica configuration for all environments"
  commands = [
    "echo 'Configured environments: ${join(", ", computed.environments)}'",
    "echo 'Deploying to ${var.env} with ${var.replicas[var.env]} replicas'",
  ]
}

Notes

  • Keys are returned in sorted order, which makes the output deterministic across runs.
  • keys also works on objects. When applied to an object, it returns the attribute names as a tuple sorted lexicographically.
  • To get the values, use values. To get both, iterate over the map directly in a shell command.