AWS · explainer · checked November 3, 2026
CloudFormation vs Terraform vs CDK: how to define AWS infrastructure as code
AWS CloudFormation and Terraform both turn a text file into real infrastructure. CloudFormation is an AWS service that tracks each stack for you and only manages AWS. Terraform is a HashiCorp tool that keeps its own state file and manages any platform with a provider. The AWS Cloud Development Kit (AWS CDK) writes CloudFormation templates from code.
Side by side
| AWS CloudFormation | AWS CDK | Terraform | |
|---|---|---|---|
| What you write | A template (JSON or YAML) describing resources | Code in TypeScript, JavaScript, Python, Java, C#/.NET or Go | Configuration files in the Terraform language, which is built on HCL |
| What does the work | The CloudFormation service in your AWS account | CloudFormation: the CDK produces a template and deploys it through CloudFormation | The Terraform CLI, calling each platform’s API through providers |
| Where the record of what exists lives | The stack, held by AWS | The CloudFormation stack, held by AWS | A state file you store and protect, such as in a remote backend |
| Preview before changing | Change sets | The CloudFormation change set | terraform plan |
| Which platforms | AWS resources | AWS resources | Any platform that has a provider, including AWS, Azure and Google Cloud |
| Several accounts and Regions | StackSets | StackSets through CloudFormation | Each provider has its own configuration, and aliases let one configuration hold several settings for the same provider, such as different accounts or Regions |
What each one is
AWS CloudFormation is a service that models and sets up your AWS resources. You write a template that describes the resources you want, such as Amazon EC2 instances or Amazon RDS databases, and CloudFormation provisions and configures them. The resources created from one template are managed as a single unit called a stack. Deleting the stack deletes the resources in it.
The AWS Cloud Development Kit (AWS CDK) is an open-source framework for defining cloud infrastructure in code and provisioning it through CloudFormation. You write in TypeScript, JavaScript, Python, Java, C#/.NET or Go, and you can use loops, conditionals and functions. A CDK app is synthesized into CloudFormation templates, and those templates are deployed as stacks. So CDK is not a rival to CloudFormation. It is another way to write what CloudFormation runs.
Terraform is an infrastructure as code tool from HashiCorp. You describe resources in configuration files, and Terraform creates them by calling the APIs of the platforms involved. It works through plugins called providers, and each provider adds the resource types for one platform or service. Providers cover cloud platforms, SaaS products and other APIs, not only AWS.
How a change reaches your infrastructure
With CloudFormation you can create a change set before you update a stack. A change set compares your proposed template or parameter values with the running stack and lists which resources will be added, modified or deleted, without changing anything. CloudFormation updates the stack only when you execute the change set. AWS notes that a change set does not guarantee the update will succeed, because some failures only appear at run time.
Terraform has the same idea built into its core workflow: write, plan, apply. terraform plan reads the current state of the existing remote objects, compares your configuration with it, and proposes what to change. terraform apply carries the changes out after you approve the plan. It can also take a saved plan file, in which case it performs exactly the operations in that file without prompting.
A CDK project adds a step in front: the code is synthesized into a template, then CloudFormation does the update, so you get the same stack behavior. The cdk deploy command can create a CloudFormation change set for the deployment.
Where the record of your infrastructure lives
This is the difference that most changes day-to-day work.
CloudFormation keeps the record for you. The stack is that record, and AWS runs it. You have nothing to store, back up or lock.
Terraform keeps its record in a state file. State maps the real objects to the resources in your configuration, and Terraform uses it to decide what to change. By default the state is a local file named terraform.tfstate. HashiCorp recommends storing state in HCP Terraform or a remote backend instead, so that a team can share it and one lost laptop does not lose it. Terraform locks the state automatically during any operation that could write it, if the backend supports locking, so two people cannot corrupt it by applying at once. Not every backend supports locking. This is more to set up than CloudFormation needs, and it is the price of Terraform not being tied to one cloud.
Drift, and many accounts
Drift is a change made to a resource outside the tool that created it, such as someone editing an EC2 instance in the console. CloudFormation has drift detection: it compares each resource’s actual properties with what the template expects, and reports the resource and the stack as drifted if any value differs. You can run it on a whole stack or on single resources. Terraform refreshes its state against the real infrastructure before any operation, so a plan shows differences that came from changes made outside Terraform.
CloudFormation StackSets create, update or delete stacks across multiple accounts and AWS Regions with one operation, from an administrator account. That is the CloudFormation answer to a multi-account organization.
When to pick each
- **CloudFormation** fits a team that works only on AWS and wants AWS to hold the record, with no state backend to run. The CDK deploys through it too.
- **CDK** fits developers who want to write infrastructure in the same language as their application, with tests and code review, and still deploy through CloudFormation.
- **Terraform** fits a team that manages more than AWS, such as Azure, Google Cloud, Kubernetes or SaaS services, and wants one workflow and one language for all of it. It is also a reasonable choice for AWS-only teams who already know it, as long as someone looks after the state.
Some teams use more than one. None of these is the wrong answer in general. The better one is the one that matches where your resources live and who runs the tool.
On the exams
How certification exams use this
The AWS exams that cover this ask the candidate to define infrastructure and reusable components as code, using CloudFormation or the CDK, and to automate deployments across accounts and Regions. The DevOps Engineer and Solutions Architect Professional guides name both tools. The Terraform Associate exam asks about the Terraform workflow itself, including providers and state. The usual trap is to answer a “which tool” scenario by habit. Read what the scenario fixes: one cloud or several, who stores the record of what exists, and whether the team wants to write code or a template.
Test yourself
See how well you know it, free.
Each exam’s diagnostic is free, with no sign-in: questions across the whole exam, and a map of what you know and what you don’t.
HashiCorp Terraform Associate
20 free questions, no sign-in.
Take the free diagnosticAbout the examAWS Certified DevOps Engineer – Professional
20 free questions, no sign-in.
Take the free diagnosticAbout the examAWS Certified Developer – Associate
20 free questions, no sign-in.
Take the free diagnosticAbout the examAWS Certified CloudOps Engineer – Associate
20 free questions, no sign-in.
Take the free diagnosticAbout the examAWS Certified Solutions Architect – Professional
20 free questions, no sign-in.
Take the free diagnosticAbout the exam
Sources
Checked against the vendors’ own documentation.
- CloudFormation template reference and tools
- cdk deploy (AWS CDK CLI reference)
- terraform plan command
- terraform apply command
- Terraform configuration syntax
- Terraform provider block reference
- What is CloudFormation?
- Update CloudFormation stacks using change sets
- Detect unmanaged configuration changes with drift detection
- Managing stacks across accounts and Regions with StackSets
- What is the AWS CDK?
- What is Terraform?
- Terraform state
- Terraform state locking
Last checked November 3, 2026. Spotted something wrong or out of date? Tell us.