Skip to main content

Command Palette

Search for a command to run...

Day 1: Introduction to Infrastructure as Code: Exploring Terraform

Updated
β€’6 min read
Day 1: Introduction to Infrastructure as Code: Exploring Terraform

Welcome to Day 1 of your Terraform learning journey! Today, we’ll start from the very beginningβ€”understanding what Infrastructure as Code means and why Terraform has become the go-to tool for managing cloud infrastructure.

πŸ€” The Traditional Way: Manual Infrastructure

Imagine you need to set up a web server on AWS. The traditional approach would be

  1. Log into AWS Console

  2. Click through menus to create a VPC

  3. Manually configure security groups

  4. Launch an EC2 instance

  5. Set up storage, networking, etc.

  6. Document everything in a Word doc or spreadsheet

Problems with this approach:

  • ❌ Time-consuming and error-prone

  • ❌ Hard to replicate exactly

  • ❌ No version control

  • ❌ Difficult to collaborate with team members

  • ❌ Documentation gets outdated quickly

πŸ’‘ Enter Infrastructure as Code (IaC)

Infrastructure as Code is the practice of managing and provisioning infrastructure through code instead of manual processes.

Think of it this way: instead of clicking buttons, you write code that describes what you want, and a tool creates it for you.

Traditional Approach:          IaC Approach:

   πŸ‘€ Human                      πŸ‘€ Human
    |                             |
    | (clicks)                    | (writes code)
    ↓                             ↓
  πŸ–₯️ AWS Console               πŸ“„ Configuration File
    |                             |
    | (creates)                   | (reads)
    ↓                             ↓
  ☁️ Infrastructure             πŸ€– IaC Tool (Terraform)
                                  |
                                  | (creates)
                                  ↓
                                ☁️ Infrastructure

πŸ”· What is Terraform?

Terraform is an open-source Infrastructure as Code tool created by HashiCorp. It allows you to define your infrastructure using a simple, human-readable language called HCL (HashiCorp Configuration Language).

Key Features:

  1. Multi-Cloud Support: Works with AWS, Azure, GCP, and 1000+ providers

  2. Declarative Syntax: You describe WHAT you want, not HOW to create it

  3. State Management: Tracks your infrastructure’s current state

  4. Plan Before Apply: Preview changes before making them

  5. Resource Graph: Understands dependencies between resources

πŸ—οΈ How Terraform Works

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  1. WRITE: Define infrastructure in .tf files   β”‚
β”‚                                                 β”‚
β”‚     resource "aws_instance" "web" {             β”‚
β”‚       ami           = "ami-12345"               β”‚
β”‚       instance_type = "t2.micro"                β”‚
β”‚     }                                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  2. PLAN: Terraform analyzes and shows changes  β”‚
β”‚                                                 β”‚
β”‚     $ terraform plan                            β”‚
β”‚     + aws_instance.web will be created          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  3. APPLY: Terraform creates the resources      β”‚
β”‚                                                 β”‚
β”‚     $ terraform apply                           β”‚
β”‚     βœ“ aws_instance.web created                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  4. STATE: Terraform saves the current state    β”‚
β”‚                                                 β”‚
β”‚     terraform.tfstate (tracks resources)        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🎯 The Terraform Workflow

Terraform follows a simple three-step workflow:

1. Write

Define your infrastructure in .tf files using HCL:

resource "aws_instance" "my_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "MyFirstServer"
  }
}

2. Plan

Terraform compares your code with the current state and shows what will change:

$ terraform plan
Terraform will perform the following actions:
  # aws_instance.my_server will be created 
          + resource "aws_instance" "my_server" {
      + ami           = "ami-0c55b159cbfafe1f0"      
      + instance_type = "t2.micro" 
       ...    
    }
      Plan: 1 to add, 0 to change, 0 to destroy.

3. Apply

Execute the plan to create/update infrastructure:

$ terraform apply
...aws_instance.my_server: Creating...
aws_instance.my_server: Creation complete after 45s
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

πŸ†š Terraform vs Other Tools

FeatureTerraformCloudFormationAnsible
Multi-cloudβœ… Yes❌ AWS onlyβœ… Yes
Agent required❌ No❌ No❌ No
Declarativeβœ… Yesβœ… Yes⚠️ Mostly
State managementβœ… Yesβœ… Yes❌ No
Best forInfrastructureAWS infraConfiguration

