Lesson 1 of 4 Initializing, Validating and Formatting Configuration

The Core Terraform Workflow

Terraform work follows a loop of writing, planning and applying. This lesson describes the loop and how it changes from an individual to a team.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the write, plan and apply workflow.
  • Explain how the workflow changes from an individual to a team.

Write, plan, apply

HashiCorp describes the core workflow in three steps. In the write step you author configuration, just as you write application code, usually in a version-controlled repository. In the plan step you preview the changes Terraform will make. In the apply step Terraform makes those changes and records them in state.

  1. Write

    Edit .tf files; run terraform fmt and terraform validate for fast feedback.

  2. Plan

    Run terraform plan and review the proposed creates, updates and deletes.

  3. Apply

    Run terraform apply, confirm, and let Terraform make the changes and update state.

Around the loop sit the supporting commands. terraform init prepares the working directory before the first plan and after provider, module or backend changes. terraform destroy removes everything the configuration manages at the end of its life.

The following diagram shows the commands in order after a fresh clone.

The command order after a fresh clone. There is no .terraform directory yet, so terraform init comes first: it configures the backend, downloads modules and installs the locked provider versions. After the edit, fmt and validate tidy and check it, and plan shows the proposed change. Plan before init fails and asks for init.
fmt and validate are optional but recommended before plan.

The workflow for individuals and teams

An individual practitioner iterates in a tight loop: edit, plan, fix and plan again until the plan shows exactly the intended changes, then apply. Plans are reviewed by the same person who wrote the code.

In a team, each step gains a control. Writing happens on a branch. Opening a pull request runs a speculative plan, a plan that cannot be applied, and posts the result for reviewers. After approval and merge, a shared system, such as a CI pipeline or HCP Terraform, runs the plan and apply with centrally managed credentials and remote state with locking. Policy checks can block non-compliant plans. The three steps stay the same, but no one applies changes from a personal laptop.

Individual
WriteLocal edits
Planterraform plan in the terminal
Applyterraform apply from the laptop
Team
WriteBranch and pull request
PlanSpeculative plan posted to the pull request
ApplyApply after merge, in a pipeline or HCP Terraform, with approvals
Put in order

A team change

Exam skills 3.1.1 · 3.1.2

Arrange how a change moves through a team's Terraform workflow.

Log in to try this activity Interactive activities are free with an account.
Worked example

Ordering the commands

A new team member clones a repository and wants to see what would change if they increased an instance count. In which order should they run the commands?

The decisive fact is that a fresh clone has no .terraform directory.

They run terraform init first, which configures the backend, downloads modules and installs the locked provider versions. After editing the count, they can run terraform fmt and terraform validate, and then terraform plan to see the proposed change. Running plan before init fails with an error telling them to run init.

Check yourself

What are the three steps of the core Terraform workflow?

Show answer

Write, Plan and Apply.

Exam Tips

Remember for the exam

  • Core workflow: Write, Plan, Apply.
  • init prepares a directory; destroy ends the lifecycle.
  • Teams add pull requests, speculative plans and controlled applies.