Skip to main content

Command Palette

Search for a command to run...

πŸš€ Terraform Series – Day 7

Published
β€’3 min read
πŸš€ Terraform Series – Day 7

Variables & Outputs (Make Your Code Smart πŸ”₯)

In real-world DevOps, writing flexible and reusable code is very important.
Today, we will learn how to use Variables and Outputs in Terraform to make our infrastructure clean, dynamic, and production-ready.

Till now, we were writing Terraform code…
but there was one problem πŸ‘‡

πŸ‘‰ Everything was hardcoded

And in real DevOps, hardcoding = BIG mistake ❌

So today we fix that πŸ’‘

🧩 Step 1: The Real Problem

Imagine this:

instance_type = "t2.micro"

Looks simple… but πŸ‘‡

❌ Want to upgrade instance? Change everywhere

❌ Want reuse? Not possible

❌ Working in team? Becomes messy

πŸ‘‰ Basically: Not scalable

πŸ’‘ Step 2: The Smart Solution β†’ Variables

Instead of fixing values in code, we use variables

Think like this:
πŸ‘‰ β€œKeep values separate, keep code clean”

βœ” Why Variables?

  • Change once β†’ Apply everywhere

  • Clean & readable code

  • Reusable infrastructure

  • Industry-level practice

πŸ“Œ In short:
Variables = Flexibility + Clean Code + DevOps Standard

πŸ“„ Step 3: Create variables.tf

variable "ec2_instance_type" {
  default = "t2.micro"
  type    = string
}

variable "ec2_root_storage_size" {
  default = 10
  type    = number
}

variable "ec2_ami_id" {
  default = "ami-0cb91c7de36eed2cb"
  type    = string
}

🧠 Simple Understanding:

  • variable β†’ variable name

  • default β†’ default value

  • type β†’ data type

πŸ‘‰ Values are now separated from the main code βœ…

πŸ”— Step 4: Use Variables in ec2.tf

Now the real magic πŸ”₯

# Create Key Pair
resource "aws_key_pair" "my_key" {
  key_name   = "terra-key-aws"
  public_key = file("terra-key-aws.pub")
}


# Default VPC
resource "aws_default_vpc" "default" {}

# Security Group
resource "aws_security_group" "my_groups" {
  name        = "my-group"
  description = "Security group for EC2"
  vpc_id      = aws_default_vpc.default.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow SSH"
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow HTTP"
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow all outbound"
  }

  tags = {
    Name = "automate-sg"
  }
}

# EC2 Instance
resource "aws_instance" "my_instance" {
  ami                    = var.ec2_ami_id
  instance_type          = var.ec2_instance_type
  key_name               = aws_key_pair.my_key.key_name
  vpc_security_group_ids = [aws_security_group.my_groups.id]

  root_block_device {
    volume_size = var.ec2_root_storage_size
    volume_type = "gp3"
  }

  tags = {
    Name = "terra-ec2"
  }
}

⚑ Important Line:

var.<variable_name>

πŸ‘‰ Example:

var.ec2_instance_type

πŸ”„ Step 5: Real Power of Variables

Before:

instance_type = "t2.micro"

After:

default = "t3.micro"

βœ” Change in one place

βœ” Applied everywhere automatically

πŸ”₯ That’s the power

πŸ“€ Step 6: Now Let’s Talk About Outputs

Deployment is done… but now πŸ‘‡

πŸ‘‰ How do you get EC2 Public IP?

πŸ˜“ Problem

❌ You have to manually check AWS Console

πŸ’‘ Solution β†’ Outputs

Terraform will show it directly πŸ”₯

πŸ“„ Step 7: Create outputs.tf

output "ec2_public_ip" {
  value = aws_instance.my_instance.public_ip
}

output "ec2_public_dns" {
  value = aws_instance.my_instance.public_dns
}

🧠 What Happens Now?

When you run:

terraform apply

πŸ‘‰ At the end, Terraform shows:

βœ” Public IP

βœ” Public DNS

Directly in terminal 🎯

πŸ‘¨β€πŸ’» About the Author

β€œA complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.”

πŸ“¬ Let's Stay Connected

Terraform

Part 2 of 8

πŸš€ Terraform Series – Automate Your Infrastructure Starting a complete **Terraform series** where I’ll cover everything from **basic to advanced level** with real-world practicals. In this series, you will learn: β€’ What is Infrastructure as Code (IaC) & why it matters β€’ Terraform fundamentals (providers, resources, state) β€’ Writing and managing Terraform configurations β€’ Variables, outputs & modules β€’ Remote state & state management β€’ Provisioning infrastructure on AWS β€’ Automation & real-world use cases β€’ Advanced concepts like workspaces, modules, and best practices 🎯 Goal: Help you automate infrastructure and become job-ready in DevOps. Perfect for **beginners, students, and DevOps learners** who want hands-on experience. Stay tuned and let’s build infrastructure the smart way βš‘πŸ’» #Terraform #DevOps #Cloud #AWS #InfrastructureAsCode #Automation

Up next

πŸš€ Terraform Series – Day 6

Deploy EC2 with VPC, Security Group & SSH Access (Full Hands-On)