Variable keys in terraform maps

There’s (now) a lookup function supported in the terraform interpolation syntax, that allows you to lookup dynamic keys in a map.

Using this, I can now do stuff like:

output "image_bucket_name" {
  value = "${lookup(var.image_bucket_names, var.environment, "No way this should happen")}"
}

where:

variable "image_bucket_names" {
  type = "map"

  default = {
    development = "bucket-dev"
    staging = "bucket-for-staging"
    preprod = "bucket-name-for-preprod"
    production = "bucket-for-production"
  }

}

and environment is a simple string variable.

Leave a Comment