What is the difference between Terraform and CloudFormation?
Compare Terraform and CloudFormation: multi-cloud vs AWS-only, HCL vs YAML, state files vs stacks, plan vs change sets, with examples and interview answers.
Expected Interview Answer
Terraform is a cloud-agnostic infrastructure-as-code tool from HashiCorp that uses the HCL language and works across AWS, Azure, GCP and hundreds of providers, while CloudFormation is AWS's native IaC service that provisions only AWS resources using JSON or YAML templates.
The biggest practical differences are portability and state management. Terraform keeps its own state file (locally or in a remote backend) and can manage multi-cloud and third-party resources through providers, whereas CloudFormation stores state as managed stacks inside AWS and is limited to the AWS ecosystem. Terraform's HCL adds loops, conditionals, modules and functions natively, while CloudFormation relies on intrinsic functions, macros and nested stacks. CloudFormation offers automatic rollback and drift detection tightly integrated with AWS, whereas Terraform gives a rich plan/apply preview and a broader provider marketplace.
- Terraform is cloud-agnostic and reusable across providers
- CloudFormation is deeply integrated with AWS services and IAM
- Terraform's plan output previews changes before applying
- CloudFormation manages state and rollback for you as stacks
- Terraform modules and HCL enable expressive, reusable code
AI Mentor Explanation
Terraform is like an international coaching manual that works for cricket, hockey and football alike, so one playbook adapts to any ground you tour. CloudFormation is like a manual written only for your home cricket stadium — brilliant there because it knows every pitch and boundary rope, but useless the moment you travel to a football field. One is portable across games; the other is expert in a single ground.
Step-by-Step Explanation
Step 1
Compare scope
Terraform manages many clouds and SaaS providers; CloudFormation manages only AWS resources.
Step 2
Compare language
Terraform uses HCL with loops and functions; CloudFormation uses JSON/YAML with intrinsic functions and macros.
Step 3
Compare state
Terraform stores an explicit state file in a backend; CloudFormation keeps state internally as managed stacks.
Step 4
Compare change preview
Terraform runs plan/apply; CloudFormation uses change sets to preview stack updates.
Step 5
Compare ecosystem
Terraform has a broad provider/module registry; CloudFormation has deep native AWS integration and rollback.
What Interviewer Expects
- Clear grasp that Terraform is multi-cloud and CloudFormation is AWS-only
- Understanding of Terraform state files vs CloudFormation managed stacks
- Awareness of HCL vs JSON/YAML expressiveness
- Knowledge of plan/apply vs change sets
- Ability to recommend one based on the team's context
Common Mistakes
- Claiming CloudFormation can provision non-AWS resources
- Saying Terraform has no state because it is declarative
- Ignoring remote state and locking when comparing the two
- Assuming HCL and YAML are functionally identical
Best Answer (HR Friendly)
“Terraform and CloudFormation both let teams define infrastructure as code, but Terraform works across many cloud providers while CloudFormation only manages AWS. Terraform tracks its own state file and uses a plan step to preview changes, whereas CloudFormation handles state for you as AWS stacks.”
Code Example
# Terraform - portable across providers
resource "aws_s3_bucket" "logs" {
bucket = "my-app-logs"
}
# CloudFormation (YAML) equivalent
# Resources:
# LogsBucket:
# Type: AWS::S3::Bucket
# Properties:
# BucketName: my-app-logsFollow-up Questions
- How does Terraform state differ from a CloudFormation stack?
- When would you still choose CloudFormation over Terraform?
- What is a Terraform provider and how does it enable multi-cloud?
- How does drift detection work in each tool?
- Can Terraform and CloudFormation be used together in one AWS account?
MCQ Practice
1. Which statement about Terraform and CloudFormation is correct?
Terraform uses providers to manage many clouds, while CloudFormation is limited to AWS resources.
2. Where does Terraform keep the record of managed resources?
Terraform records real infrastructure in a state file stored locally or in a remote backend.
3. Which language does Terraform primarily use?
Terraform configurations are written in HashiCorp Configuration Language (HCL), though JSON is also supported.
Flash Cards
Terraform scope? — Cloud-agnostic; manages AWS, Azure, GCP and hundreds of providers.
CloudFormation scope? — AWS-native; provisions only AWS resources as managed stacks.
How does Terraform track state? — In an explicit state file stored locally or in a remote backend with locking.
Preview mechanism? — Terraform uses plan/apply; CloudFormation uses change sets.