Tools: 1. Terraform Basics & Workflow

Tools: 1. Terraform Basics & Workflow

Source: Dev.to

🧩 What is Terraform? ## πŸ”„ The Standard Terraform Workflow ## 1. ✍️ Write ## 2. 🏁 Terraform Init ## 3. πŸ“ Terraform Plan ## 4. πŸš€ Terraform Apply ## ⚠️ Common Mistakes for Beginners ## πŸŽ“ Exam-Focused Notes (003) ## πŸ“’ What’s Next? 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. At its core, Terraform is an Infrastructure as Code (IaC) tool that uses a declarative approach. 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: 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. Before Terraform can do anything, it needs to prepare the working directory. This is the "dry run" phase. It lets you see what will happen without actually making changes. This is where the magic happens. 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! πŸš€ Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse - 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. - 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. - 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. - 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. - 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. - 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.