terraform console - command provides an interactive console for evaluating expressions. This is useful for testing interpolations before using them in configurations, and for interacting with any values currently saved in state.
echo "1 + 5" | terraform console
a + b returns the result of adding a and b together.
a - b returns the result of subtracting b from a.
a * b returns the result of multiplying a and b.
a / b returns the result of dividing a by b.
a % b returns the remainder of dividing a by b.
-a returns the result of multiplying a by -1.
a == b returns true if a and b both have the same type and the same value, or false otherwise.
a != b is the opposite of a == b.
a < b returns true if a is less than b, or false otherwise.
a <= b returns true if a is less than or equal to b, or false otherwise.
a > b returns true if a is greater than b, or false otherwise.
a >= b returns true if a is greater than or equal to b, or false otherwise.
a || b returns true if either a or b is true, or false if both are false.
a && b returns true if both a and b are true, or false if either one is false.
!a returns true if a is false, and false if a is true.
The syntax of a conditional expression is as follows:
condition ? true_val : false_val
...
instance_type = var.env == "prod" ? "t2.large" : "t2.micro"
...
If condition is true then the result is true_val. If condition is false then the result is false_val.
Link: https://www.terraform.io/docs/language/expressions/index.html
output "iam_users_custom" {
value = [
for user in aws_iam_user.users :
"Username: ${user.name} Unique id: ${user.unique_id}"
]
}
Link: https://www.terraform.io/docs/language/expressions/for.html
A for expression can also include an optional if clause to filter elements from the source collection, producing a value with fewer elements than the source value:
output "custom_if_length" {
value = [
for x in aws_iam_user.users :
x.name
if length(x.name) < 5
]
}