-3

What's the best way to compare two lists of string in terraform?

["arm64"] == ["arm64"] evaluates to false

My use case is constructing lambda extension ARNs based on aws_lambda_function's architectures, which has valid values ["arm64"] and ["x86_64"]. I fell back to comparing architectures[0], but that's not quite what I wanted to say: if the valid values expands in the future by allowing multiple values in the list, it may not fail fast with a clear error.

3
  • "["arm64"] == ["arm64"] evaluates to false" I just attempted to reproduce that with Terraform 1.7.5 and it evaluated to true. Please demonstrate otherwise with a MCVE, and include your version of Terraform. Commented Jun 21, 2024 at 15:46
  • 1
    also please explain if ordering matters in the comparison, i.e. do you expect the comparison between ["arm64", "x86_64"] and ["x86_64", "arm64"} to fail or not? From your question I think that you don't care about ordering, but I'm not sure Commented Jun 21, 2024 at 16:07
  • @LorenzoFelletti I care about ordering, but only because lists are ordered (rather than caring because of this use case) Commented Jun 24, 2024 at 17:47

3 Answers 3

1

I suspect that what you are actually doing is comparing a tuple type with a list type.

Here's some terraform console evaluations to demonstrate:

> type(["arm64"])
tuple([
    string,
])
> type(tolist(["arm64"]))
list(string)
> ["arm64"] == ["arm64"]
true
> tolist(["arm64"]) == ["arm64"]
false
> tolist(["arm64"]) == tolist(["arm64"])
true

Two values in Terraform can only be equal (as defined by ==) if they are of exactly the same type. Terraform's automatic type conversion rules mean that in most cases we don't need to worry too much about the difference between a tuple type and a list type, but the == operator doesn't give Terraform any hints about what types you might have intended and so it must compare the values exactly as given, without any automatic type conversions.

If you force both expressions to be of type list(string) using the tolist function, as I did in the last expression from my console session above, then they will compare equal if both lists contain the same strings in the same order.

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

1 Comment

Aha, I didn't know terraform even had a tuple type, nor that terraform console existed (though I wanted it to!). When I get a chance I'll see if I can verify this is what I was hitting, but I bet it was.
0

Since the lexographical order of a list matters when you compare two lists, I would advice to always use sort(). Furthermore, they need to be of the same type, comparing a tuple with a list will always render false.

["b", "a"] == ["a", "b"] => false, different lexographical order

sort(["b", "a"]) == sort(["a", "b"]) => true, same lexographical order

sort(["b", "a"]) == tolist(["a", "b"]) => true, sort returns a list as well

tolist(["a", "b"]) == tolist(["a", "b"]) => true, both are list

["a", "b"] == tolist(["a", "b"]) => false, comparing a tuple with a list

Comments

-1

@Martin Atkins provided a very insightful answer, answering indeed your question of how to compare two lists.

On another side, I have a feeling that what you may want to do in your code is something like that

contains(aws_lambda_function.this.architectures, "arm64") ? "something" : "something_else"

2 Comments

As I mention in the question, my intent was rather for it to fail if the list (or was it a tuple?) is not of length 1.
Sorry, I failed to understand that part. If you want to fail fast and with a clear error message, then you may want to look at Terraform's pre-/post-conditions. With that you can enforce a check on the length of that field, with an error message like "This module's logic is not ready to support > 1 lengths of the architectures field." or something like that

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.