0

I need to assign short_id to variable short_location_name,

I have map short_location_names in below terrfaorm code, if "var.location" is "westus", I need to assign short_location_names "wus"

my_code:

locals {
  short_location_names = {
      "westus": "wus",
      "eastus": "eus",
      "westus2": "wus2",
      "eastus2": "eus2"
  }
  short_location_name = {
    for location, short_id in local.short_location_names: location == var.location ? short_location_name => short_id
  }

I tried above code, I getting error missing false statement, How to fix?

0

2 Answers 2

2

Use the lookup function instead:

short_location_name = lookup(local.short_location_names, var.location, null)

or

short_location_name = { var.location: lookup(local.short_location_names, var.location, null) }

Or simply local.short_location_names[var.location] if the value will always be present in the map.

Sign up to request clarification or add additional context in comments.

Comments

2

Working Solution

locals {
 short_location_name  = "${lookup(local.region_code, var.location, "null")}"

 region_code = {

 "East US 2" = "use2"
 "eastus2" = "use2"
 "West US 2" = "usw2"
 "westus2" = "usw2"
 
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.