Terraform Fundamentals

💤0
Lv 10 XP
← 📜 Infrastructure as Code · Terraform

Terraform Fundamentals

Beginner ⭐ 50 XP ⏱ 20 min #terraform#iac#hcl

The Terraform workflow, HCL syntax, and the core building blocks.

📖Theory

Terraform provisions infrastructure declaratively across any cloud. You write HCL describing the desired state; Terraform figures out the changes to get there. The core workflow:

  • terraform init — download providers and set up the backend
  • terraform plan — preview create/change/destroy actions
  • terraform apply — make it so
  • terraform destroy — tear it all down

Building blocks: providers (cloud plugins), resources (things to create), variables (inputs), outputs (results), and data sources (read existing infrastructure). It’s declarative and idempotent — re-running converges to the desired state.

🌍Real-World Example
variable "region" { default = "eu-west-1" }

provider "aws" {
  region = var.region
}

resource "aws_s3_bucket" "data" {
  bucket = "my-app-data-12345"
  tags   = { env = "dev" }
}

output "bucket_name" {
  value = aws_s3_bucket.data.bucket
}
terraform init
terraform plan
terraform apply
✍️Hands-On Exercise
  1. List the four main Terraform CLI commands and what each does.
  2. Write a variable, use it in a provider, and add an output.
  3. Explain why Terraform is described as declarative and idempotent.
  4. Run a plan and identify a create vs a destroy action in the output.
🧾Cheat Sheet
Command / blockPurpose
terraform initDownload providers, init backend
terraform planPreview changes
terraform applyApply changes
terraform destroyRemove everything
providerCloud plugin
resourceThing to create
variable / outputInputs / results
dataRead existing infra
💬Common Interview Questions
What's the Terraform core workflow?

init (download providers/backend), plan (preview changes), apply (execute), and destroy (tear down). plan/apply is the everyday loop.

What does it mean that Terraform is declarative?

You describe the desired end state, not the steps. Terraform compares it to current state and computes the minimal changes — and re-running is idempotent.

What is a data source?

A read-only lookup of existing infrastructure (e.g. an existing VPC or AMI) that you reference in your configuration without managing it.

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type