Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export AWS configuration as CloudFormation template

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

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

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

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

like image 541
paul Avatar asked Jun 27 '16 15:06

paul


People also ask

How do I export aws VPC configuration?

To export client configuration (console)Open the Amazon VPC console at https://console.aws.amazon.com/vpc/ . In the navigation pane, choose Client VPN Endpoints. Select the Client VPN endpoint for which to download the client configuration and choose Download Client Configuration.

Can we generate CloudFormation template from existing resources?

Create a stack from existing resources using the AWS Management Console. Sign in to the AWS Management Console and open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation . On the Stacks page, choose Create stack, and then choose With existing resources (import resources).

How do I export from CloudFormation?

To export a stack's output value, use the Export field in the Output section of the stack's template. To import those values, use the Fn::ImportValue function in the template for the other stacks. For a walkthrough and sample templates, see Walkthrough: Refer to resource outputs in another AWS CloudFormation stack.


1 Answers

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

  1. Using a new CloudFormation native introduced feature (since Nov 2019) that allows you to Import existing resources into a CloudFormation stack

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

  • RDS Instance -> Type: AWS::RDS::DBInstance,
  • RDS (EC2) SG -> Type: AWS::EC2::SecurityGroup,
  • RDS Subnet Group -> Type: AWS::RDS::DBSubnetGroup and
  • RDS DB Param Group -> Type: AWS::RDS::DBParameterGroup

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. Using 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
  • 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.

Extra points! :)

  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.

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

  • https://former2.com
  • https://modules.tf

Related Article: https://medium.com/@exequiel.barrirero/aws-export-configuration-as-code-cloudformation-terraform-b1bca8949bca

like image 132
Exequiel Barrirero Avatar answered Sep 28 '22 10:09

Exequiel Barrirero