Terraform is an infrastructure-as-code tool for building, changing, and versioning cloud and on-premises resources safely and efficiently.
Core concepts
- Providers — plugins that interact with cloud APIs (AWS, Azure, GCP, Kubernetes, etc.).
- Resources — the infrastructure objects managed by Terraform (e.g.,
aws_instance, kubernetes_deployment).
- State — a snapshot of managed resources stored in
terraform.tfstate.
- Modules — reusable, composable packages of Terraform configuration.
- Workspaces — isolated state environments within the same configuration.
Common CLI commands
| Command | Purpose |
|---|
terraform init | Initialize the working directory and download providers. |
terraform plan | Preview changes before applying. |
terraform apply | Apply the planned changes. |
terraform destroy | Remove all resources defined in the configuration. |
terraform validate | Check configuration syntax and consistency. |
terraform fmt | Format configuration files to canonical style. |
State operations
| Operation | Command |
|---|
| Show current state | terraform show |
| List resources in state | terraform state list |
| Move a resource in state | terraform state mv <source> <destination> |
| Remove a resource from state | terraform state rm <address> |
| Pull remote state | terraform state pull |
Workspace commands
terraform workspace list — show available workspaces.
terraform workspace new <name> — create a new workspace.
terraform workspace select <name> — switch to an existing workspace.
terraform workspace delete <name> — remove a workspace.
Best practices
- Store remote state in a shared backend with locking (S3, DynamoDB, Terraform Cloud, etc.).
- Keep modules small, focused, and versioned.
- Run
terraform plan in CI before every apply.
- Use variable validation and output descriptions.
- Test infrastructure code with Terratest and OPA.
In-depth reference
- Getting Started — your first Terraform deployment
- Why Terraform — IaC philosophy and Terraform's declarative model
- Configuration Syntax — HCL deep-dive: resources, variables, outputs, data sources
- Managing State — remote backends, state locking, workspace isolation
- Modules — building reusable, composable infrastructure
- Loops, Conditionals & Deployments —
count, for_each, for expressions, zero-downtime
- Secret Management — KMS, Secrets Manager, IAM roles, OIDC
- Multiple Providers — multi-region, multi-account, AWS + Kubernetes
- Production-Grade Infrastructure — small modules, preconditions, validation
- Testing Terraform Code — Terratest, OPA, unit and integration tests
- Team Collaboration — Terragrunt, CI/CD pipelines, code review workflows