Skip to main content
In this lesson you’ll learn the standard Terraform workflow that lets you manage infrastructure safely, consistently, and with full auditability. The workflow is intentionally simple so you can review and control every change. Core workflow steps:
  • Write configuration (create .tf files)
  • Initialize the working directory (terraform init)
  • Validate and format (terraform fmt, terraform validate)
  • Plan changes (terraform plan)
  • Apply changes (terraform apply)
  • Inspect state and outputs
  • Manage environments (optional workspaces)
  • Destroy infrastructure when needed (terraform destroy)
Each step reduces risk and makes infrastructure changes predictable. Below we walk through the workflow, show common commands, and list best practices.
Store Terraform configurations in version control, run terraform fmt and terraform validate in CI, and always review terraform plan output before applying changes.

1) Write configuration

Author Terraform configuration files using the HashiCorp Configuration Language (HCL). Organize code into logical files such as main.tf, variables.tf, outputs.tf, and providers.tf. Example minimal configuration:
Best practices when writing config:
  • Break code into purpose-specific files for readability and reuse.
  • Use variables for environment-specific values and provide sensible defaults.
  • Pin provider versions in required_providers to avoid unexpected upgrades.
  • Avoid hard-coding secrets — use secret managers or environment variables.

2) Initialize the working directory

Initialization downloads providers and configures the backend. Run this once per directory (or whenever backend/provider requirements change):
What terraform init does:
  • Downloads provider plugins required by your configuration.
  • Initializes and validates the backend (local or remote) used to store state.
  • Prepares the working directory for subsequent Terraform commands.
Useful flags:
  • -backend-config="key=value" — override backend config values at init time.
  • -reconfigure — ignore existing backend configuration and reconfigure.

3) Validate and format

Catch errors early and keep code consistent:
terraform validate performs static checks on your configuration. Combine these commands in pre-commit hooks or CI pipelines.

4) Plan changes

terraform plan shows a preview of the changes Terraform will make—do not skip this step. Generate a human-readable plan:
Create a saved plan (binary) for automation and safe applies:
Notes:
  • Use -out to create a plan file that can be applied later (recommended for CI/CD).
  • The plan uses symbols: + create, ~ update in-place, -/+ destroy and recreate, - destroy.
  • Inspect a saved plan with terraform show plan.tfplan.

5) Apply changes

Apply interactive changes (prompts for confirmation):
Apply from a saved plan (non-interactive, safe for automation):
When you apply:
  • Terraform updates real resources to match your configuration.
  • The state file is written/updated to reflect the new resource state.
  • When using a remote backend, state locking prevents concurrent writes.
Always review the terraform plan output before applying. Plans indicate destructive changes (resource deletions) — verify those carefully.

6) Inspect state and outputs

Commands for examining Terraform state and outputs:
Avoid manual edits to the state file unless absolutely necessary and you fully understand the implications.

7) Manage environments with workspaces (optional)

Workspaces allow multiple states within the same configuration directory (e.g., dev, prod):
Workspaces are convenient for small environment splits but for larger teams or stricter separation consider separate state backends or independent Terraform configurations.

8) Destroy infrastructure

Remove resources managed by a configuration when needed:
Or create and apply a destroy plan:
terraform destroy shows a plan and prompts before deleting resources.

Quick reference — common Terraform commands

Additional best practices

  • Store state in a remote backend (e.g., Amazon S3 + DynamoDB for locking, or Terraform Cloud/Enterprise) to enable collaboration and locking.
  • Pin provider versions in required_providers to prevent unexpected behavior after provider updates.
  • Use CI pipelines to run terraform plan and require code review before terraform apply.
  • Enforce terraform fmt and terraform validate in CI or pre-commit hooks.
  • Avoid storing sensitive secrets in .tf files. Use secure variables, secret management systems, or encrypted storage.
  • Use -out plans in automation to separate plan and apply phases and to guarantee the exact plan is applied.
This lesson provided a concise, practical workflow for using Terraform safely and predictably: write configuration, initialize, validate/plan, apply, inspect state, manage environments, and destroy when needed. For deeper dives, consult the linked Terraform documentation and provider-specific guides.

Watch Video