Skip to main content

Command Palette

Search for a command to run...

Terraform - A Guide to Infrastructure as Code

Published
3 min read
Terraform - A Guide to Infrastructure as Code
S

💻 Sahil learns, codes, and automates, documenting his journey every step of the way. 🚀

My DevOps learning journey is going great, and I hope yours is too. Amazingly, people have created tools to automate manual processes. Now, let's look at Terraform, which truly meets the expectations of automating these tasks. I hope you're as excited as I am to explore Terraform. Let's get started.


What is Terraform?

  • Terraform is an Infrastructure as Code (IaC) tool that helps you automate and manage cloud infrastructure (like servers, databases, networks) using simple configuration files.

  • Instead of manually creating cloud resources (like launching an EC2 instance on AWS or setting up a database)

  • Terraform allows you to define everything in a code file, and it will automatically create and manage the infrastructure for you.

  • It works with multiple cloud providers like AWS, Azure, and Google Cloud, and helps maintain consistency, scalability, and automation.

  • Example: Setting Up an EC2 Instance with Terraform

    Before Terraform, if you wanted an EC2 instance in AWS, you'd manually click through the AWS console, configure settings, and launch it. With Terraform, you write a simple configuration file:

  •   provider "aws" {
        region = "us-east-1"
      }
    
      resource "aws_instance" "my_instance" {
        ami           = "ami-0c55b159cbfafe1f0"
        instance_type = "t2.micro"
      }
    
  • Then, running terraform apply will automatically create the instance in AWS without clicking anything!


Challenges before Terraform :

Imagine a small company CloudTech that hosts its applications on AWS.

Before Terraform (The Manual Nightmare)

CloudTech’s engineers used to manually set up everything using the AWS console. Every time they needed a new server, they would:

  1. Log into AWS

  2. Click through multiple settings

  3. Manually configure networking, security groups, and storage

  4. Launch the server

This worked fine until they needed 50 servers for a new project.

  • Each engineer had to manually repeat the process 50 times, leading to human errors (some servers had the wrong configuration).

  • If a server crashed, there was no quick way to recreate it with the exact same settings.

  • The team had no version control, meaning if someone accidentally changed something, they had no way to revert.

Their infrastructure became difficult to manage, error-prone, and time-consuming.


After Terraform (The Automation Revolution)

Then, CloudTech discovered Terraform.

  • Instead of clicking and configuring manually, they wrote everything in a Terraform file (code).

  • Now, whenever they needed 50 servers, they just ran a single command:

      terraform apply
    
  • Terraform automatically created and configured all servers identically.

The Benefits They Experienced:
Consistency – No more human errors! Every server was created exactly as defined.
Speed – What took hours manually was now done in minutes.
Version Control – They could track infrastructure changes using Git.
Easier Scaling – Need 100 more servers? Just update the code and apply changes.
Rollback – If something went wrong, they could easily destroy and recreate everything in seconds.