Welcome to the first technical post of my Terraform series! Before we dive into complex configurations, we need to understand the "heartbeat" of Terraform: The Workflow.
In this post, weβll cover what Terraform actually does and the three commands you will use 90% of the time.
π§© What is Terraform?
At its core, Terraform is an Infrastructure as Code (IaC) tool that uses a declarative approach.
- Imperative (The "How"): You tell the cloud exactly what steps to take (e.g., "Step 1: Create a VPC, Step 2: Create a Subnet").
- Declarative (The "What"): You define the final state you want (e.g., "I want a VPC with this CIDR block"), and Terraform figures out how to make it happen.
π The Standard Terraform Workflow
The Terraform lifecycle follows a simple, repeatable four-step process. Whether you are deploying a single S3 bucket or a massive Kubernetes cluster, the steps remain the same:
1. βοΈ Write
You write your infrastructure code in files ending in .tf using HCL (HashiCorp Configuration Language). This is where you define your providers (like AWS or Azure) and your resources.
2. π Terraform Init
Before Terraform can do anything, it needs to prepare the working directory.
-
Command:
terraform init -
What it does: Downloads the necessary Provider plugins, initializes the backend, and sets up the local
.terraformfolder. -
Exam Tip: You must run
initevery time you add a new provider or change backend configurations.
3. π Terraform Plan
This is the "dry run" phase. It lets you see what will happen without actually making changes.
-
Command:
terraform plan - What it does: Compares your code against the State file and the real-world infrastructure. It tells you what will be Added, Changed, or Destroyed.
4. π Terraform Apply
This is where the magic happens.
-
Command:
terraform apply -
What it does: It executes the plan. Terraform calls the Cloud APIs to provision the resources and updates the
terraform.tfstatefile.
β οΈ Common Mistakes for Beginners
- Skipping the Plan: Always read your plan before hitting 'yes' on the apply.
-
Forgetting Init: If you copy a project to a new folder, you must run
terraform initagain. -
Editing State Manually: Never edit the
terraform.tfstatefile by hand.
π Exam-Focused Notes (003)
-
The "Destroy" Command:
terraform destroyis used to remove all managed infrastructure. -
Execution Plan: You can save a plan to a file using
terraform plan -out=path. -
Initialization:
terraform initdoes not create any infrastructure; it only prepares the environment.
π’ Whatβs Next?
Now that we know how Terraform works, we need to get it onto our systems. My next post will cover:
βInstalling & Setting Up Terraform (Step-by-Step)β
If you're following along, let me know which cloud provider you're planning to use with Terraform! π
Top comments (0)