Overview

1 A brief overview of Terraform

Infrastructure as Code transforms infrastructure work into repeatable, shareable software, and Terraform has become the de facto standard for doing so across vendors. The chapter opens with a hands-on story of moving from slow, manual cloud setup to defining a VPC once and reusing it reliably, highlighting gains in speed, quality, and accessibility for teams. Treating infrastructure like software unlocks version control, code reviews, automated tests, and CI/CD, while modules promote reuse and continual improvement. The result is reproducible environments, faster iteration, and a culture shift where developers can self-serve complex platforms with confidence.

Terraform’s power comes from clear separation of concerns and a declarative model. Developers describe the desired end state in HCL, and Terraform builds a dependency graph to plan and execute the required actions. Providers abstract vendor APIs, the CLI/Core executes plans, state is managed via backends, and workspaces represent individual deployments with their own configuration and state. The deployment workflow follows a predictable rhythm—write a change, initialize dependencies, generate and review a plan, then apply it—while concurrency accelerates real-world provisioning. Declarative dependency resolution simplifies ordering across complex stacks, though circular dependencies remain a common pitfall to avoid or work around.

These foundations enable a wide range of use cases, from spinning up machine learning training clusters to scalable web services and complex SSO configurations, all benefiting from modular design and repeatability. The chapter also explains the 2023 license change for Terraform and the community-driven fork, OpenTofu, which keeps an open-source path forward. OpenTofu is largely a drop-in replacement, maintaining compatibility while adding long-requested features, though release timing and feature availability can differ. Throughout the book, examples are designed to work with both Terraform and OpenTofu so readers can adopt the workflows, practices, and tooling that best fit their teams and constraints.

Terraform has multiple levels of abstraction that allow developers to get things done without worrying about vendor-specific minutiae.
Vendor Abstraction in Terraform using Providers
A diagram showing the relationship between the components in a Web Application.
An example of a circular dependency in which resources are dependent on one another.
The Terraform Development Flow
A Graph of the Terraform Plan. This graph was directly generated by Terraform using the terraform graph command.
An example machine learning training cluster.
Components that are common when deploying web services.
Single Sign-On System Resources
Terraform and OpenTofu forking development.

Summary

  • Infrastructure as Code allows Software Development best practices to be applied to infrastructure.
  • Declarative Languages focus on what the end results should look like and allow the underlying engine to figure out how to get there.
  • Terraform is built on top of the HashiCorp Configuration Language (HCL).
  • Terraform Plans are Directed Acyclic Graphs, which means that actions are done in a specific order and that circular dependencies are not allowed.
  • Terraform has a generic workflow- Init, Plan, Apply- that developers will use when deploying changes.
  • Terraform Init is where Terraform downloads any needed providers or modules.
  • Terraform Plan is where Terraform compares the resources in the workspace with the desired set of resources and configuration in the code to create a plan for aligning the two.
  • During the Terraform Apply phase, Terraform runs the actions from the plan.
  • HashiCorp is no longer releasing Terraform under an open source license, which has resulted in a new open source fork named OpenTofu being created.
  • OpenTofu and Terraform are mostly interchangeable, and the differences between them will be called out in this book.

FAQ

What is Infrastructure as Code (IaC), and how does Terraform fit into it?Infrastructure as Code lets you define and provision infrastructure using code and software engineering practices (version control, reviews, tests, CI/CD). Terraform is a vendor-agnostic IaC tool that uses a simple, declarative language (HCL) to describe the desired end state and then creates, updates, or destroys resources to match it. Its maturity and broad ecosystem have made it a de facto standard.
Why choose Terraform over vendor-specific or other IaC tools?Terraform is vendor-agnostic, so the same workflow and language work across thousands of providers (AWS, GCP, Azure, DNS, SaaS tools, even “fun” ones like pizza delivery). Compared to vendor-specific tools (e.g., CloudFormation), Terraform’s HCL is designed for readability and ease of use, and Terraform’s ecosystem is very broad and mature.
How do Providers, the Terraform CLI/Core, and vendor APIs work together?Terraform Core (in the CLI) reads your HCL and builds an execution plan. Providers are plugins (typically written in Go, communicating via gRPC) that translate Terraform’s requests into vendor-specific API calls and return results back to Core. This abstraction lets vendors and the community evolve providers independently of Terraform itself.
What are State, Backends, and Workspaces in Terraform?State is Terraform’s metadata about the resources it manages. A Backend defines where that state lives (local filesystem by default, or remote stores like S3, GCS, etc.), enabling team collaboration. A Workspace represents one deployment of a codebase with its own state and configuration—think “a saved file” in a “filesystem” (backend) analogy.
What is HCL and why does Terraform use a declarative language?HCL (HashiCorp Configuration Language) is human-readable and declarative: you describe the desired end state, not step-by-step instructions. Terraform then figures out the actions needed to reach that state. This improves readability, maintainability, and enables safe evolution without hand-written migration logic.
How does Terraform resolve dependencies, and what are the pitfalls?Terraform builds a Directed Acyclic Graph (DAG) of actions by analyzing references between resources (e.g., app uses DB outputs, so DB comes first). It can parallelize independent steps and enforce order where needed. A core pitfall is circular dependencies—cycles in the graph prevent Terraform from proceeding and usually require design changes or manual workarounds.
What are the key phases of the Terraform workflow (init, plan, apply)?Init downloads providers/modules and initializes the backend. Plan refreshes real resource state, compares it to your code, and produces a DAG of create/update/destroy actions. Apply executes that plan in the required order. Plans can be reviewed, stored, and used for gated approvals before apply.
How does Terraform integrate with CI/CD, and what are TACOS?Terraform code fits naturally into Git-based workflows with formatting, linting, tests, and automated plans/applies. TACOS (Terraform Automation & Collaboration Software) platforms—like HCP Terraform/Terraform Enterprise, Spacelift, and Scalr—integrate with GitHub/GitLab for speculative plans, policy checks, module registries, and controlled deployments. General CI tools (GitHub Actions, CircleCI, Jenkins) often complement these.
What real-world use cases does the chapter highlight?- Machine learning training clusters: rapidly spin up/down complex GPU clusters with networking, storage, permissions, and autoscaling. - Web/API platforms: reusable modules for load balancers, compute, SSL, DNS, caches, and databases. - SSO/identity management: codify users, groups, apps, and policies for auditability and safer change control. - Rapid prototyping: developers reuse battle-tested modules to experiment quickly without infrastructure overhead.
What is OpenTofu, why was it created, and is it compatible with Terraform?After HashiCorp moved Terraform to the Business Source License (BSL) in 2023, the community created OpenTofu as an open-source fork under the Linux Foundation. OpenTofu aims for compatibility with Terraform code and is adding long-requested features, effectively becoming a superset in some areas. Most examples work with both, though OpenTofu may lag slightly behind Terraform on brand-new features, and using OpenTofu-only features can complicate moving back. Many package managers now favor OpenTofu for open-source availability.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Terraform in Depth ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Terraform in Depth ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Terraform in Depth ebook for free