We close the chapter with a practical review of the commands you’ll use daily with Terraform. We’ve already seen some separately; here we bring them together in order, add two new ones (fmt and validate), and arrange them as a real workflow. Consider this your command cheat sheet.

The complete workflow

This is the typical order of commands in a Terraform project:

   fmt → validate → init → plan → apply → ... → destroy
  (format) (check)  (prepare) (preview) (apply)   (destroy)

Let’s go through them one by one.

terraform init: prepare the project

What it does: initializes the working directory. Downloads the providers (subchapter 11.1) and configures the state backend (subchapter 11.3).

When to run it:

  • The first time you work on a project.
  • When you add or change a provider.
  • When you change the backend configuration.
terraform init

It’s the first command you run in any new project. Without it, the other commands won’t work because the providers haven’t been downloaded.

terraform fmt: format the code

What it does: automatically formats your .tf files to have a consistent style (indentation, alignment, spaces). It doesn’t change the logic, just the appearance.

terraform fmt

Why use it: well-formatted code is easier to read and avoids style debates within the team. It’s like an automatic style checker. It’s usually run before saving or pushing changes. In teams, it’s checked automatically in CI (Chapter 22).

Analogy: fmt is like the “tidy/align” button in a document: it makes everything neat and uniform without changing the content.

terraform validate: check that the code is correct

What it does: checks that your code is valid syntactically and logically, without connecting to AWS or creating anything. It detects errors like misspelled arguments or non-existent references.

terraform validate

Why use it: it warns you of errors before you try to apply anything. It’s a quick and cheap check. If validate fails, there’s no point in continuing.

Difference with plan: validate only checks that the code is well written (doesn’t need credentials). plan goes further: it queries AWS and tells you what changes it would make. First validate (is it well written?), then plan (what will it do?).

terraform plan: preview the changes

What it does: shows what changes it would make without applying them. It compares your code, the state, and reality (subchapters 9.4 and 11.2).

terraform plan

Remember the symbols: + create, ~ modify, - destroy. It’s your safety net: you review before touching anything.

terraform apply: apply the changes

What it does: executes the changes for real, after showing you the plan and asking for confirmation (yes).

terraform apply

After applying, your real infrastructure matches your code, and the state is updated.

Tip: terraform apply already does a plan internally and shows it to you before asking for confirmation, so you can review one last time before typing yes.

terraform destroy: delete the infrastructure

What it does: destroys all managed resources, after confirmation. Ideal for cleaning up tests and stopping payments (subchapter 9.4).

terraform destroy

⚠️ Irreversible. Great for testing, dangerous in production. Use with care.

Command summary table

Command What it does Touches AWS? Asks for confirmation?
init Downloads providers and prepares backend No (just downloads) No
fmt Formats the code No No
validate Checks that the code is valid No No
plan Previews changes Reads No
apply Applies the changes Yes (writes) Yes (yes)
destroy Deletes everything Yes (deletes) Yes (yes)

Other useful commands (to know)

Besides the essentials, there are others you’ll use:

Command What for
terraform show View the current state or a saved plan
terraform state list List managed resources (subchapter 11.2)
terraform output View defined outputs (subchapter 10.1)
terraform refresh Sync state with reality
terraform import Bring existing resources into state (Chapter 20)
terraform graph View the dependency graph

A real workflow, step by step

This is what your day-to-day would look like creating new infrastructure:

1. terraform init       # only the first time (or when changing providers/backend)
2. (write your .tf code)
3. terraform fmt        # tidy up the code
4. terraform validate   # is it well written?
5. terraform plan       # what will change? -> review
6. terraform apply      # apply -> type "yes"
7. (use your infrastructure)
8. terraform destroy    # when you no longer need it (in tests)

Tip: Get used to always running plan before apply and reading the plan carefully. It’s the habit that will save you from costly mistakes. Many cloud scares are avoided simply by carefully reading what Terraform was about to do.

What you should remember

  • The typical flow is initfmtvalidateplanapplydestroy.
  • init: prepares the project (downloads providers, configures backend). First command, essential.
  • fmt: formats the code (clean and uniform style).
  • validate: checks that the code is valid, without touching AWS.
  • plan: previews the changes (doesn’t apply anything). Your safety net.
  • apply: applies the changes (asks for yes). destroy: deletes everything (asks for yes, irreversible).
  • Golden habit: always plan before apply, and read the plan carefully.

With this, you finish Chapter 11. You now have all the theoretical pieces of Terraform: language, providers, state, and commands. In Chapter 12 we’ll put it all together by building your first real infrastructure: a VPC with an EC2 server, step by step.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved