Working with Terraform / Lesson 1 of 2

Plan and Apply

Walk through the core Terraform workflow: init, plan, review, apply and destroy.

7 min Beginner 8 steps
Cloud accountEngineermain.tfTerraformaws_instance.webExecution planStateinit

IN THIS LESSON

Step 1 of 8

STEP 1 / 8

terraform init

Every project starts here. Init prepares the working directory: it downloads the providers and modules you reference and sets up the backend where state is stored.

$ terraform init

Initializing provider plugins...
- Installing hashicorp/aws v5.x

Terraform has been successfully initialized!

← → step · R replay · Space play/pause

YOU'LL LEARN

  • What terraform init, plan and apply each do
  • How to read the symbols in a plan
  • Why you review a plan before applying
  • How later changes only touch what differs
  1. terraform init. Every project starts here. Init prepares the working directory: it downloads the providers and modules you reference and sets up the backend where state is stored.
  2. terraform plan. Plan reads your configuration and the current state, then works out what would have to change. It writes nothing to your infrastructure, so it is safe to run as often as you like.
  3. Review the plan. Read the plan before you accept it. A plus means create, a tilde means update in place, a minus means destroy, and minus-slash-plus means replace. Surprises here, such as an unexpected destroy, are exactly what the plan is for.
  4. terraform apply. Apply carries out the plan. Terraform shows the plan again, asks you to confirm, and then makes the API calls in dependency order.
  5. State is updated. Once the server exists, Terraform records it in the state file, including its real ID. That record is what lets the next plan recognise the server as one it already manages.
  6. Change it and plan again. Edit the configuration, for example a bigger instance type, and plan again. Terraform compares against state and proposes only the difference. Everything that already matches is left alone.
  7. terraform destroy. When infrastructure is no longer needed, destroy removes everything Terraform tracks in state. It shows a plan first and asks for confirmation. Use it with care, especially outside throwaway environments.
  8. Recap. The workflow is the same every time: write, init, plan, review, apply.