
- Understand the Terraform CLI workflow from initialization to destruction.
- Learn the purpose and typical usage of core commands:
init,validate,plan,apply,destroy, and state management commands. - Learn best practices for safely reviewing and applying changes, managing remote state, and working with modules and workspaces.
Tip: Treat the CLI as your single source of truth for executing infrastructure changes. Combine careful use of
terraform plan with remote state locking to avoid race conditions in team environments.| Command | Purpose | Typical example |
|---|---|---|
terraform init | Initialize a working directory, download providers and modules, and configure the backend. | terraform init |
terraform validate | Validate configuration syntax and internal consistency. | terraform validate |
terraform fmt | Reformat configuration files to canonical style. | terraform fmt |
terraform plan | Create an execution plan showing proposed changes without applying them. | terraform plan -out=plan.tfplan |
terraform apply | Apply the changes required to reach the desired state. Accepts a saved plan: terraform apply plan.tfplan. | terraform apply |
terraform destroy | Destroy all resources defined in the configuration. | terraform destroy |
terraform state | Inspect and modify the Terraform state. Useful for advanced state corrections. | terraform state list |
terraform output | Read outputs from the state. | terraform output -json |
terraform import | Import existing resources into state. | terraform import aws_instance.example i-1234567890abcdef0 |
terraform taint / untaint | Mark/unmark resources for recreation on next apply (legacy; prefer terraform apply -replace). | terraform taint aws_instance.example |
Warning: Avoid editing the Terraform state file directly. Use
terraform state subcommands or remote state features (backends) to prevent corruption and ensure team safety.terraform init— set up the directory and backend.terraform validateandterraform fmt— ensure configuration quality.terraform plan -out=plan.tfplan— generate a reviewable plan.- Review the plan carefully, then
terraform apply plan.tfplan— apply changes. - Use
terraform outputto retrieve values andterraform stateto inspect state as needed. - When decommissioning resources, use
terraform destroywith caution.
- Terraform Documentation: https://www.terraform.io/docs
- CLI Commands Reference: https://www.terraform.io/cli
- State Management: https://www.terraform.io/language/state