DEV Community

Ville
Ville

Posted on

30 Days of Terraform – Day 1: Introduction to Infrastructure as Code

Welcome to Day 1 of my 30‑day Terraform blog series! 🎉 This series is designed to complement and expand on the excellent 30 Days of Terraform YouTube series by Tech with Piyush Sachdeva. You can follow along with the full video playlist here: 30 Days of Terraform Playlist.

Each day, I’ll break down key concepts, provide hands‑on notes, and share practical tips to help you master Infrastructure as Code using Terraform. By the end of this journey, you’ll be able to confidently provision, manage, and automate cloud infrastructure across AWS and beyond.

Getting Started with Terraform: Infrastructure as Code Made Simple

In today’s cloud‑driven world, managing infrastructure manually through consoles like AWS, Azure, or GCP quickly becomes overwhelming. Imagine provisioning a simple three‑tier application with web, app, and database layers. You’d need multiple servers, load balancers, auto‑scaling groups, VPCs, databases, and more. Doing this step‑by‑step in a GUI might take two hours for one app. Now scale that to six environments (dev, test, staging, prod, disaster recovery, performance) and hundreds of applications in an enterprise. Suddenly, manual provisioning is not just inefficient—it’s nearly impossible.

This is where Infrastructure as Code (IaC) comes in. IaC means writing code to define and provision infrastructure resources. Instead of clicking through consoles, you describe your infrastructure in files that can be versioned, reused, and automated. Among the many IaC tools available, Terraform stands out as the most popular and universal option, supporting multiple cloud providers.

📺 Day 1 Video: Introduction to Terraform

How Terraform Works

At its core, Terraform uses configuration files with the .tf extension, written in HCL (HashiCorp Configuration Language). HCL is simple, human‑readable, and machine‑friendly—similar to JSON but designed specifically for infrastructure. DevOps engineers write these .tf files to describe resources like EC2 instances, S3 buckets, or VPCs, and then store them in GitHub or another version control system.

From there, Terraform provides a set of commands to manage infrastructure:

  • terraform init: Initializes Terraform for the project.
  • terraform validate: Checks for syntax or validation errors.
  • terraform plan: Performs a dry run, showing what changes will be made (resources created, modified, or destroyed).
  • terraform apply: Actually provisions the infrastructure.
  • terraform destroy: Tears down resources defined in the .tf files.

Behind the scenes, Terraform interacts with cloud provider APIs (AWS, Azure, GCP, etc.) to perform these actions. It’s the same API layer that consoles and CLIs use, but automated and consistent.


Why Terraform Matters

Manual provisioning introduces several challenges:

  • Time‑consuming: Hours per environment, multiplied across apps.
  • Human error: Misconfigured databases, missing encryption, or inconsistent setups.
  • Inconsistency: Different engineers provisioning environments differently, leading to “works on my machine” issues.
  • High cost: Large teams dedicated solely to repetitive infrastructure tasks.

Terraform solves these by enabling:

  • Consistency: Write once, deploy many. The same script provisions identical environments.
  • Automation: Infrastructure creation, updates, and destruction are streamlined.
  • Version control: Every change is tracked in GitHub, eliminating blame games.
  • Efficiency: Teams focus on building apps and pipelines instead of manual infra work.

Installing Terraform

Getting started with Terraform is straightforward. You can install it on Windows, macOS, or Linux using the official guide: Terraform Installation Tutorial. Once installed, you’ll have access to the terraform CLI.


Enhancing Your Workflow

To make Terraform even smoother, consider these tips:

  • Autocomplete: Run terraform -install-autocomplete to enable command autocompletion in your shell. This saves time and reduces typos.
  • Alias: Set an alias like alias tf=terraform so you can run commands faster (e.g., tf apply instead of terraform apply).
  • CI/CD Integration: Connect Terraform with GitHub Actions or other CI/CD pipelines. This way, infrastructure changes are automatically applied whenever .tf files are updated in your repository.

The Bigger Picture

Terraform embodies the DRY principle (Don’t Repeat Yourself). You write your infrastructure code once, and reuse it across environments. Modules allow you to encapsulate reusable patterns, making it easy to scale from a single app to thousands. Combined with version control, Terraform ensures transparency, accountability, and collaboration across teams.

Ultimately, Terraform empowers DevOps engineers, developers, and infrastructure teams to focus on innovation rather than repetitive tasks. By automating infrastructure provisioning, organizations save time, reduce costs, and eliminate inconsistencies. Whether you’re deploying a simple web app or managing enterprise‑scale environments, Terraform is the tool that makes Infrastructure as Code practical, powerful, and accessible.

Below is my cue-card style notes as an alternative to blog style notes

Infrastructure as Code (IaC) – Notes

