Both define infrastructure as code, but Terraform uses HCL (HashiCorp Configuration Language) which is more readable, while CloudFormation uses JSON or YAML templates familiar to many AWS users.
Resource
Resource
Both define infrastructure components like EC2 instances, S3 buckets, etc., as resources in their respective configs.
State file (terraform.tfstate)
Managed state internally by CloudFormation
Terraform requires explicit state management via a state file that tracks resource states; CloudFormation manages state automatically within AWS.
Workspace (previously called Environment)
Stack
CloudFormation has the concept of a stack which is an instantiation of a template and can be created multiple times; Terraform has no direct stack concept but uses workspaces to separate state for different environments, though it requires manual switching.
Module
Nested Stack / Macros
Terraform modules are reusable, composable units of infrastructure code; CloudFormation supports nested stacks and macros for modularity, but Terraform’s modules are generally considered more flexible and easier to reuse.
Provider
AWS Service Integration
Terraform uses providers to interact with different cloud platforms (AWS, Azure, GCP, etc.); CloudFormation is AWS-specific and integrates natively with AWS services.
Plan (terraform plan)
Change Set
Terraform’s plan command shows all changes before applying; CloudFormation’s Change Sets provide a preview of changes before deployment, but Terraform’s plan is often regarded as more detailed and transparent.
Apply (terraform apply)
Stack Update / Create
Both apply the defined changes to provision or update infrastructure.
Destroy (terraform destroy)
Stack Delete
Both can delete all resources managed by the configuration/stack.
Interpolation / Expressions
Intrinsic Functions (e.g., Fn::Join, Ref)
Both support expressions to reference resources and outputs dynamically, though Terraform’s HCL syntax is more expressive and user-friendly.
Outputs
Outputs
Both allow defining outputs from the infrastructure for use elsewhere.
Provisioners
Custom Resources / Lambda-backed Custom Resources
Terraform provisioners run scripts or commands on resources; CloudFormation uses custom resources backed by Lambda functions for similar extensibility.
State Locking
Stack Locking (via AWS mechanisms)
Terraform supports state locking to prevent concurrent changes; CloudFormation inherently prevents concurrent stack updates.
Multi-cloud support
AWS-only
Terraform supports multiple cloud providers via providers; CloudFormation is AWS-specific.