D1 · IaC and Terraform Fundamentals

The purpose and advantages of Terraform

Why Terraform exists, what problems it solves with its multi-cloud and provider-agnostic approach, and the fundamental role of state in infrastructure management.

The purpose of Terraform

Terraform lets you define, plan, and apply infrastructure changes safely and efficiently, using a high-level configuration language (HCL) that describes the desired state of the infrastructure.

✍️

Write

You define resources in HCL: what you want to exist

🔍

Plan

Terraform compares the current vs. desired state and shows the changes

⚡

Apply

Terraform calls the provider APIs to make the changes

Multi-cloud and provider-agnostic advantage

Terraform uses the same language (HCL) and the same workflow to manage resources on any cloud provider or technology.

ScenarioWithout TerraformWith Terraform
Multi-cloud (AWS + Azure)2 tools, 2 languages, 2 different workflows1 HCL language, 1 init→plan→apply workflow
Reproducing the dev environment in stagingManual process, prone to differencesSame configuration, different variables
Distributed teamEach person applies changes their own wayGit + PR + apply reviewed by everyone
Migrating from AWS to GCPRewrite everything with native GCP toolsChange the provider, adapt resources
On-prem + hybrid cloudDifferent scripts per environmentVMware provider + AWS provider, same config

💡 Provider-agnostic in practice

Terraform has for more than 3,000 services: AWS, Azure, GCP, Kubernetes, GitHub, Datadog, PagerDuty, Cloudflare, MongoDB Atlas, and many more. Everything is managed with the same language and the same workflow.

The role of state

Terraform needs to know the current state of the infrastructure to calculate what changes to make. This mapping between configuration and real resources is stored in the .

Mapping

Records which Terraform resource corresponds to which real resource (with its ID in the provider).

Diff

When running , Terraform compares the state with the configuration to know what to create, modify, or destroy.

Metadata

Stores dependencies between resources so they are destroyed in the correct order.

Performance

On large infrastructures, using state avoids refreshing every resource on each plan.

Collaboration

The (S3, ) lets multiple people share the same state.

Secrets (⚠️)

State can contain sensitive values in plain text. Treat it as confidential data.

Dependency graph (DAG)

Terraform builds a directed acyclic graph (DAG) of all the resources. This lets it apply changes in parallel when safe, and serialize them when dependencies exist.

Example: VPC → Subnet → EC2

1

aws_vpc.main

Created first — no dependencies

2

aws_subnet.pub

Depends on aws_vpc.main (references vpc_id)

3

aws_instance.web

Depends on aws_subnet.pub (references subnet_id)

When destroying, the order is reversed: EC2 → Subnet → VPC.

🎯 For the exam

  • • Terraform detects implicit dependencies when you reference an attribute of another resource: vpc_id = aws_vpc.main.id
  • • For dependencies without a direct reference, use depends_on = [aws_vpc.main] (explicit dependency)
  • • Resources with no dependencies between them are created/destroyed in parallel for speed

Did you get this topic?

Test what you just learned

What is the main purpose of the terraform.tfstate file?

Ready to test "Terraform's Purpose and Advantages"?

Put what you learned to the test with original Terraform Associate exam questions.

Start Terraform Associate mock exam

Unlimited mock exams with the Pro plan

Log in to track your progress.