1

I have a map with some environment ID's as the key, then keywords as the values in a list.

variable "environments" {
  type    = map(list(string))
  default = {
    "env-one" = ["dev", "test", "stage", "staging"],
    "env-two" = ["prod", "production", "live"]
  }
}

I'm looking to use this to set the environment name based on the value of var.context["stage"].

So, if var.context["stage"] is equal to staging the value of environment will be dev

I was initially thinking to use lookup(), something like;

environment = "${lookup(var.environments, var.context["stage"])}"

However, I realise that's looking up the wrong way (finding the value as opposed to the key), and also it won't work as part of a map. So presumably I need to look through the map and run the lookup (albeit) backwards(?) on each iteration?

jonnybinthemix
  • 637
  • 1
  • 9
  • 29

1 Answers1

1

You would want to restructure the type into map(string). Then it would follow that the value would be:

{
  "dev"        = "env-one",
  "test"       = "env-one",
  "stage"      = "env-one",
  "staging"    = "env-one",
  "prod"       = "env-two",
  "production" = "env-two",
  "live"       = "env-two"
}

You could also modify this to be map(object) to contain more information. Based on the usage described in the question, this would actually make more sense to be a local. If you were to place this data into a locals block named environments, then the key-value pair could be accessed (according to the question) like local.environments[var.context["stage"]].

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67