DEV Community

smit_kalathiya
smit_kalathiya

Posted on

1. Terraform Basics & Workflow

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 .terraform folder.
  • Exam Tip: You must run init every 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.tfstate file.

⚠️ 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 init again.
  • Editing State Manually: Never edit the terraform.tfstate file by hand.

πŸŽ“ Exam-Focused Notes (003)

  • The "Destroy" Command: terraform destroy is used to remove all managed infrastructure.
  • Execution Plan: You can save a plan to a file using terraform plan -out=path.
  • Initialization: terraform init does 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)