Logo The AI Dev
Back to Article
Deep Dive Note

The Workflow Of Terraform

Day 1: The Terraform Workflow

Understanding the core Terraform workflow is essential to using the tool effectively. It’s a simple, repeatable process that ensures safety, predictability, and control over your infrastructure. The workflow revolves around four key commands: init, plan, apply, and destroy.

Let’s break down each step of this workflow.

The Core Workflow: init -> plan -> apply

This three-step process is what you’ll use every time you create or update infrastructure with Terraform.

1. terraform init

The terraform init command is the first command you’ll run in a new Terraform configuration. It performs several initialization steps to prepare the working directory for use with Terraform.

What does it do?

You only need to run terraform init once per session, or whenever you add a new provider to your configuration.

+-----------------------------+      +-----------------------------+
|      Your Local Machine     |      |   Terraform Registry        |
+-----------------------------+      +-----------------------------+
|                             |      |                             |
|  terraform.tfvars           |      |  +-----------------------+  |
|  main.tf  (defines provider)|      |  | Provider "aws" v5.0   |  |
|  ...                        |      |  +-----------------------+  |
|                             |      |                             |
|  `terraform init` is run    |      |                             |
|                             |      |                             |
|  Downloads provider "aws"   | <----o                             |
|                             |      |                             |
|  Installs it in .terraform/ |      |                             |
+-----------------------------+      +-----------------------------+

2. terraform plan

The terraform plan command is arguably the most important command in the Terraform workflow. It creates an execution plan, which is a “dry run” of the changes Terraform will make to your infrastructure.

What does it do?

The output of terraform plan is color-coded for clarity:

This is your opportunity to review and sanity-check the changes before they are applied. This step prevents surprises and gives you confidence that the changes you’re making are the ones you intended.

Scenario: A Sanity Check

You’ve written some Terraform code to create a new EC2 instance. You run terraform plan and see the following output:

Terraform will perform the following actions:

  # aws_instance.web will be created
  + resource "aws_instance" "web" {
      + ami           = "ami-0c55b159cbfafe1f0"
      + instance_type = "t2.micro"
      + ...
    }

Plan: 1 to add, 0 to change, 0 to destroy.

This confirms that Terraform is going to create one new resource, as expected. If you saw a plan that was going to destroy existing resources, you would know that something was wrong with your configuration and you could fix it before applying the changes.

3. terraform apply

The terraform apply command executes the plan generated by terraform plan. It makes the actual API calls to the cloud provider to create, update, or destroy resources.

What does it do?

By default, terraform apply will show you the plan again and ask for your confirmation before proceeding. You can skip this interactive prompt by using the -auto-approve flag, which is common in CI/CD environments.

The Teardown Workflow: terraform destroy

Sometimes, you need to tear down the infrastructure you’ve created. This is where the terraform destroy command comes in.

4. terraform destroy

The terraform destroy command is a convenient way to destroy all of the resources managed by your Terraform configuration.

What does it do?

This command is incredibly useful for cleaning up temporary environments, such as those used for development, testing, or training.


This init -> plan -> apply workflow is the bedrock of using Terraform. It provides a safe, predictable, and repeatable process for managing the entire lifecycle of your infrastructure. As you become more familiar with these commands, you’ll start to appreciate the power and elegance of this workflow.