Terraform Tutorial — FAQ
Answers to the most common Terraform questions.
1. Is Terraform free?
Yes. Terraform CLI is open-source and free. Terraform Cloud has a free tier for small teams. Terraform Enterprise is a paid, self-hosted product.
2. Terraform vs CloudFormation?
| Factor | Terraform | CloudFormation |
|---|---|---|
| Multi-cloud | ✅ | ❌ (AWS only) |
| Language | HCL | JSON/YAML |
| State | Explicit file | Managed by AWS |
| Rollback | Manual | Automatic |
| Ecosystem | 3,000+ providers | AWS only |
Choose CloudFormation if you’re AWS-only and want managed state. Choose Terraform for multi-cloud or multi-provider workflows.
3. Terraform vs Ansible?
Terraform is for provisioning infrastructure (create servers, networks, databases). Ansible is for configuring those resources (install software, manage files, run commands). They complement each other — use Terraform to create the EC2 instance, Ansible to configure it.
4. What happens if I lose my state file?
Terraform won’t know what resources it manages. You’ll need to:
- Import each resource manually with
terraform import - Or recreate the infrastructure
Prevention: Always use a remote backend (S3 + DynamoDB) with versioning enabled.
5. Can I use Terraform with existing infrastructure?
Yes. Use terraform import to bring existing resources under Terraform management. You’ll need to write the corresponding HCL configuration to match the imported resource’s settings.
6. How do I handle secrets in Terraform?
| Approach | When to Use |
|---|---|
sensitive = true on variables |
Hide from CLI output |
Environment variables (TF_VAR_*) |
Keep out of files |
| HashiCorp Vault | Dynamic secrets |
| AWS Secrets Manager / SSM | Cloud-native secrets |
.gitignore the .tfvars file |
Prevent accidental commits |
Never commit secrets to version control.
7. How do I manage multiple environments?
Three common patterns:
- Workspaces — same code, different state per environment
- Directory-based — separate directories for dev/staging/prod
- Terragrunt — DRY wrapper around Terraform for multiple environments
8. What is state locking?
State locking prevents two users from running terraform apply at the same time, which could corrupt the state. With an S3 backend, DynamoDB handles the lock. Terraform Cloud handles it automatically.
9. How often should I run terraform plan?
Run terraform plan frequently:
- Before every
apply - In CI/CD on every pull request
- Periodically to detect state drift (manual changes outside Terraform)
10. What’s the difference between count and for_each?
| Feature | count |
for_each |
|---|---|---|
| Input | Number | Map or set |
| Index | count.index (number) |
each.key / each.value |
| Removing items | Shifts all higher indexes | Only removes the specific item |
| Best for | Identical resources | Resources with unique keys |
Prefer for_each — it’s safer when adding or removing items because it uses keys, not positions.
Still Have Questions?
- Check the Terraform Documentation
- Ask on Stack Overflow (tag:
terraform) - Join the HashiCorp Discuss Forum
Congratulations on completing the Terraform Basics Tutorial! 🎉
You now have a solid foundation in infrastructure as code with Terraform. Continue to the Terraform Advanced Tutorial to take your skills further.