Skip to content
Optimal Lab

DevOps · interactive

Terraform: plan and apply

You describe the infrastructure you want. Terraform compares it with what exists and prints a plan of exactly what it will create, change or destroy. Nothing happens until you apply. Make a change and read the plan.

$ terraform plan

aws_instance.web no changes

aws_db_instance.main no changes

+ aws_s3_bucket.assets will be created

bucket = "shop-assets"

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

A new block in the config with nothing in state: Terraform will create it.

What changed

Step 1 of 4

  1. 1

    Write the desired state

    HCL files describe resources and their settings, not the steps to make them.

    resource "aws_instance" "web" { … }

  2. 2

    Plan diffs config, state and reality

    Terraform reads the real resources, compares them with your config, and works out create, update, replace or destroy for each.

    Some settings can't change in place and force a replacement.

  3. 3

    Review the plan

    In CI, post the plan on the pull request. Look hard at every - and -/+ before merging.

    terraform plan -out=tfplan

  4. 4

    Apply the saved plan

    Applying the saved plan does exactly what was reviewed, then records the result in state.

    terraform apply tfplan

In production

Tools and technologies

Infrastructure as code tools all follow plan, review, apply.

Not tied to one language — use these names when you search docs or design a system.