Day 5 - Terraform Variables
Content
Today felt different. Terraform started feeling less like writing config and more like writing logic.
Until now I was hardcoding values. Today I learned how to pass values, build them, and get them back using variables.
There are 3 types
Input variables
Local variables
Output variables
At first it was confusing, but once I ran it myself, it made sense.
What I built
I created a simple S3 bucket using variables instead of hardcoding the name.
To understand variables better, I created a small setup with multiple Terraform files.
I used separate files for input variables, local values, main resource creation, and outputs. I also added tfvars files to test different environments like development and production.
What I tested
I tried a few things to understand how variables behave.
First I ran normally using terraform.tfvars
Terraform picked those values automatically
Then I removed tfvars and ran again
Terraform used default values from variables.tf
Then I passed values in command line
That overrode everything
Then I used a different tfvars file (Dev and Prod as below)
That simulated another environment
Outputs
After apply, I checked outputs
Terraform showed me
bucket name
environment
tags
That helped me confirm everything is wired correctly
Terraform automatically picked values from terraform.tfvars without me passing anything. This helped me understand how Terraform loads default variable files by itself.
What clicked for me
Variables are just inputs
Locals are like building something from those inputs
Outputs are just showing what got created
Simple, but powerful
One thing I realized
Using just bucket_name is not enough
Combining environment + name + random value makes it reusable and unique
That’s where locals helped
Cleanup
Finally I destroyed everything to avoid charges
Closing thought
Today was the first day Terraform felt like actual programming
Not just writing configs, but passing values and building logic
Comments
Post a Comment