Today in the #30DaysOfAWSTerraform challenge, I finally understood how powerful Terraform variables are and how using them properly makes your code cleaner, more organized, less repetitive, and easier to maintain.
This was the day things started clicking for me.
What I Learned Today
Here’s what I personally understood during Day 5:
- Variables remove repetition and make code more organized and readable.
- I learned input variables, output variables, and local values.
- I understood how to view resource IDs using
terraform output. -
I learned how to override variables using:
terraform.tfvars- command-line overrides (
terraform plan -var=environment=test) - environment variables (
$env:TF_VAR_environment="test")
Overall, variables make code flexible and eliminate hardcoding everywhere.
Commands I Used Today
These are the exact commands I ran while experimenting:
terraform plan
terraform apply --auto-approve
terraform output
terraform plan -var=environment=test
mv terraform.tfvars terraform.tfvars.backup
mv terraform.tfvars.backup terraform.tfvars
$env:TF_VAR_environment="test"
These helped me understand how Terraform reads variable values based on priority.
My Terraform Code for Day 5
Below is the exact code I worked with today:
terraform {
backend "s3" {
bucket = "devopswithzacks-terraform-state"
key = "dev/terraform.tfstate"
region = "us-east-1"
encrypt = true
use_lockfile = true
}
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
}
variable "environment" {
default = "dev"
}
variable "channel_name" {
default = "dwz"
}
locals {
bucket_name = "${var.channel_name}-bucket-${var.environment}-${var.region}"
vpc_name = "${var.environment}-VPC"
}
variable "region" {
default = "us-east-1"
}
# Create s3 bucket
resource "aws_s3_bucket" "first_bucket" {
bucket = local.bucket_name
tags = {
Name = local.bucket_name
Environment = var.environment
}
}
# Create a vpc
resource "aws_vpc" "sample" {
cidr_block = "10.0.1.0/24"
region = var.region
tags = {
Environment = var.environment
Name = local.vpc_name
}
}
resource "aws_instance" "example" {
ami = "resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64"
instance_type = "t3.micro"
region = var.region
tags = {
Environment = var.environment
Name = "${var.environment}-EC2-Instance"
}
}
output "vpc_id" {
value = aws_vpc.sample.id
}
output "ec2_id" {
value = aws_instance.example.id
}
My Key Takeaways
Here’s what stood out clearly today:
Variables = Cleaner Code
No more repeating “dev” everywhere. I can just set it once.
Input Variables
Perfect for passing dynamic values into resources.
Output Variables
Useful when I need resource IDs after provisioning.
Local Values
Super helpful for computed values like dynamic bucket names.
Changing Variable Values Is Easy
I changed values using:
terraform.tfvarsterraform plan -var=environment=test- environment variables
This helped me see how Terraform picks values based on priority.
Day 5 Video
Day 5 was all about understanding flexibility inside Terraform.
Variables make your code:
more reusable.
more readable.
easier to manage.
safer for collaboration.
And now that Terraform no longer feels static, everything is starting to connect.
On to Day 6!
Top comments (0)