Concept

  • Defenition: Translating infrastructure setup into code.
  • **Purpose: Write code/scripts to provision infrastructure resources.
  • Examples: Servers, virtual machines, VPCs, S3 buckets, firewall rules, EBS volumes, IAM policies, etc.
  • Approach: Any programming language, shell scripts, or command formats can be used.

Benefits

  • Automation: Infrastructure can be provisioned consistently and repeatedly.
  • **Scalability: Easily manage multiple cloud resources.
  • Portability: Code can be reused across environments.

Tools

  • Universal tools (work with most cloud providers):

    • Terraform (most popular, chosen for this series)
    • Pulumi
  • Azure-specific tools:

    • ARM (Azure Resource Manager templates)
    • Bicep
  • AWS-specific tools:

    • CloudFormation
    • CDK (Cloud Development Kit)
    • SAM (Serverless Application Model)
  • GCP-specific tools:

    • Deployment Manager
    • Config Controller
    • Config Connector

Key Takeaway

  • Focus on Terraform: Most popular, widely supported across cloud providers, beginner-friendly starting point.
  • No need to learn all tools: Start with one universal tool before exploring vendor-specific options.

Why Infrastructure as Code (IaC) is Needed

Manual Provisioning via Cloud Consoles

  • Cloud providers offer GUIs (AWS Console, Azure Portal, GCP Console).
  • Provisioning resources manually is slow and repetitive.
  • Example: Three-tier architecture (web, app, database tiers) requires:
    • Multiple servers (e.g., EC2 instances)
    • Auto-scaling groups
    • Load balancers (external & internal)
    • CDN, Route 53, health checks
    • Databases (master & replica)
    • VPC and more

Time & Complexity

  • Manual setup for one simple app: ~2 hours.
  • Enterprise apps require multiple environments (dev, SIT, performance, DR, prod, staging).
  • Six environments per app → ~12 hours per app.
  • Enterprises run hundreds/thousands of apps → impossible to manage manually.

Challenges of Manual Approach

  • Dependency delays: Dev, QA, and testing teams wait for infra team.
  • Resource inefficiency: Large teams only provisioning infra daily.
  • High cost: More people needed, repetitive tasks.
  • Human errors: Misconfigured databases, missing encryption, insecure setups.
  • Inconsistency: Different people provisioning → environments differ (dev vs prod mismatch).
  • “Works on my machine” problem: Code runs in dev but fails in prod due to inconsistent infra.
  • Security risks: Manual steps may miss critical configurations.

Solution: Infrastructure as Code (Terraform Example)

  • Write once, deploy many: One configuration file reused across environments.
  • Consistency: Same script ensures identical infra across dev, test, prod.
  • Efficiency: Saves time, cost, and reduces need for large infra teams.
  • Automation: Provisioning and destruction of environments (e.g., non-prod) is easy.
  • Maintenance: Updates, patches, and changes applied via code.
  • Version control: Track changes in GitHub/VC systems → transparency, accountability, no blame game.
  • DRY principle: Don’t Repeat Yourself → reusable modules.
  • Focus shift: Infra team, DevOps, and developers can focus on apps, CI/CD, innovation instead of manual infra tasks.

✅ Key takeaway: IaC (with tools like Terraform) transforms infrastructure management from a slow, error-prone manual process into a fast, consistent, automated, and scalable workflow.

Terraform – High-Level Workflow

TF Files

  • Extension: .tf
  • Language: HCL (HashiCorp Configuration Language)
    • Human-readable and machine-readable
    • Similar to JSON, simple and easy to use
  • Purpose: Define infrastructure resources in code
  • Storage: Checked into GitHub or other version control systems

Core Commands

  • terraform init: Initialize Terraform (first-time setup)
  • terraform validate: Check syntax and validation errors
  • terraform plan: Dry run; shows what changes will be made (resources created, modified, deleted)
  • terraform apply: Actually provisions infrastructure resources
  • terraform destroy: Deletes infrastructure defined in .tf files

How It Works

  • Terraform interacts with cloud provider APIs (e.g., AWS APIs)
  • Similar to how AWS Console or AWS CLI communicates with APIs
  • Uses Terraform providers to manage resources (topic for later)

Automation Options

  • Run commands manually via CLI
  • Integrate with CI/CD pipelines (e.g., GitHub Actions)
    • Auto-detect changes in repo
    • Apply changes automatically

Benefits

  • Consistent, repeatable infrastructure provisioning
  • Easy creation, modification, and destruction of environments
  • Version-controlled changes (GitHub)
  • Scales across multiple environments and applications

Prerequisites

  • AWS fundamentals
  • Visual Studio Code setup
  • Git installed
  • Basic knowledge of Linux, shell scripting, YAML, JSON

✅ Key takeaway: DevOps engineers write .tf files in HCL, store them in GitHub, and use Terraform commands (or CI/CD pipelines) to provision, update, and destroy infrastructure by interacting with cloud provider APIs.

Top comments (0)