Skip to main content
lookup(inputMap, key, default) retrieves the value associated with key in inputMap. If the key does not exist, default is returned.

Signature

lookup(inputMap map, key string, default any) any

Example

Look up environment-specific database URLs from a map, falling back to a local development URL:
variable "env" {
  description = "Target deployment environment"
  type        = string
  default     = "development"
}

computed "db_url" {
  description = "Database URL for the target environment"
  expression  = lookup({
    staging    = "postgres://db.staging.internal/app"
    production = "postgres://db.prod.internal/app"
  }, var.env, "postgres://localhost/app_dev")
}

task "migrate" {
  description = "Run database migrations"
  commands    = ["./bin/migrate --db ${computed.db_url}"]
}
For development (or any unknown environment), the fallback postgres://localhost/app_dev is used.

Notes

  • lookup also works on objects, looking up attribute values by name.
  • If you need a hard failure when a key is missing (rather than a fallback), access the map directly with bracket notation: map[key].
  • default must be compatible with the map’s value type.