# 📘  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, <mark class="bg-yellow-200 dark:bg-yellow-500/30">hardcoding = BIG mistake</mark> ❌

So today we fix that 💡

### 🧩 Step 1: The Real Problem

Imagine this:

```shell
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`](http://variables.tf)

```shell
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
}
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/6051ea1a-7882-46e1-93af-4f09f089883f.png align="center")

🧠 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`](http://ec2.tf)

Now the real magic 🔥

```shell
# 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:

```shell
var.<variable_name>
```

👉 Example:

```shell
var.ec2_instance_type
```

### 🔄 Step 5: Real Power of Variables

Before:

```plaintext
instance_type = "t2.micro"
```

After:

```plaintext
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`](http://outputs.tf)

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

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

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/5ea76f21-8d21-40ba-8169-6d35579afa17.png align="center")

### 🧠 What Happens Now?

When you run:

```plaintext
terraform apply
```

👉 At the end, Terraform shows:

✔ Public IP

✔ Public DNS

Directly in terminal 🎯

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/804365ec-f053-449e-95fc-2dbdb35ca982.png align="center")

## **👨‍💻 About the Author**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751797710818/123a7231-3dca-4273-ad68-7bd026f69b95.png?auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp align="center")

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

### **📬 Let's Stay Connected**

*   📧 **Email**: [**gujjarapurv181@gmail.com**](mailto:gujjarapurv181@gmail.com)
    
*   🐙 **GitHub**: [**github.com/ApurvGujjar07**](http://github.com/ApurvGujjar07)
    
*   💼 **LinkedIn**: [**linkedin.com/in/apurv-gujjar**](http://linkedin.com/in/apurv-gujjar)
