Terraform is the most widely used Infrastructure as Code (IaC) tool in the world. It allows you to define, provision, and manage cloud infrastructure using declarative configuration files written in HCL (HashiCorp Configuration Language). Whether you use AWS, Azure, GCP, or any combination, Terraform provides a consistent workflow for managing it all.
Free Terraform Cheat Sheet
All essential commands, HCL syntax, resource examples, and Terraform vs alternatives comparison.
Download Free PDFTable of Contents
- What is Terraform?
- Core Concepts
- The Terraform Workflow
- Essential Commands
- HCL Configuration
- State Management
- Modules
- Workspaces
- Terraform vs Alternatives
- Best Practices
What is Terraform?
Terraform, created by HashiCorp, treats infrastructure as code. Instead of clicking through AWS Console or Azure Portal, you write configuration files that describe your desired infrastructure state. Terraform then calculates what changes are needed and applies them.
Key advantages:
- Multi-cloud β Single tool for AWS, Azure, GCP, Kubernetes, and 3000+ providers
- Declarative β Describe what you want, Terraform figures out how
- Plan before apply β Preview all changes before they happen
- State tracking β Knows current infrastructure state, only changes what is needed
- Version controlled β Infrastructure definitions in Git = history, review, rollback
- Modular β Reusable modules for common patterns
Core Concepts
| Concept | Description | Example |
|---|---|---|
| Provider | Plugin that connects to a cloud/service | aws, azurerm, google, kubernetes |
| Resource | Infrastructure component to manage | aws_instance, aws_s3_bucket |
| Data Source | Read-only query for existing resources | data.aws_ami, data.aws_vpc |
| Variable | Input parameter for configuration | var.region, var.instance_type |
| Output | Exported value after apply | output.instance_ip |
| Module | Reusable collection of resources | module.vpc, module.eks |
| State | Current infrastructure state (JSON) | terraform.tfstate |
| Backend | Where state is stored | S3, Azure Blob, Terraform Cloud |
The Terraform Workflow
- Write β Define infrastructure in .tf files
- Init β
terraform initdownloads providers and initializes backend - Plan β
terraform planshows what will change (add, modify, destroy) - Apply β
terraform applymakes the changes - Destroy β
terraform destroytears down all managed resources
Essential Commands
| Command | Description |
|---|---|
terraform init | Initialize working directory, download providers |
terraform plan | Preview changes without applying |
terraform apply | Apply changes to infrastructure |
terraform destroy | Destroy all managed resources |
terraform fmt | Format .tf files to canonical style |
terraform validate | Validate configuration syntax |
terraform state list | List all resources in state |
terraform import | Import existing resource into state |
terraform output | Show output values |
terraform workspace | Manage workspaces (dev/staging/prod) |
HCL Configuration
Terraform uses HCL (HashiCorp Configuration Language), a declarative language designed for infrastructure. Key elements:
- Providers β Configure which cloud services to use
- Resources β Define infrastructure components
- Variables β Parameterize configurations
- Locals β Computed values for DRY code
- Outputs β Export values for other modules or display
- for_each / count β Create multiple similar resources
- dynamic blocks β Generate repeated nested blocks
State Management
Terraform state is a JSON file that maps your configuration to real-world resources. It is the most critical part of Terraform β if you lose state, Terraform cannot manage existing resources.
Remote State Backends
| Backend | Best For | Locking |
|---|---|---|
| S3 + DynamoDB | AWS teams | Yes (DynamoDB) |
| Azure Blob | Azure teams | Yes (native) |
| GCS | GCP teams | Yes (native) |
| Terraform Cloud | Teams, CI/CD | Yes (managed) |
Modules
Modules are reusable packages of Terraform configuration. The Terraform Registry has thousands of community modules for common infrastructure patterns.
Workspaces
Workspaces let you manage multiple environments (dev, staging, prod) from the same configuration with separate state files.
Terraform vs Alternatives
| Feature | Terraform | OpenTofu | Pulumi | CloudFormation |
|---|---|---|---|---|
| Language | HCL | HCL (compatible) | Python/TS/Go/Java | YAML/JSON |
| Multi-cloud | Yes (3000+ providers) | Yes (same providers) | Yes | AWS only |
| License | BSL 1.1 | MPL 2.0 (open source) | Apache 2.0 | Proprietary |
| Community | Largest | Growing fast | Growing | AWS ecosystem |
| Best for | Most teams | Open-source focused | Devs who prefer code | AWS-only shops |
Best Practices
- Use remote state β S3/Azure Blob with locking. Never local state in production
- Version pin providers β Use
version = "~> 5.0"to prevent breaking changes - Use modules β DRY principle. Create modules for repeated infrastructure patterns
- Plan before apply β Always review
terraform planoutput - Use workspaces or directories β Separate environments (dev/staging/prod)
- Format code β Run
terraform fmtbefore committing - Use variables β Never hardcode values. Use .tfvars for environment-specific values
- Enable state locking β Prevent concurrent modifications
- Tag everything β Tags enable cost tracking, ownership, and automation
- Use CI/CD β Automate plan/apply with GitHub Actions or GitLab CI
Download the Terraform Cheat Sheet
All commands, HCL syntax, state management, and alternatives comparison in a printable PDF.
Download Free PDF