How to convert Terraform to a CloudFormation template
- Add your codePaste it into the Terraform (HCL) editor, drag in a file, or keep the sample to see how it works.
- Convert to CloudFormationClick the button or press Ctrl + Enter. The result streams into the right-hand editor.
- Review and use itCopy or download the result, then test it before you rely on it. AI output is a strong first draft, not a guarantee.
How Terraform concepts map to CloudFormation
Most of the mapping is direct. Each resource "aws_vpc" becomes an AWS::EC2::VPC entry under Resources, variable blocks become Parameters, and output blocks become Outputs. References such as aws_vpc.main.id turn into intrinsic functions like {"Ref": "MainVpc"} or Fn::GetAtt, and string interpolation becomes Fn::Sub.
Some Terraform features have no plain CloudFormation equivalent. count and for_each usually become repeated resources, unless you opt in to the AWS::LanguageExtensions transform and its Fn::ForEach. Data sources that look things up, such as data "aws_ami", need a parameter or an SSM parameter type instead.
Before you create a stack from the template, validate it with aws cloudformation validate-template and review a change set so you can see exactly what CloudFormation will create.
The template describes new resources. It doesn't adopt the ones Terraform already manages. To move existing resources, use CloudFormation resource import, then remove them from Terraform state with terraform state rm.
Tips for better results
- Paste variables and outputs too. They become the template's
ParametersandOutputs, so the stack stays configurable. - Replace provider-level settings. The
provider "aws"region isn't part of a template. You choose the region when you create the stack. - Check logical IDs. Terraform names like
private_abecome CloudFormation logical IDs such asPrivateA. Pick names you're happy to keep, because renaming one later replaces the resource. - Watch for fixed names. A hard-coded name, such as a security group's
GroupName, stops CloudFormation from replacing that resource during an update. Leave names out unless you need them. - Prefer YAML to read, JSON to generate. CloudFormation accepts both formats, so convert the JSON to YAML afterwards if your team prefers it.
Frequently asked questions
Can CloudFormation take over resources created by Terraform?
Yes, with resource import. Create a stack or change set that imports each existing resource by its identifier, as described in the AWS import guide, then run terraform state rm so Terraform stops managing them.
What happens to Terraform count and for_each?
CloudFormation has no loops by default, so the converter usually writes each instance as its own resource. For large loops, consider the AWS::LanguageExtensions transform, which adds Fn::ForEach.
Why is the output JSON rather than YAML?
This tool produces JSON templates, which CloudFormation accepts just like YAML. If you prefer YAML, convert the JSON with a tool such as AWS Labs' cfn-flip.
How do I check the template before deploying?
Run aws cloudformation validate-template --template-body file://template.json for syntax errors, then create a change set to preview every resource CloudFormation will add or change.