将 AWS 配置导出为 CloudFormation 模板 [英] Export AWS configuration as CloudFormation template

查看:90
本文介绍了将 AWS 配置导出为 CloudFormation 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AWS CLI 和 CloudFormation,但在文档中找不到任何参考资料.

I´m using AWS CLI and CloudFormation, and I could not find any reference in the documentation.

有人知道是否可以从当前配置创建 CloudFormation 模板.

Does anybody know if it´s possible to create a CloudFormation template from a current configuration.

假设我想从当前的安全组配置中获取 CloudFormation 模板.

Let´s say that I want to get a CloudFormation template from my current security group configuration.

知道是否可以使用 CLI 将该配置导出为模板吗?

Any idea if it´s possible to export that configuration as a template using CLI?

推荐答案

根据我们的经验,我们发现了 3 种可能的方法来将现有的手动部署(从 Web 控制台 UI)AWS 基础设施转换为 Cloudformation (CF).

Based on our experience we found 3 possible ways to translate existing manually deployed (from Web Console UI) AWS infra to Cloudformation (CF).

  1. 使用 CloudFormation 原生引入的新功能(自 2019 年 11 月起),允许您将现有资源导入 CloudFormation 堆栈

使用 aws cli 为构成您的堆栈的每个元素执行 $aws service_name_here describe,例如 RDS 数据库堆栈:

Using aws cli execute $aws service_name_here describe for each element that make up your stack eg for RDS Database Stack:

  • RDS 实例 ->类型:AWS::RDS::DBInstance,
  • RDS (EC2) SG ->类型:AWS::EC2::SecurityGroup,
  • RDS 子网组 ->类型:AWS::RDS::DBSubnetGroup
  • RDS 数据库参数组 ->类型:AWS::RDS::DBParameterGroup
  • 并根据从每个组件的 aws cli 获得的输出手动转换为 CF.这种方法通常需要更多的 AWS 和 CF 经验,但您创建的模板可以在良好实践下结构化和设计,完全参数化(Sub、Ref、Join、Fn::GetAtt:、Fn::ImportValue),模块化,应用 conditions 并且在第一次迭代中,结果可能接近模板的最终状态(有趣的参考示例:https://github.com/widdix/aws-cf-templates/).

    And manually translate to CF based on the outputs obtained from the aws cli for each of the components. This approach usually requires more experience in both AWS and CF but the templates that you are creating can be structured and designed under good practices, fully parameterized (Sub, Ref, Join, Fn::GetAtt:, Fn::ImportValue), modular, applying conditions and in a 1st iteration the result would probably be close to the final state of the templates (interesting reference examples: https://github.com/widdix/aws-cf-templates/).

    1. 使用 AWS CloudFormer:

    • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-using-cloudformer.html
    • http://www.tothenew.com/blog/using-aws-cloudformer-to-create-template-of-existing-infrastructure
    • 注意:生成的模板不是整洁"的.作为在方法 (1) 下手动开发的那些,它们将硬编码所有 ID 和值,并且它的结构可能不会遵循您用于 CF 模板的约定,而是当然,这将提供一个很好的起点.
      • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-using-cloudformer.html
      • http://www.tothenew.com/blog/using-aws-cloudformer-to-create-template-of-existing-infrastructure
      • NOTE: The resulting templates are not as "neat" as those developed manually under the approach (1), they will have all the IDs and values hard-coded and perhaps the structure of it will not follow the convention that you use for your CF templates, but of course it will provide a good starting point.
      • 加分!:)

        1. 使用 Terraforming(https://github.com/dtan4/地球化).考虑到新版本的 Terraform 0.12.0-beta2(terraform-provider-aws 2.7.0 已在稳定版本中支持),具有新功能和更友好的语法,并且没有忽略它是一个开源工具和云提供商不可知论者,我不会排除基于现有 AWS 基础设施生成 Terraform 代码的可能性,如果可能的话,以具有作为参考的模块和子模块的形式 ->https://registry.terraform.io/ 作为 AWS CF 的替代方案.

        1. Using Terraforming (https://github.com/dtan4/terraforming). Considering the new version of Terraform 0.12.0-beta2 (already supported in stable release by terraform-provider-aws 2.7.0), with new features and its more friendly syntax, and without ignoring that it is an open source tool and cloud-provider agnostic, I would no dismiss the possibility of generating Terraform code based on existing AWS infra, if possible under the form of modules and sub-modules having as reference -> https://registry.terraform.io/ as an alternative to AWS CF.

        将您当前部署的 AWS 基础设施导出到 Cloudformation/Terraform 代码的其他一些新替代方案:

        Some other new alternatives to export your current deployed AWS infra to Cloudformation / Terraform code:

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