✨ Why Learn Terraform?

  1. High Demand: Top skill in DevOps job listings

  2. Multi-Cloud: One tool for all cloud providers

  3. Version Control: Track infrastructure changes like code

  4. Automation: Integrate with CI/CD pipelines

  5. Collaboration: Teams can work together on infrastructure

  6. Reusability: Create modules used across projects

πŸ“ Real-World Example

Instead of manually creating a web server, you write this:

# main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "web_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name        = "WebServer"
    Environment = "Development"
  }
}

Then run:

terraform init    
# Initializeterraform plan    
# Preview changesterraform apply   
# Create resources

And Terraform creates everything for you! πŸŽ‰

πŸ”‘ Key Concepts to Remember

  • Provider: Plugin that allows Terraform to interact with cloud platforms (AWS, Azure, etc.)

  • Resource: A component of your infrastructure (EC2 instance, S3 bucket, etc.)

  • State: Terraform’s knowledge of your infrastructure’s current status

  • HCL: HashiCorp Configuration Language - Terraform’s syntax

πŸ§ͺ Hands-On Lab: Explore Terraform Basics

Prerequisites

  • An AWS account (free tier)

  • A computer with internet access

Lab Steps

Step 1: Create Your AWS Account

  1. Go to https://aws.amazon.com

  2. Click β€œCreate an AWS Account”

  3. Follow the signup process (requires credit card but we’ll use free tier)

  4. Verify your email

Step 2: Understand the AWS Free Tier

  • AWS provides 750 hours/month of t2.micro instances (free for 12 months)

  • We’ll stay within free tier limits throughout this series

  • Always remember to destroy resources after practice!

Step 3: Familiarize with AWS Console

  1. Log into AWS Console: https://console.aws.amazon.com

  2. Navigate to EC2 Dashboard

  3. Browse around - notice how many clicks it takes to launch an instance

  4. Don’t create anything yet - just explore

Step 4: Understand the Manual Process Count the steps to manually create an EC2 instance:

  1. Choose AMI (Amazon Machine Image)

  2. Choose Instance Type

  3. Configure Instance Details

  4. Add Storage

  5. Add Tags

  6. Configure Security Group

  7. Review and Launch

  8. Create/Select Key Pair

That’s 8+ steps with dozens of options! Tomorrow, we’ll do this with just a few lines of code.

Step 5: Reflection Questions Think about these (write down your answers):

  • How would you recreate this exact setup in another region?

  • How would you document all the settings you chose?

  • How would you track who made what changes?

  • How would you share this setup with your team?

These questions highlight why IaC is essential!

πŸ“š Summary

Today you learned:

  • βœ… What Infrastructure as Code is and why it matters

  • βœ… What Terraform is and how it works

  • βœ… The three-step Terraform workflow (Write, Plan, Apply)

  • βœ… Key advantages of using Terraform over manual processes

  • βœ… Basic Terraform concepts (providers, resources, state)

πŸš€ Tomorrow’s Preview

Day 2: Installing Terraform & Your First Configuration

Tomorrow, we’ll:

  • Install Terraform on your computer

  • Set up AWS CLI and credentials

  • Write your very first Terraform configuration

  • Create your first AWS resource with code!

πŸ’­ Quiz Yourself

  1. What does IaC stand for?

  2. Name three benefits of using Terraform

  3. What are the three main steps in the Terraform workflow?

  4. What language does Terraform use?

  • Click for answers

    1. Infrastructure as Code

    2. Multi-cloud support, version control, automation (any three benefits mentioned above)

    3. Write, Plan, Apply

    4. HCL (HashiCorp Configuration Language)


Ready for Day 2? Tomorrow we’ll install Terraform and create your first infrastructure!

Happy Learning! πŸŽ‰

Thanks For Reading, Follow Me For More

Subscribe youtube channel for the recap videos

Have a great day!..

← Back to Intro


Remember: The best way to learn is by doing. Make sure you complete the hands-on lab before moving to the next day!

More from this blog

S

StackOps - Diary

33 posts

Welcome to the StackOps - Diary. We’re dedicated to empowering the tech community. We delve into cloud-native and microservices technologies, sharing knowledge to build modern, scalable solutions.