# Terraform Expressions

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed terraform in-built functions.

https://cloudaffaire.com/terraform-functions/

In this blog post, we will discuss terraform expressions. Expressions are used to refer to or compute values within a configuration. The simplest expressions are just literal values, like “hello” or 5, but the Terraform language also allows more complex expressions such as references to data exported by resources, arithmetic, conditional evaluation, and a number of built-in functions.

#### Literal Expressions:

A literal expression is an expression that directly represents a particular constant value. Terraform has a literal expression syntax for each of the value types described below.

1 2 3 4 5 6 7 |
## Literal Expressions String: "this is a string" Numbers: 12, 2.5 Bool: True or False Null: null List: ["element_of_type_string", 3.5, false] Map: {<KEY> = <VALUE>} |

#### Conditional Expressions:

A conditional expression uses the value of a bool expression to select one of two values.

1 2 |
## Conditional Expressions CONDITION ? TRUEVALUE : FALSEVALUE |

#### For Expressions:

A for* *expression creates a complex type value by transforming another complex type value. Each element in the input value can correspond to either one or zero values in the result, and an arbitrary expression can be used to transform each input element into an output element. For expression is a new feature and available from Terraform 0.12 onwards.

1 2 |
## For Expressions (available in terraform 0.12 onwards) for <variable1> in <variable2>: <action> |

#### Splat Expressions:

A splat expression provides a more concise way to express a common operation that could otherwise be performed with a for expression. The special [*] symbol iterates over all of the elements of the list given to its left and accesses from each one the attribute name given on its right.

1 2 |
## Splat Expressions list[*] |

#### Arithmetic and Logical Operators:

An operator is a type of expression that transforms or combines one or more other expressions. Operators either combine two values in some way to produce a third result value, or transform a single given value to produce a single result.

1 2 3 4 5 6 7 8 9 |
## Arithmetic and Logical Operators Addition: + Subtraction: - Multiplication: * Division: / Modulus: % Equality: == and != Numerical comparison: >, <, >=, <= Boolean logic: &&, ||, unary ! |

Next, we are going to explain terraform expressions with examples. We will use terraform null resource for this demo.

#### Terraform expressions:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
##---------------------------- ## Terraform: Expressions ## ##---------------------------- ## Create a directory and get inside it mkdir terraform && cd terraform ## Create main.tf vi main.tf --------------- variable "ENV" { default = "PROD" } variable "A" { default = 20 } variable "B" { default = 10 } variable "C" { default = 3 } resource "null_resource" "myresource" { #creates three null resources count = "${var.C}" #for(i=0;i<3;i++){return i} } output "myoutput1" { value = "${var.ENV == "PROD" ? "PRODUCTION" : "NONPRODUCTION"}" #if(ENV=="PROD"){return "PRODUCTION"} else{return "NONPRODUCTION"} } output "myoutput2" { value = "A + B = ${var.A + var.B}\n A - B = ${var.A - var.B}\n A * B = ${var.A * var.B}\n A / B = ${var.A / var.B}\n A % C = ${var.A % var.C}" } output "myoutput3" { value = "${((var.A > var.B && var.A > var.C) ? "A is greatest" : (var.B > var.C ? "B is greatest" : "C is greatest"))}" #if(A>B && A>C){return A} elseif(B>C){return B} else{return C} output "myoutput4" { value = ["${null_resource.myresource.*.id}"] } --------------- :wq ## Format the code terraform fmt ## Apply terraform apply ## Cleanup cd .. && rm terraform |

Hope you have enjoyed this article. In the next blog post, we will discuss terraform workspaces.

For the complete list of terraform expressions, please refer below terraform documentation

https://www.terraform.io/docs/configuration/expressions.html

To get more details on terraform, please refer below terraform documentation.

https://www.terraform.io/docs/index.html