CloudFormation vs Terraform
What Are They Used For?
Both AWS CloudFormation and Terraform are Infrastructure as Code (IaC) tools used to automate the provisioning and management of cloud resources. They help define infrastructure using configuration files rather than manual deployment.
AWS CloudFormation
- A native AWS service that helps define and manage AWS infrastructure using JSON or YAML templates.
- Automatically provisions, updates, and deletes AWS resources as defined in a CloudFormation template.
- Fully integrated with AWS but limited to AWS resources.
Terraform
- An open-source IaC tool developed by HashiCorp that supports multi-cloud and on-premise infrastructure.
- Uses HashiCorp Configuration Language (HCL) to define infrastructure.
- Works with multiple cloud providers like AWS, Azure, and Google Cloud.
Key Differences
| Feature | CloudFormation | Terraform |
|---|---|---|
| Cloud Support | AWS only | Multi-cloud (AWS, Azure, GCP, etc.) |
| Language | JSON/YAML | HCL (HashiCorp Configuration Language) |
| State Management | No external state (AWS manages state) | Uses a state file to track changes |
| Modularity | Less flexible | Highly modular (supports reusable modules) |
| Custom Plugins | Limited to AWS | Supports custom providers and plugins |
| Execution Speed | Slower (depends on AWS stack updates) | Faster (parallel execution of resources) |
| Rollback Mechanism | Built-in rollback for failed deployments | Requires manual intervention |
| Community & Extensibility | AWS-focused | Large community with extensive provider support |
When to Use Each
- Use CloudFormation if you are fully invested in AWS and want a managed solution with deep AWS integration.
- Use Terraform if you need a multi-cloud strategy, better modularization, and custom provider support.
Conclusion: Terraform provides more flexibility and multi-cloud support, while CloudFormation is ideal for AWS-native deployments with built-in rollback capabilities.
Interview angle
- “Terraform or CloudFormation?” - Terraform for multi-cloud, a far larger provider ecosystem, and a better module story. CloudFormation for deep AWS-native integration, no state file to manage, and native rollback. Most teams pick Terraform unless they’re strictly AWS and value managed state.
- “What is the state file and why is it a problem?” - Terraform’s record of the real-world mapping. It must be shared (remote backend), locked to prevent concurrent applies, and treated as sensitive because it contains secrets in plaintext. See terraform/06_opentofu_and_licensing.md.
- “How do you review infrastructure changes?” -
planin CI on the pull request so reviewers see exactly what will change, with apply gated on approval. The destructive operations in a plan are what review is for.