简体   繁体   English

将 AWS CloudFormation 转换为 Terraform 模板

[英]Convert AWS CloudFormation to Terraform template

I want to convert the existing AWS CloudFormation templates (yaml) to Terraform templates(hcl) automatically.我想将现有的 AWS CloudFormation 模板 (yaml) 自动转换为 Terraform 模板 (hcl)。

Can anyone please suggest some tools, approaches, etc?任何人都可以建议一些工具,方法等吗?

From a conceptual point of view, CFT and TF are not feature equivalent.从概念的角度来看,CFT 和 TF 并不是等价的。 You won't be able to express all things that CFT is able to deploy in TF.您将无法表达 CFT 能够在 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如果您的 CFT 使用内在函数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 ).在我看来,您应该使用 terraform aws_cloudformation_stack 来部署现有的 CFT,而不是尝试对其进行转换 ( 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.到目前为止,我还没有看到将用 YAML 或 JSON 编写的 CFN 转换为 terraform 代码的简单方法。

However you can try to deploy the resources via CFN, then use terraforming against the account you deployed CFN into.但是,您可以尝试通过 CFN 部署资源,然后对您部署 CFN 的帐户使用terraforming You can suck out most of the resources out in terraform code but not all.您可以提取 terraform 代码中的大部分资源,但不是全部。 The code will need some refactoring and remaining resources (lambdas etc.) need to be rewritten in TF or "translated" from CFN into terraform.代码需要一些重构,剩余的资源(lambdas 等)需要用 TF 重写或从 CFN“翻译”到 terraform。

Other option is to use terraform aws_cloudformation_stack to deploy CFN stack for you and manage it from there.其他选项是使用 terraform aws_cloudformation_stack为您部署 CFN 堆栈并从那里管理它。

I'm not familiar with a tool for converting between Terraform <--> Cloudformation templates .我不熟悉用于在 Terraform <--> Cloudformation模板之间进行转换的工具。

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.使用 Terraform 的数据源: aws_cloudformation_export ,它允许使用可选的导出属性访问 Cloudformation 模板的输出部分中指定的堆栈导出。

  2. Using a CLI tool like cf-to-tf for generating Terraform configuration and state for existing CloudFormation resources.使用cf-to-tf等 CLI 工具为现有 CloudFormation 资源生成 Terraform 配置和状态。

There isn't any tool to convert Cloudformation to Terraform, unless you're talking about simply managing Cloudformation templates via aws_cloudformation_stack .没有任何工具可以将 Cloudformation 转换为 Terraform,除非您只是在谈论通过aws_cloudformation_stack管理 Cloudformation 模板。

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).想象一个创建四个子网的 Cloudformation 模板,可能使用复制/粘贴 JSON 或 YAML 和四个单独的资源(“PublicSubnet0”、“PublicSubnet1”等)写出。 The way you'd likely do that in Terraform is with aws_subnet and count = 4 .在 Terraform 中,您可能会使用aws_subnetcount = 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 .但它也应该足够聪明,知道“PrivateSubnet0”将是一个完全不同的aws_subnet ,它有自己的count That's very hard for any software to do.这是任何软件都很难做到的。

So if you care about code quality in your Terraform scripts, do it manually.因此,如果您关心 Terraform 脚本中的代码质量,请手动进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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