How to convert a CloudFormation YAML template to Terraform
- Add your codePaste it into the CloudFormation (YAML) editor, drag in a file, or keep the sample to see how it works.
- Convert to TerraformClick 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.
From YAML tags to HCL expressions
YAML templates usually use the short form of intrinsic functions: !Ref, !GetAtt SessionsTable.Arn and !Sub "sessions-${Stage}". The converter rewrites them as Terraform references and interpolation, such as aws_dynamodb_table.sessions.arn and "sessions-${var.stage}". Parameters become variables, and AllowedValues can carry over as a validation block.
Resource properties map to provider arguments, with some structural changes. A DynamoDB TimeToLiveSpecification becomes a ttl block, KeySchema and AttributeDefinitions become hash_key plus attribute blocks, and an IAM role's inline Policies often become separate aws_iam_role_policy resources.
The converted code creates new resources unless you import the existing ones. Retain them in the stack, then adopt them with Terraform import blocks.
Before deleting the old stack, set DeletionPolicy: Retain on its resources and update the stack. Deleting a stack without it removes the resources too.
Tips for better results
- Paste valid CloudFormation YAML. Short-form tags like
!Refare fine. Tabs for indentation aren't valid YAML and confuse the conversion. - Expand SAM first. For
Transform: AWS::Serverless-2016-10-31templates, download the processed template withaws cloudformation get-template --template-stage Processedand convert that. - Check ARN construction.
!Sub arn:aws:...strings often have a direct Terraform attribute, such as.arn, which is cleaner than rebuilding the string. - Convert conditions with care.
Conditionsand!Ifusually becomecountor conditional expressions. Test each branch with different variable values. - Run a plan after import.
terraform planshould show no changes for resources you imported.
Frequently asked questions
What happens to !Ref, !Sub and !GetAtt?
They become Terraform expressions. !GetAtt Table.Arn becomes aws_dynamodb_table.table.arn, !Sub becomes string interpolation, and !Ref becomes whichever attribute that resource type returns for Ref, such as an ID, name or ARN.
Can I move deployed resources without recreating them?
Yes. Retain the resources in the CloudFormation stack, delete the stack, then import each resource into Terraform with import blocks. Check that terraform plan shows no changes afterwards.
What about SAM and Serverless templates?
SAM resources such as AWS::Serverless::Function expand into several CloudFormation resources at deploy time. Convert the processed template to see the real resources you need in Terraform.
Is there a JSON version of this converter?
Yes. For JSON templates, use the CloudFormation JSON to Terraform converter.