locals can do calculations, not just store values.
locals {
mul = 2 * 8 # 16 - multiplication
add = 2 + 2 # 4 - addition
eq = 2 != 3 # true - not equal (comparison)
}
Arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% modulo (remainder)
Comparison operators:
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal
<= less than or equal
Logic operators:
&& and
|| or
! not
# Simple list of numbers
variable "num_list" {
type = list(number)
default = [1, 2, 3, 4, 5]
}
# List of objects - each item has multiple fields
variable "person_list" {
type = list(object({
fname = string
lname = string
}))
default = [
{ fname = "Raju", lname = "Rastogi" },
{ fname = "Sham", lname = "Paul" }
]
}
# Map - key value pairs
variable "map_list" {
type = map(number)
default = {
"one" = 1
"two" = 2
"three" = 3
}
}
for = Loop through a list or map and transform/filter values.
# List output → use [ ]
[for item in collection : output]
# Map output → use { }
{for key, value in collection : key => output}
# With filter (if condition)
[for item in collection : output if condition]