Planfile

Terraform vs CloudFormation: differences, examples and when to choose each

Updated

AWS CloudFormation is AWS's own infrastructure-as-code service: you write JSON or YAML templates and AWS deploys them as stacks, keeping the state for you. Terraform is HashiCorp's tool: you write HCL, it works with AWS and hundreds of other providers, and it records what it manages in a state file you store. Choose CloudFormation for AWS-only teams that want AWS to run everything; choose Terraform for multi-cloud work or one workflow across many services.

A Terraform plan in Planfile replacing an S3 bucket because the bucket name changed, marked forces replacement
Terraform's preview: renaming a bucket forces replacement. A CloudFormation change set reports the same with Replacement: True.

The main difference: AWS-native service vs multi-provider tool

CloudFormation is a managed AWS service. You hand it a template; AWS works out the order, creates the resources, tracks them as a stack, and rolls back if a deployment fails. There is no state file to look after and nothing to install beyond the AWS CLI.

Terraform is a command-line tool that runs wherever you run it (a laptop, CI, or HCP Terraform). It reaches AWS through the AWS provider, and the same workflow covers Azure, Google Cloud, Kubernetes, GitHub, Cloudflare, Datadog and many other services through their own providers. In exchange, you manage the state: usually an S3 backend with locking.

Everything else follows from that split: who stores state, how previews work, how fast new AWS features arrive, and what it costs.

Terraform vs CloudFormation side by side

TerraformAWS CloudFormation
Made byHashiCorp (an IBM company)AWS
CloudsAWS, Azure, Google Cloud and hundreds of other providersAWS (plus third-party types published in the CloudFormation registry)
LanguageHCLJSON or YAML templates; CDK generates them from code
StateState file you store (S3, HCP Terraform, others)Managed by AWS as a stack
Previewterraform planChange sets
Failed deploymentStops; what succeeded stays, fix and re-applyRolls back the stack by default
DriftEvery plan refreshes and shows driftDrift detection on demand; drift-aware change sets
ReuseModules (local, Git, Terraform Registry)Nested stacks, CloudFormation modules, CDK constructs
Cost of the toolCLI free; HCP Terraform has free and paid tiersNo charge for AWS resource types; registry extensions billed per handler operation
LicenceBUSL 1.1 (CLI); AWS provider MPL 2.0Managed service

HCL vs JSON/YAML templates (and the CDK)

CloudFormation templates are JSON or YAML with a fixed structure: Parameters, Resources, Outputs and so on, plus intrinsic functions such as !Ref, !GetAtt, !Sub and !If. Each resource has a type like AWS::S3::Bucket.

Terraform's HCL was designed for configuration and reads more like a small language: variables, locals, expressions, for_each and count for repetition, and a library of built-in functions. Templates that need loops are where most people feel the difference; CloudFormation's answer has been macros such as AWS::LanguageExtensions and, more often, the CDK.

The AWS Cloud Development Kit (CDK) lets you define infrastructure in TypeScript, JavaScript, Python, Java, C#/.NET or Go. It synthesises CloudFormation templates and deploys them through CloudFormation, so you keep stacks and rollback while writing ordinary code. If you're comparing Terraform with the CDK, you're comparing HCL with a general-purpose language on top of CloudFormation; the trade-offs are close to those in Terraform vs Pulumi.

The same S3 bucket in both

CloudFormation (YAML)

AWSTemplateFormatVersion: "2010-09-09"
Description: Logs bucket

Resources:
  LogsBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: example-logs-2026
      Tags:
        - Key: env
          Value: dev

Outputs:
  BucketArn:
    Value: !GetAtt LogsBucket.Arn
aws cloudformation deploy \
  --template-file bucket.yaml \
  --stack-name logs

Terraform (HCL)

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
  }
}

provider "aws" {
  region = "eu-west-2"
}

resource "aws_s3_bucket" "logs" {
  bucket = "example-logs-2026"

  tags = {
    env = "dev"
  }
}

output "bucket_arn" {
  value = aws_s3_bucket.logs.arn
}
terraform init
terraform plan
terraform apply

The resources are equivalent. The differences are around them: CloudFormation takes the region from your AWS CLI configuration and stores state in the stack; Terraform names the provider and region in code and writes terraform.tfstate (move it to a remote backend for team use).

State file vs stacks

In CloudFormation the stack is the state. AWS records which physical resources belong to it, you see them in the console under the stack's Resources tab, and two people can't update the same stack at once. Deleting the stack deletes its resources (unless a resource has a DeletionPolicy of Retain).

