DEV Community

Cover image for ๐Ÿš€ Terraform Day 5: Mastering Variables โ€” The Key to Clean, Reusable Infrastructure Code
Jeeva
Jeeva

Posted on

๐Ÿš€ Terraform Day 5: Mastering Variables โ€” The Key to Clean, Reusable Infrastructure Code

Variables are one of the most important concepts in Terraform.
Day 5 of the 30 Days AWS Terraform challenge focuses entirely on understanding what variables are, why we need them, and how they make Terraform configurations reusable, maintainable, and safe.

This lesson finally answers why hardcoding must be avoided and shows how variables help control environments like dev, stage, and production without rewriting code.

๐ŸŒฑ Why Do We Need Variables?

Hardcoding values such as:
AWS region
VPC CIDR
Tags
Bucket names
AMI IDs
โ€ฆcauses repetition and errors.

A single typo can break your infrastructure.
Variables solve this by:
Centralizing values in one place
Reducing duplication
Allowing environment switching
Preventing human error
Improving long-term maintainability

๐Ÿงฉ Types of Variables in Terraform

Terraform supports 3 major types of variables:

#๐Ÿ”ธ 1. Input Variables

Provide user-defined values to Terraform.
Example:
variable "env" {
type = string
default = "dev"
}

Use it:
tags = {
Environment = var.env
}

Input variables help eliminate repeated hardcoded values.

๐Ÿ”ธ 2. Local Values (locals)

Used for computed or reusable internal expressions.
Example:
locals {
bucket_name = "${var.env}-myapp-bucket"
}
Then use it:
bucket = local.bucket_name
Locals help maintain consistent naming conventions and avoid long expressions.

๐Ÿ”ธ 3. Output Variables

Display useful values after applying infrastructure.
Example:
output "vpc_id" {
value = aws_vpc.main.id
}

Useful for:
Debugging
Passing values to other modules
Automation

๐Ÿง  Variable Types

Terraform supports:
string
number
bool
list()
set()
map()
object()
tuple()
Special: any, null

Type constraints improve safety and catch errors early.

๐Ÿงฎ String Interpolation

Old syntax:
${var.env}-vpc
New recommended syntax:
"${var.env}-vpc"
Interpolation allows dynamic naming, concatenation, and more.

๐Ÿ”€ Variable Precedence โ€” Which Value Wins?

Terraform chooses variable values in the following order (highest โ†’ lowest):

1๏ธโƒฃ Command line
terraform apply -var="env=stage"

2๏ธโƒฃ .tfvars file
Example: dev.tfvars
env = "dev"

3๏ธโƒฃ Environment variables
export TF_VAR_env="prod"

4๏ธโƒฃ Default values (inside variable block)

Understanding precedence allows flexible environment switching without editing code.

๐Ÿš€ Hands-On: Using Variables While Deploying Resources

Day 5 showed real provisioning with variables:
Creating resources (e.g., VPC, S3 buckets)
Fixing AMI and permission errors
Updating variable-driven names

Running:
terraform plan
terraform apply
terraform output

Outputs displayed meaningful resource attributes, such as IDs.

๐Ÿงฐ Helpful Terraform State & Output Commands

View outputs:
terraform output
Useful for referencing values externally (pipelines, scripts, modules).

๐Ÿ Conclusion

Day 5 introduced the core of writing clean Terraform code: Variables.
By mastering input variables, locals, outputs, and precedence rules, Terraform configurations become:

โœ” Reusable
โœ” Scalable
โœ” Easier to maintain
โœ” Environment-ready
โœ” Less error-prone
_
Variables turn Terraform from โ€œworking codeโ€ into professional Infrastructure-as-Code._

Top comments (0)