AWS Import/Export infrastructure as code (CloudFormation | Terraform)
Tags: aws, terraform, devops, cloud
Published: 2022-07-05 13:57:12

Based on our **Infrastructure as Code (IaC)** experience at **_Binbash_** (https://www.binbash.com.ar) we found several ways to translate existing manually deployed (from Web Console UI) AWS infra to Cloudformation (CF) and / or Terraform (TF) code.

### AWS Cloudformation Templates


![aws cloudformation overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a842w6856x2jrv9uh54m.png) <center><small>https://aws.amazon.com/cloudformation/</small></center> 

---
### CF-#1 | AWS CloudFormation native import feature
Introduced feature (since Nov 2019) that allows to Import existing resources into a CloudFormation stack

For more info check: https://aws.amazon.com/blogs/aws/new-import-existing-resources-into-a-cloudformation-stack/


![aws cloudformation workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfyfmxwrluxathkvt81w.png) <center><small>https://aws.amazon.com/cloudformation/</small></center>

---
### CF-#2 | `aws cli` & manually translate to CF
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`

Afterwards 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 best practices, fully parameterized (`Sub, Ref, Join, Fn::GetAtt:, Fn::ImportValue`), modular, applying `conditions` . As a result your 1st iteration would probably be close to the final state of the templates

Very interesting reference examples: <https://github.com/widdix/aws-cf-templates/>

![aws cloudformation template workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/js5wq2soh7um44kn2wlk.png) <center><small>https://aws.amazon.com/cloudformation/</small></center>

---
### CF-#3 | Former2
[Former2](https://former2.com/) allows you to generate Infrastructure-as-Code outputs from your existing resources within your AWS account. By making the relevant calls using the AWS JavaScript SDK, Former2 will scan across your infrastructure and present you with the list of resources for you to choose which to generate outputs for.

---
## Terraform Code / Modules
![cloudcraft + modules.tf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/536dkzh58dflqd4trvbq.png) <center><small>https://cloudcraft.co/ + https://modules.tf/</small></center>

### TF-#1 | CloudCraft + Modules.tf
Map your reality, don't waste time producing static documentation that is outdated as soon as you publish.

[Cloudcraft](https://cloudcraft.co/) Live instantly analyzes all the service relationships of your AWS environment, and reverse engineers a complete system architecture diagram.

Their powerful auto-layout and advanced AWS account scanning capabilities are also available to you as an API. Then via the "Terraform code export" (<https://modules.tf/>) will be generating infrastructure as Terraform configurations from visual diagrams created using [Cloudcraft](https://cloudcraft.co/).

![cloudcraft console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2rnzvbgrvb0zuekwp69.png) <center><small>Cloudcraft terraform code export</small></center>

---
### TF-#2 | Brainboard.co
With https://brainboard.co you could import your cloud infrastructure
and turn AWS environments into actionable visual Terraform infrastructure-as-code IaC.

They named this feature as _"Terraform Reverse"_ , which allows users to  import an infrastructure from your cloud credentials. :ledged: **NOTE:** This feature is in Beta and will be released soon for everyone.

The complete doc could be found here https://docs.brainboard.co/start/import-infrastructure

![brainboard terraform reverse](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekasfm9b8wvxtfvv50f8.png) <center><small>Importing your infrastructure with brainboard.co from AWS</small></center>

![brainboard terraform diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnswdngg6cqpyw08scse.jpeg) <center><small>https://www.brainboard.co/ diagram + terraform code</small></center>