Opscraft
Terraform · Lesson 1 of 12 · 7 min read

Infrastructure as code and how Terraform works

Terraform is the most requested infrastructure-as-code tool in Bangalore DevOps job descriptions. If you already know CloudFormation, EKS or GitOps, the concepts will feel familiar; what interviews test is fluency in HCL, a real understanding of state, the module and environment patterns teams use, and the team workflow around plans and applies. This topic is written to take you from zero to interview-ready, ending with a worked EKS platform example you can build as a portfolio project.

What Terraform is

Terraform (by HashiCorp) is a declarative IaC tool: you describe the infrastructure you want in HCL (HashiCorp Configuration Language) files, and Terraform works out the API calls to create, change or delete real resources to match.

  • Providers are plugins that talk to APIs — aws, kubernetes, helm, github, datadog, cloudflare — over 4,000 in the public registry. That's the multi-cloud/multi-service strength.
  • Resources are things Terraform manages (aws_vpc, aws_eks_cluster).
  • Data sources read things it doesn't manage (an existing VPC, the latest AMI).
  • State records the mapping between your configuration and real-world resource IDs.
Enlarge diagramYour .tf filesdesired infrastructureStatewhat Terraform createdRealitywhat exists in AWS nowterraform planthe differenceterraform applyprovider API callscreate, change ordelete
Figure 1. How Terraform works: you write the desired state, Terraform compares it with what it knows (state) and what exists (reality), then calls provider APIs to close the gap.
Swipe sideways to see all of it, or tap to open it full screen.

The core workflow

terraform init        # download providers & modules, configure the backend
terraform fmt         # format code to the canonical style
terraform validate    # syntax and internal consistency (no API calls)
terraform plan        # compare config ↔ state ↔ reality; show the changes
terraform apply       # execute the plan (asks for confirmation)
terraform destroy     # delete everything this configuration manages
Enlarge diagraminitproviders,modules,backendfmttidy formattingvalidatesyntax, no APIcallsplanshow thechangesapplymake thechanges
Figure 2. The everyday commands in order. In CI, plan is saved to a file and apply runs exactly that saved plan.
Swipe sideways to see all of it, or tap to open it full screen.

Plan output symbols: + create, - destroy, ~ update in place, -/+ destroy and recreate (replacement), <= read (data source). Always read the summary line: Plan: 3 to add, 1 to change, 0 to destroy.

Enlarge diagram+~--/+<=createa new resourceupdate in placethe same resource is changeddestroythe resource is deletedreplacedestroy then create: new ID, possible data lossreada data source is read
Figure 3. Reading plan output. The symbol to stop at is -/+: the resource will be destroyed and recreated.
Swipe sideways to see all of it, or tap to open it full screen.

Save a plan and apply exactly that plan (standard in CI):

terraform plan -out=tfplan
terraform show tfplan            # human-readable
terraform show -json tfplan      # machine-readable (policy checks)
terraform apply tfplan           # no surprises: applies only what was reviewed

How plan works internally

  1. Refresh: read the current state of each managed resource from the provider APIs (detecting drift).
  2. Diff: compare desired configuration with the refreshed state.
  3. Graph: build a dependency graph from references between resources (aws_subnet.a.vpc_id = aws_vpc.main.id means the subnet depends on the VPC).
  4. Order: create/update in dependency order, destroy in reverse order, and run independent operations in parallel (default parallelism 10, -parallelism=n).
Enlarge diagramRefreshread every managedresource from the API(finds drift)Diffcompare config withrefreshed stateGraphdependencies fromreferencesOrdercreate in order,destroy in reverse,parallel wherepossible1234
Figure 4. What happens inside terraform plan.
Swipe sideways to see all of it, or tap to open it full screen.

Dependencies are usually implicit through references. depends_on adds explicit ones for hidden dependencies (e.g. an IAM policy attachment that must exist before a Lambda runs).

Enlarge diagramaws_vpc.mainaws_subnet.avpc_id = vpc.main.idaws_security_group.webaws_instance.websubnet + security groupcreate: left → right · destroy: right → left · subnet and security group in parallel
Figure 5. References create the dependency graph. The subnet uses the VPC id, so the VPC is created first and destroyed last; unrelated resources run in parallel.
Swipe sideways to see all of it, or tap to open it full screen.

Terraform vs alternatives

Terraform CloudFormation AWS CDK Pulumi
Language HCL YAML/JSON TypeScript/Python/Java... → CloudFormation General-purpose languages
Clouds/services Multi-cloud + SaaS AWS only AWS (CDK for Terraform exists) Multi-cloud
State You manage it (S3 backend etc.) AWS manages it (stacks) AWS-managed (via CFN) Managed service or self-managed
Preview plan Change sets cdk diff pulumi preview
Drift detection plan / refresh-only Drift detection feature Via CFN refresh
Rollback on failure No automatic rollback (partial apply stays) Automatic stack rollback Via CFN No
New AWS features Usually fast (provider updates) Sometimes delayed coverage Via CFN Fast

How to talk about it if your background is CloudFormation: "I've built and run CloudFormation stacks; the declarative model, dependency ordering and change preview are the same ideas. Terraform's differences are explicit state I have to manage and protect, a much larger provider ecosystem, modules from the registry, and no automatic rollback — so plan review and small blast radius matter more."

Licence and OpenTofu

In 2023 HashiCorp moved Terraform from the open-source MPL licence to the Business Source License (BSL). The community forked the last open version as OpenTofu (Linux Foundation), which is largely drop-in compatible (tofu init/plan/apply) and has added features of its own (e.g. state encryption). IBM acquired HashiCorp in 2025. Interviewers may ask; the practical answer is that both work and many teams still use Terraform, with some regulated orgs preferring OpenTofu.