Day 14: Mastering Expressions and Functions in Terraform


After covering variables, outputs, and locals yesterday, today I went deeper into expressions and functions in Terraform. These are like the “logic” and “tools” inside Terraform that make your code smarter and cleaner.
Instead of hardcoding values, you can calculate, combine, and manipulate data with expressions and functions. Let’s break it down 👇
🔹 What Are Expressions?
Expressions in Terraform are just like formulas in math or Excel. They allow you to combine values and get results dynamically.
For example:
variable "instance_count" {
default = 2
}
output "double_count" {
value = var.instance_count * 2
}
Here, var.instance_count * 2
is an expression.
If instance_count = 2
, the output will be 4.
Expressions can be:
Arithmetic (
+
,-
,*
,/
)Comparisons (
==
,!=
,>
,<
)Boolean logic (
&&
,||
,!
)String interpolation (
"Hello ${var.name}"
)
🔹 Terraform Functions
Functions are built-in helpers in Terraform. They take input and return a value.
There are more than 100+ functions, but let’s focus on the most useful ones with simple examples.
1. String Functions
upper()
→ makes text uppercase.
output "upper_name" {
value = upper("terraform")
} # Output: TERRAFORM
lower()
→ makes text lowercase.replace()
→ replace parts of a string.
2. Numeric Functions
min()
→ returns the smallest number.
output "min_value" {
value = min(10, 20, 5)
} # Output: 5
max()
→ returns the largest.
3. Collection Functions (Lists/Maps)
length()
→ count items in a list.
output "list_length" {
value = length(["a", "b", "c"])
} # Output: 3
element()
→ pick one item.
output "first_item" {
value = element(["dev", "staging", "prod"], 0)
} # Output: dev
4. File Functions
file()
→ read a file.
output "policy" {
value = file("policy.json")
}
Useful for attaching IAM policies stored in JSON.
5. CIDR Functions (Networking)
cidrsubnet()
→ create subnets from a CIDR block.
output "subnet" {
value = cidrsubnet("10.0.0.0/16", 4, 1)
}
This helps when designing VPCs.
6. Terraform-Specific Functions
terraform.workspace
→ tells which workspace you are in.timestamp()
→ returns current time.
🔹 Why Are Expressions and Functions Important?
Reduce hardcoding → write flexible code.
Enable automation → let Terraform calculate values.
Improve reusability → same module can be used in multiple environments.
Example:
variable "env" {
default = "dev"
}
output "instance_name" {
value = "${var.env}-webserver"
}
If env=dev
→ output = dev-webserver
.
If env=prod
→ output = prod-webserver
.
💡 Key Takeaways
Expressions = formulas or logic in Terraform.
Functions = built-in helpers (string, numeric, collections, files, CIDR, etc).
Together, they make your code dynamic, reusable, and smart.
🔗 Follow My Journey
📖 Blogs: Hashnode
💻 Code: GitHub
🐦 Updates: X(Twitter)
Subscribe to my newsletter
Read articles from Abdul Raheem directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abdul Raheem
Abdul Raheem
Cloud DevOps | AWS | Terraform | CI/CD | Obsessed with clean infrastructure. Cloud DevOps Engineer 🚀 | Automating Infrastructure & Securing Pipelines | Bridging Gaps Between Code and Cloud ☁️ I’m on a mission to master DevOps from the ground up—building scalable systems, automating workflows, and integrating security into every phase of the SDLC. Currently working with AWS, Terraform, Docker, CI/CD, and learning the art of cloud-native development.