In Terraform, the state file maps each resource address to a real object. You choose where it lives and must protect it: it can contain secrets, it needs locking (the S3 backend's native locking, use_lockfile = true, is generally available since Terraform 1.11), and it should never be edited by hand. Commands such as terraform state list, state mv and moved blocks let you reorganise it.

The managed model is simpler to run. The state-file model is more flexible: you can split, move and import resources between configurations, and it works the same whichever cloud a resource is in.

Change sets vs terraform plan

Both tools preview changes before making them.

A CloudFormation change set compares a new template or parameters with the stack and lists the resources it will add, modify or remove, with a Replacement field of True, False or Conditional for each modification. You review it and then execute it:

aws cloudformation create-change-set \
  --stack-name logs \
  --change-set-name rename-bucket \
  --template-body file://bucket.yaml

aws cloudformation describe-change-set \
  --stack-name logs \
  --change-set-name rename-bucket

aws cloudformation execute-change-set \
  --stack-name logs \
  --change-set-name rename-bucket

A terraform plan does the same job with symbols (+, ~, -, -/+) and an attribute-level diff, and terraform plan -out=tfplan saves it so terraform apply tfplan applies exactly what you reviewed. Changing the bucket name is a replacement in both: # forces replacement in the plan, Replacement: True in the change set.

Drift detection in both

Drift is a change made outside the tool, such as a tag edited in the console.

Terraform checks for drift on every plan: it refreshes state from the provider and shows Objects have changed outside of Terraform, then plans to put things back. terraform plan -refresh-only shows only the drift. See Terraform drift.

CloudFormation runs drift detection when you ask for it, on a stack or a single resource, and reports each resource as IN_SYNC, MODIFIED, DELETED or NOT_CHECKED (not every resource type supports it, and only explicitly set properties are compared). Traditional change sets compare templates and don't account for drift; drift-aware change sets (--deployment-mode REVERT_DRIFT) compare the new template with the live resources and bring drifted ones back in line.

New AWS features and coverage

CloudFormation is AWS's own service, and many AWS launches include CloudFormation support on day one, though not all; some resource types and properties arrive later. CloudFormation resource types also power the AWS Cloud Control API, a uniform create, read, update, delete and list API for AWS resources.

Terraform's main AWS provider (hashicorp/aws) is written by HashiCorp and the community. It usually adds new services and arguments quickly, but not always on launch day. HashiCorp also publishes the hashicorp/awscc provider, generated from CloudFormation resource schemas and built on Cloud Control, which picks up new resource types as Cloud Control supports them. You can use both providers in the same configuration.

Modules vs nested stacks

Terraform modules are folders of .tf files with inputs and outputs. You call them from local paths, Git, or the public Terraform Registry, which has a large catalogue of community modules for AWS.

CloudFormation offers nested stacks (a stack created by an AWS::CloudFormation::Stack resource in another template), modules registered in the CloudFormation registry and used like resource types, and cross-stack references via exported outputs. CDK users mostly reuse constructs instead. StackSets deploy one template across multiple accounts and regions.

Cost

As of November 2026: CloudFormation has no additional charge for resource types in the AWS::* and Alexa::* namespaces; you pay only for the resources. Registry extensions such as third-party resource types are billed per handler operation. The Terraform CLI is free to download and use. HCP Terraform, HashiCorp's hosted service for remote runs, state and policy, has a free tier and paid tiers; check HashiCorp's pricing page for current terms. In both cases the cloud bill is the same: the bucket costs what it costs.

When to choose Terraform or CloudFormation

New to Terraform? Start with what Terraform is and the Terraform tutorial.

Practise the Terraform workflow

Planfile teaches Terraform on your iPhone with a simulated AWS provider, so you can create, rename and replace S3 buckets and read the plans without an AWS account. The first two chapters, including Providers and resources, are free. It covers Terraform only, not CloudFormation.

Questions

Is Terraform better than CloudFormation?

Not in every case. Terraform is better for multi-cloud and multi-service work; CloudFormation is simpler to run for AWS-only teams because AWS manages state and rollback.

Does CloudFormation have a state file?

No file you manage. AWS tracks the resources in each stack, which plays the role of Terraform's state file.

What is the CloudFormation equivalent of terraform plan?

A change set. It lists the resources a template update would add, modify or remove, including whether each modification needs a replacement, before you execute it.

Can Terraform manage CloudFormation stacks?

Yes. The AWS provider has an aws_cloudformation_stack resource to deploy a stack and a data source to read an existing stack's outputs.

Is AWS CloudFormation free?

There is no additional charge for AWS resource types; you pay for the resources created. Third-party registry extensions are billed per handler operation.

Should I learn Terraform or CloudFormation first?

If you only work on AWS, either works. Terraform's skills carry to other clouds and it has a vendor certification, the HashiCorp Terraform Associate.