简体   繁体   中英

Convert AWS CloudFormation to Terraform template

I want to convert the existing AWS CloudFormation templates (yaml) to Terraform templates(hcl) automatically.

Can anyone please suggest some tools, approaches, etc?

From a conceptual point of view, CFT and TF are not feature equivalent. You won't be able to express all things that CFT is able to deploy in TF.

From a practical perspective, it is somehow possible but you need to write a grammar converter ; and that is going very complex (almost impossible) if your CFT is using intrinsic functions https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html

In my opinion, you should use terraform aws_cloudformation_stack to deploy your existing CFT instead of trying to convert it ( https://www.terraform.io/docs/providers/aws/r/cloudformation_stack.html ).

So far I havent seen an easy way to convert CFN written in YAML or JSON to terraform code.

However you can try to deploy the resources via CFN, then use terraforming against the account you deployed CFN into. You can suck out most of the resources out in terraform code but not all. The code will need some refactoring and remaining resources (lambdas etc.) need to be rewritten in TF or "translated" from CFN into terraform.

Other option is to use terraform aws_cloudformation_stack to deploy CFN stack for you and manage it from there.

I'm not familiar with a tool for converting between Terraform <--> Cloudformation templates .

But, please check out the following alternatives:

  1. Using Terraform's Data Source: aws_cloudformation_export which allows access to stack exports specified in the Output section of the Cloudformation Template using the optional Export Property.

  2. Using a CLI tool like cf-to-tf for generating Terraform configuration and state for existing CloudFormation resources.

There isn't any tool to convert Cloudformation to Terraform, unless you're talking about simply managing Cloudformation templates via aws_cloudformation_stack .

And it's likely that you wouldn't even want to use such a tool anyway. Imagine a Cloudformation template which creates four subnets, probably written out using copy/pasted JSON or YAML with four separate resources ("PublicSubnet0", "PublicSubnet1", and so on). The way you'd likely do that in Terraform is with aws_subnet and count = 4 . Any converter that you use should be smart enough to handle that. But it should also be smart enough to know that "PrivateSubnet0" would be a whole different aws_subnet with its own count . That's very hard for any software to do.

So if you care about code quality in your Terraform scripts, do it manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM