Skip to content

Terraform Overview: What Is Infrastructure as Code?

DodaTech 5 min read

Terraform is an open-source Infrastructure as Code tool by HashiCorp that lets you define and provision cloud infrastructure using declarative configuration files instead of manual CLI commands or web consoles.

What You'll Learn

In this tutorial, you will learn what Terraform is, how Infrastructure as Code transforms cloud operations, and why declarative configuration beats manual infrastructure management for reliability and scale.

Why It Matters

Manual infrastructure management through cloud consoles or ad-hoc scripts leads to configuration drift, unrepeatable deployments, and human error. Infrastructure as Code solves this by treating infrastructure like application code -- version-controlled, reviewed, tested, and deployed through automated pipelines. Teams using IaC report 80 percent fewer deployment failures and 90 percent faster recovery times.

Real-World Use

DodaTech manages its entire cloud infrastructure across AWS and Azure using Terraform modules. Durga Antivirus Pro's backend auto-scaling group, database clusters, and CDN distribution are all defined in Terraform configuration files stored in Git and deployed through CI/CD pipelines.

What Is Infrastructure as Code?

Infrastructure as Code (IaC) is the practice of managing infrastructure -- servers, networks, databases, load balancers -- through machine-readable definition files rather than manual processes. Instead of clicking through a cloud console to create an EC2 instance, you write a configuration file that describes the desired state.

Declarative vs Imperative

Terraform uses a declarative approach. You define what you want the final infrastructure to look like, and Terraform figures out how to get there. The alternative is imperative IaC (like Ansible or Chef scripts) where you specify step-by-step commands.

# Declarative: you describe the result
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

Expected output: Terraform creates exactly one EC2 instance with the specified AMI and type, regardless of the current state.

The Terraform Workflow

The core Terraform workflow has three steps: Write, Plan, Apply. You write configuration files describing desired infrastructure. You run <a href="/devops/terraform/">terraform</a> plan to preview changes. You run <a href="/devops/terraform/">terraform</a> apply to execute them.

Core Concepts

Providers

Providers are plugins that let Terraform interact with cloud platforms and services. Each provider exposes resources and data sources specific to that platform. AWS, Azure, GCP, Kubernetes, and hundreds of others are available.

Resources

Resources are the most important element in Terraform. They represent infrastructure objects like virtual machines, DNS records, storage buckets, and database instances.

State

Terraform tracks the current state of your infrastructure in a state file. This file maps your configuration to real-world resources and is essential for detecting drift and planning changes.

# A complete Terraform configuration
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "data" {
  bucket = "my-company-data-lake"
  tags = {
    Environment = "Production"
    ManagedBy   = "Terraform"
  }
}

Expected output: Terraform configures the AWS provider and creates an S3 bucket named "my-company-data-lake" with environment tags.

Benefits of Terraform

Version Control

All infrastructure definitions live in Git. Changes go through pull requests, code review, and approval workflows. You can roll back to any previous state.

Consistency

The same configuration produces identical infrastructure across development, staging, and production environments. No more "it works on my machine" for infrastructure.

Automation

Terraform integrates with CI/CD pipelines, enabling fully automated provisioning and change management. Infrastructure updates become part of the normal software delivery process.

Multi-Cloud

Terraform supports over 2000 providers. You manage AWS, Azure, GCP, and on-premises infrastructure using the same tool, language, and workflow.

Common Mistakes

1. Editing State Files Directly

Never edit the state file manually. Always use <a href="/devops/terraform/">terraform</a> state commands or modify configuration and reapply.

2. Not Using Version Constraints

Always pin provider and module versions. Unpinned versions cause unexpected upgrades that break infrastructure.

3. Storing Secrets in Configuration

Never hardcode passwords, API keys, or tokens in Terraform files. Use variables, environment variables, or a secrets manager.

4. Running Apply Without Plan

Always run <a href="/devops/terraform/">terraform</a> plan first and review the output. Blind applies can destroy or modify resources unexpectedly.

5. Ignoring State File Security

The state file contains sensitive data including resource IDs and sometimes plaintext secrets. Store it in a secure remote backend with encryption.

Practice Questions

1. What is the difference between declarative and imperative IaC? Declarative IaC (Terraform) describes the desired end state. Imperative IaC (Ansible, Chef) describes step-by-step commands to reach the state.

2. What are the three steps in the core Terraform workflow? Write configuration files, run <a href="/devops/terraform/">terraform</a> plan to preview changes, run <a href="/devops/terraform/">terraform</a> apply to execute them.

3. Why does Terraform maintain a state file? The state file maps configuration to real-world resources, enabling Terraform to detect drift, plan accurate changes, and manage resource lifecycle.

4. How does Terraform support multi-cloud strategies? Terraform has over 2000 providers covering all major cloud platforms. You use the same tool, language, and workflow across AWS, Azure, GCP, and others.

5. Challenge: Write a Terraform configuration that creates an AWS S3 bucket with versioning enabled and a lifecycle rule to expire objects after 90 days.

Mini Project: First Infrastructure Deployment

Create a directory called <a href="/devops/terraform/">terraform</a>-demo with a main.tf file. Define the AWS provider, create an S3 bucket, and run <a href="/devops/terraform/">terraform</a> init, <a href="/devops/terraform/">terraform</a> plan, and <a href="/devops/terraform/">terraform</a> apply. Verify the bucket exists in the AWS console, then run <a href="/devops/terraform/">terraform</a> destroy to clean up.

Install Terraform
HCL Syntax

What's Next

Continue to Install Terraform to set up the CLI on your system, then study HCL Syntax to write your first configuration files.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro