- Write configuration (create
.tffiles) - 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)
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 asmain.tf, variables.tf, outputs.tf, and providers.tf.
Example minimal configuration:
- 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_providersto 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):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.
-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:
- Use
-outto 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):- 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:7) Manage environments with workspaces (optional)
Workspaces allow multiple states within the same configuration directory (e.g.,dev, prod):
8) Destroy infrastructure
Remove resources managed by a configuration when needed: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+DynamoDBfor locking, orTerraform Cloud/Enterprise) to enable collaboration and locking. - Pin provider versions in
required_providersto prevent unexpected behavior after provider updates. - Use CI pipelines to run
terraform planand require code review beforeterraform apply. - Enforce
terraform fmtandterraform validatein CI or pre-commit hooks. - Avoid storing sensitive secrets in
.tffiles. Use secure variables, secret management systems, or encrypted storage. - Use
-outplans in automation to separate plan and apply phases and to guarantee the exact plan is applied.
Links and references
- Terraform language docs: https://developer.hashicorp.com/terraform/language
- Terraform CLI docs: https://developer.hashicorp.com/terraform/cli
- Terraform Cloud: https://www.terraform.io/cloud
- AWS S3: https://aws.amazon.com/s3/
- AWS DynamoDB: https://aws.amazon.com/dynamodb/