45

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?

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
paul
  • 12,873
  • 23
  • 91
  • 153

4 Answers4

31

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/).

Extra points! :)

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

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

Exequiel Barrirero
  • 4,998
  • 1
  • 36
  • 27
  • 2
    Note: CloudFormer doesn't pick up many services as of this date, like Elasticsearch. – Alex Moore-Niemi Jan 23 '20 at 16:35
  • 1
    Great answer, make sure you know what are the supported resources for option #1 as not all the services are supported e.g AppSync. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import-supported-resources.html – Oscar Nevarez May 11 '20 at 01:04
15

It's not possible using the AWS CLI but you can use the CloudFormer [1] tool to create a CloudFormation template from existing resources. I've had decent success with it. The templates aren't as "pretty" as hand-made templates but they provide a good starting point.

[1] http://aws.amazon.com/developertools/6460180344805680

jzonthemtn
  • 3,344
  • 1
  • 21
  • 30
  • I tried to use it, but always after finish it start running the new stack in cloudformation instead of allow me to download the template, which is what I need, am I doing something wrong here? – paul Jun 27 '16 at 15:29
  • It should give you the option to save the template to an S3 bucket. (See bottom of http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-using-cloudformer.html.) Or, you could just copy/paste the template to a file on your computer. – jzonthemtn Jun 27 '16 at 15:33
  • The link does not work, and in the bottom of the page you paste before, give me the option to Specify an Amazon S3 template URL, but not to download. Like I said after I finish all steps, start running the stack in cloudformation. I think I´m using the same tool that you said – paul Jun 27 '16 at 15:43
  • A trailing period got included in that previous link: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-using-cloudformer.html – jzonthemtn Jun 27 '16 at 15:46
  • 4
    For anyone reading this now, CloudFormer is no longer maintained: https://stackoverflow.com/a/62944334/5402565 – libcthorne Oct 12 '21 at 10:12
6

In addition to CloudFormer, you might want to take a look at Bellerophon: https://github.com/arminhammer/bellerophon.

ataylor
  • 64,891
  • 24
  • 161
  • 189
  • 1
    Note that this Bellerophon doesn't support a lot of resources, e.g. the entire Elastic Beanstalk suite. – duality_ Nov 02 '18 at 14:51
2

I had some problems getting the tradidtional tools - mentioned above - working in our environment; we have a complicated API Gateway. Former2 didnt' find it at all (although seemed ideal for other resources)

I found another tool, "Terraformer" which extracts AWS into Terraform, which can then be turned into CloudFormation -or used directly as IaC.

https://github.com/GoogleCloudPlatform/terraformer#installation

Maybe that will work for others if the above tools don't.

J. Gwinner
  • 931
  • 10
  • 15