Infrastructure as Code (IaC) has transformed the way we build and manage cloud environments. Instead of clicking through a cloud console, we define infrastructure in code and let automation handle the rest.
One of the most important concepts to understand in Terraform is its lifecycle:
Write → Init → Plan → Apply → Destroy
In this blog, we’ll break down each stage clearly with examples so you can confidently use Terraform in real-world projects.
Terraform is an Infrastructure as Code (IaC) tool created by HashiCorp. It allows you to define, provision, and manage infrastructure using declarative configuration files written in HCL (HashiCorp Configuration Language).
Terraform works with cloud providers like:
Now let’s understand how the lifecycle works.
Everything starts with writing configuration files.
Terraform configurations are written in .tf files using HCL.
Example:
1provider "aws" {
2 region = "us-east-1"
3}
4
5resource "aws_instance" "web" {
6 ami = "ami-123456"
7 instance_type = "t2.micro"
8}Here, we are declaring:
This defines the desired state of infrastructure.
Important:
You are not writing steps. You are describing the final state.
terraform init)Before Terraform can create anything, it must initialize the working directory.
Run:
terraform initThink of init as: “Prepare Terraform to work with this project.”
You only need to run this once per project (or after adding new providers/modules).
terraform plan)Now comes the most important safety step.
Run:
terraform planTerraform compares:
.tf files)It then generates an execution plan showing what will change.
Example output:
+ aws_instance.webMeaning: Terraform will create a new EC2 instance.
Important:
plan does NOT change anything.terraform apply)Once you are satisfied with the plan, run:
terraform applyTerraform will:
After confirmation, Terraform calls the provider APIs and provisions infrastructure.
At this stage:
Terraform keeps track of infrastructure using a state file:
terraform.tfstateThe state file:
Without state, Terraform wouldn’t know what it created.
In team environments, state is usually stored remotely (e.g., Terraform Cloud or S3 backend) to prevent conflicts.
terraform destroy)When you no longer need the infrastructure, run:
terraform destroyTerraform will:
Common use cases:
Warning:
Always double-check before running destroy in production.
#