Conditional Expression
A conditional expression is a way of choosing between two values depending on a boolean (true or false) condition. This allows you to concisely write and express code that makes a decision based on specific conditions. The syntax of it is simple:
<condition>?<true_val>:<false_val>
Here is the breakdown of syntax:
<condition>
: A Boolean condition, true or false, that determines the outcome. It can be any expression that evaluates to true or false, such as comparisons, logical operations, and function calls that return boolean expressions.
Here is an example of a conditional expression in Terraform:
variable "condition" { type = string default = "Linux" } resource "null_resource" "condition" { provisioner "local-exec" { command = "echo 'Condition example'" interpreter = var.condition == "Linux"? null:["/bin/bash","-c"] } }
Terraform will perform the following actions:
# null_resource.condition will be created
+ resource "null_resource" "string_joiner" {
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Enter a value: yes
null_resource.condition: Creating...
null_resource.condition: Provisioning with 'local-exec'...
null_resource.condition: Executing: ["/bin/sh" "-c" "echo 'Condition example'"]
null_resource.condition: foo-bar-baz
null_resource.condition: Creation complete after 0s [id=4425172414869377883]
Here, in this example, the interpreter attribute is conditionally set depending on the value of var.condition. If var.condition is "Linux", then the interpreter is set to null, which means the default interpreter should be used. Otherwise, the interpreter is set to ["/bin/bash", "-c"], meaning that the execution of the command will be done using the /bin/bash interpreter with the option -c.
Within a Terraform condition expression, the two result values (
<true_val>
and <false_val>
) can be of any type, but they must both be of the same type. This enables Terraform to know what type the whole conditional expression is, without knowing the value of the condition.
If the two result expressions are not the same type, Terraform will try to find a common type that both can be automatically converted to. When it can find a common convertible type, Terraform automatically converts the result values to type. For example, if the two result values are string and integer, respectively, Terraform will try to convert these to a common type, such as string, so that the conditional expression can be correctly evaluated.
variable "condition" { type = string default = "Linux" } output "automatic-convertion" { value = var.os == "Windows" ? "Welcome" : 42 }
Apply complete! Resources: 0 to added, 0 to changed, 0 to destroyed.
Outputs:
automatic-convertion = "42"
In this example, the
<true_val>
is a string "Welcome", and the <false_val>
is an integer 42. These are not the same type, but Terraform can automatically convert the integer to a string, so the overall type of the conditional expression is a string.
However, If the result values were not convertible to a common type, Terraform would throw an error, and you would need to ensure that the
<true_val>
and <false_val>
are of the same type. For example
variable "condition" { type = string default = "Linux" } output "automatic-convertion" { value = var.os == "Windows" ? true : 42 }
Error: Inconsistent conditional result types
The true and false result expressions must have consistent types. The 'true' value is bool, but the 'false' value is number.
Related Pages
- Variables - Input value and Local value
Feedback
Was this page helpful?