Terraform & Infrastructure as Code Explained 2026 - IaC, Terraform vs CloudFormation & Config Management
A beginner's guide to Infrastructure as Code with Terraform: what IaC is and why it matters, how Terraform works across clouds, Terraform vs CloudFormation, and how configuration management fits in.
Manually clicking through cloud consoles does not scale and is impossible to reproduce. Infrastructure as Code (IaC) fixes that - and Terraform is the most popular IaC tool in DevOps.
What is Infrastructure as Code?
IaC means defining your servers, networks and cloud resources in code files instead of setting them up by hand. That code can be versioned in Git, reviewed, and re-run to produce the exact same environment every time - giving you consistency, repeatability and speed.
The test of whether a team really practises IaC is simple: if the production environment were deleted tonight, could you rebuild it from the repository? If the answer involves anyone remembering what they clicked, the answer is no.
What is Terraform?
Terraform is a cloud-agnostic IaC tool that works across AWS, Azure, GCP and many other providers. You declare the infrastructure you want, and Terraform figures out how to create, update or delete resources to match that desired state.
Declarative is the key word. You do not write "create a server". You write "there is a server, with these properties", and Terraform compares that against what exists and works out the difference. Run it twice and nothing happens the second time.
Terraform, OpenTofu and the licence change
One thing older tutorials get wrong: Terraform is no longer open source in the strict sense. In August 2023 HashiCorp moved it from the Mozilla Public License to the Business Source License (BSL 1.1). For almost everyone learning it or using it at work, nothing practical changed. You can still download Terraform and use it free of charge to manage your own infrastructure. What the licence restricts is building a competing commercial product on top of it.
The change did produce a fork. OpenTofu was forked from the last open-source release, is run under the Linux Foundation and is still MPL-licensed. It reads the same HCL and, for configuration like the example below, behaves the same way: you type tofu plan where you would type terraform plan. HashiCorp itself is now part of IBM, which completed the acquisition in February 2025.
What this means for you: learn HCL and the plan-and-apply workflow, and the skill carries over to either tool. Put "Terraform" on your resume, because that is the word job descriptions use. If an interviewer asks about OpenTofu, knowing why it exists shows you keep up with the field.
What Terraform code actually looks like
Here is a small but complete configuration - a VPC-attached EC2 instance with its security group. This is roughly the first thing you will write.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = var.region
}
variable "region" {
type = string
default = "ap-south-1"
}
resource "aws_security_group" "web" {
name = "web-sg"
description = "Allow HTTP in"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
data "aws_ami" "al2023" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["al2023-ami-2023.*-x86_64"]
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.al2023.id
instance_type = "t3.micro"
vpc_security_group_ids = [aws_security_group.web.id]
tags = {
Name = "web-01"
ManagedBy = "terraform"
}
}
output "public_ip" {
value = aws_instance.web.public_ip
}
Two details in there are worth internalising early. The instance references the security group as aws_security_group.web.id rather than a hardcoded ID - that reference is how Terraform works out it must create the security group first, without you specifying any ordering. And the AMI comes from a data lookup rather than a pasted ami- string, because AMI IDs differ per region and change whenever the image is rebuilt; hardcoding one is the reason a configuration that worked in Mumbai fails in Singapore.
The workflow: init, plan, apply
terraform initdownloads the providers named in your configuration. Run it once per project, and again with-upgradewhen you want newer provider versions, because the.terraform.lock.hclfile it writes keeps you on the versions it first selected. Commit that file.terraform planshows what would change, without changing anything. It is the most important command in the tool.terraform applymakes those changes, after showing you the plan again and asking for confirmation.terraform destroytears it all down. Useful for learning environments, terrifying elsewhere.
Read every plan before applying it. Terraform expresses changes as + create, ~ update in place, and -/+ destroy and recreate. That third one is the one to watch: some attribute changes force a resource to be replaced rather than modified, and on a database that is the difference between an edit and an outage.
State - the concept that catches everyone out
Terraform records what it created in a state file. That file is how it knows the difference between "this resource does not exist yet" and "this resource exists and needs updating". Without it, Terraform cannot tell what it owns.
Three consequences follow, and they cause most beginner incidents:
- State must not live only on your laptop. If a teammate runs apply from their own machine with their own state, you will get duplicated infrastructure. Use a remote backend - an S3 bucket, Azure Storage, GCS, or HCP Terraform (called Terraform Cloud until 2024).
- State must be locked. Two people applying at the same time against the same state corrupts it. The S3 backend can now lock natively, with a small lock file stored next to the state (
use_lockfile = true, added in Terraform 1.10). The older DynamoDB lock table still works but is deprecated, so new setups should not start with it. - State is not a secret store, but it contains secrets. Database passwords and generated keys end up in it in plain text, unless the resource offers a write-only argument (Terraform 1.11 and later), such as
password_woonaws_db_instance. Never commit it to Git, and encrypt the bucket it lives in.
A remote backend with locking and encryption is a short block:
terraform {
backend "s3" {
bucket = "acme-terraform-state"
key = "prod/network/terraform.tfstate"
region = "ap-south-1"
encrypt = true
use_lockfile = true
}
}
Create the bucket first, by hand or from a separate small configuration, with versioning switched on. Versioning is your undo button if a bad apply ever writes broken state. Give each environment its own key, so dev and prod never share a state file.
If someone changes infrastructure by hand in the console, Terraform's state no longer matches reality - this is drift. The next plan will try to undo their change. That is the tool working correctly, and it is also the reason teams that adopt IaC halfway have a bad time.
Modules
A module is a reusable folder of Terraform configuration - the same idea as a function. Once you have written a VPC three times, you write it once as a module with variables for the bits that differ, and call it from each environment. The public Terraform Registry has maintained modules for most common patterns, and reading a well-written one is a fast way to learn the idioms.
The usual advice is to start without modules. Write the plain resources until the repetition genuinely annoys you, then extract. Modules written before you understand the pattern tend to have the wrong variables.
Terraform vs CloudFormation vs Ansible
| Terraform | CloudFormation | Ansible | |
|---|---|---|---|
| Job | Provision infrastructure | Provision infrastructure | Configure what runs on it |
| Scope | Any cloud, one workflow | AWS only | Any machine over SSH |
| State | Explicit state file you manage | AWS tracks it for you as a stack | None - it checks and converges each run |
| Language | HCL | YAML or JSON | YAML playbooks |
Terraform and Ansible are not competitors - most teams use both. Terraform creates the servers; Ansible installs and configures what runs on them. If a question in an interview frames them as alternatives, saying so is a good answer.
Terraform vs CloudFormation
CloudFormation is AWS's native IaC service - deeply integrated with AWS but locked to it. Terraform works across every major cloud with one workflow, which is why teams running multi-cloud or wanting portability prefer it. If you are 100% on AWS forever, CloudFormation is fine; otherwise Terraform is the safer skill to learn.
How configuration management fits in
IaC tools like Terraform provision infrastructure (create the servers). Configuration management tools like Ansible then configure what runs on them (install packages, set up services) so every server stays consistent. Together they ensure environments are reproducible from scratch - no manual drift.
Mistakes that cost people their first week
- Committing
terraform.tfstateto Git. Add it to.gitignorebefore your first commit, not after. - Running
applywithout reading the plan, then discovering-/+next to a database. - Hardcoding credentials in a
.tffile. Use environment variables or the provider's own credential chain. - One giant
main.tfholding every environment, so a change to dev shows up in the production plan. Separate state per environment. - Not pinning provider versions, then finding a fresh
initpulls a new major and half the plan changes.
Adopting resources someone built by hand: import blocks
Very few people start Terraform on an empty cloud account. Your first real task is more likely to be a bucket or security group that someone created in the console years ago and that now has to live in code. Older tutorials use the terraform import command for this, which writes straight into state without showing you anything first. Since Terraform 1.5 the better route is an import block, which goes through plan like any other change. It works the same in 1.17, the current release.
Suppose a bucket called acme-app-logs was made by hand. Add this to your configuration:
import {
to = aws_s3_bucket.logs
id = "acme-app-logs"
}
Then let Terraform draft the matching resource block for you:
terraform plan -generate-config-out=generated.tf
Three things to check as you go:
- The plan summary. It should read
1 to import, 0 to add, 0 to change, 0 to destroy. If it also wants to change or replace something, your configuration does not yet describe the real bucket. Adopting a resource should never modify it. - The generated file. HashiCorp still labels config generation experimental. It writes out every attribute, including defaults nobody would type, and for some resource types it produces arguments that conflict with each other. Trim it, move the resource to the file it belongs in, and swap fixed values for variables. The output file must not exist already, or the plan stops with an error.
- The import block afterwards. Once
terraform applyhas run, the bucket is in state. Importing to the same address again does nothing, so you can delete the block or keep it as a note of where the resource came from.
A useful exercise: create a bucket by hand in the console, adopt it this way, then change one tag in code and confirm the next plan shows a single in-place update. That round trip, from console to code to a clean plan, is the same job you will do on inherited infrastructure, only smaller.
Industry usage
Companies use Terraform to manage cloud infrastructure across dev, staging and production reliably, making it a core, well-paid DevOps skill.
It is also one of the more interview-friendly skills to demonstrate, because a small public repository containing a working configuration and a sensible module structure proves more than any certificate does.
Terraform is one of several tools employers test — to learn the full stack, our live DevOps certification course covers the full 2026 stack across five AWS projects in small batches. Fees are published openly (₹35,000), and placement assistance (resume review, mock interviews and referrals) is included — no job guarantee.
Related Learning Resources
Kubernetes Explained Simply 2026 - Orchestration, Networking & K8s vs Docker Swarm
Kubernetes for beginners: what it is, core concepts (pods, deployments, services, clusters), how Kubernetes networking works, and how K8s compares to Docker Swarm.
Docker Explained for Beginners 2026 - Containers, Images & Docker vs VMs
A beginner's guide to Docker: what containers are, how images and Dockerfiles work, how Docker differs from virtual machines, and how DevOps teams use Docker in CI/CD and Kubernetes.
GitOps Explained: ArgoCD, Flux and Progressive Delivery
How the reconciliation loop actually works, how Argo CD and Flux differ once you live with them, what canary and blue-green add on top, and the secrets and drift problems tutorials skip.
Ready to Start Your DevOps Career?
Join our comprehensive DevOps + GenAI course with hands-on projects, live mentorship, and placement support