Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Terraform Conditionals

I have 4 envs, qa and dev use one ID, uat and prod use another. I’m trying to do an if else, basically, if env is dev or qa, use id1, else use id2. This is what I tried:

locals{
  endpoint_id = "${var.env == "dev" || "qa" ? "id1" : "id2"}"
}

And this is what I get:

Error: Invalid operand
│ 
│   on ssm-parameters.tf line 2, in locals:
│    2:   endpoint_id = "${var.env == "dev" || "qa" ? "id1" : "id2"}"
│ 
│ Unsuitable value for right operand: a bool is required.

Apparently I can’t do an "OR" here. How would I go about this? Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

How about:

locals{
  endpoint_id = length(regexall("dev|qa", var.env)) > 0 ? "id1" : "id2"
}

This will check if var.env matches either dev or qa, the output is a list, if there’s some match the list will have at least one element and zero otherwise.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading