Terraformer — Generate Terraform Files from Existing AWS infrastructure

Ashutosh Kaushik
4 min readAug 17, 2023

--

What is Terraformer ?

Terraformer is a CLI tool , which is written in Go . It is used to generate terraform files from existing infrastructure (Infrastructure to Terraform Files). It’s quite complex that the entire infrastructure begins to be written from scratch. Many industries have existing complex cloud infrastructure setup and they face challenge while integrating their existing infrastructure to Terraform script in a timely and cost-effective manner that Terraformer solves.

Terraformer is a tool which is created by Waze SRE . It is an open-source tool that can be modified and used across all major platforms and it supports supports 16 clouds and more than 15+ providers like Kubernetes, Datadog, PagerDuty, GitHub etc.

Here theGithub link of Terraformer :- https://github.com/GoogleCloudPlatform/terraformer

Capabilities of Terraformer

1. Generate tf/json + tfstate files from existing infrastructure for all supported objects by resource.

2. Remote State sharing and exporting to specified bucket locations.

3. Filtering

4. Requires only read-only permissions to list service resources, thus ensuring that it does not change anything within the existing infrastructure.

5. Connect between resources with terraform_remote_state (local and bucket).

6. Planning Capabilities — similar to Terraform plan

7. Import by resource name and type.

Here I am using Linux(Ubuntu: 20.0) for import the infrastructure.

1.Install Terraform beforehand(Check terraform version)

Run the command:- terraform -v

2.Installation of Terraformer:

First thing go to terraformer github link and clone the repository using this command:- git clone https://github.com/GoogleCloudPlatform/terraformer.git

As i told you earlier i am using linux so run this command in your linux os:

Linux

i. export PROVIDER={aws}

or you wants to export all the provider then use this command

export PROVIDER={all,google,aws,kubernetes}

ii. curl -LO "https://github.com/GoogleCloudPlatform/terraformer/releases/download/$(curl -s https://api.github.com/repos/GoogleCloudPlatform/terraformer/releases/latest | grep tag_name | cut -d '"' -f 4)/terraformer-${PROVIDER}-linux-amd64"

iii. chmod +x terraformer-${PROVIDER}-linux-amd64

iv. sudo mv terraformer-${PROVIDER}-linux-amd64 /usr/local/bin/terraformer

v. brew install terraformer

vi. if brew command is not working that means i is not present in your OS. so run this command to install brew: sudo apt install brew

vii. Now for testing purpose check the version of terraformer that insure terraformer is successfully installed in your system ot not so run this command: terrafomer — version

Now Everything is done for terraformer lets give permission to create .tf files.

3. Create IAM role or user in AWS :

Give Readonly access so that it will not change the insfrastructure by mistake. Create CLI Access to user for this now you have your access-key and secret-key both.

So now i will give access to that particular OS. But you need to install AWS-cli in your system. so for this you need to follow these steps:

i. curl “https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o “awscliv2.zip”

ii. unzip awscliv2.zip
if you have not unzip command then install this by use of this command: sudo apt install unzip

ii. sudo ./aws/install

iv. /usr/local/bin/aws — version
Run this command for check AWS-cli is installed or not.

4. Now configure your AWS role or user inside this OS:

Run this command:
i. aws configure

it will ask you to put access-key and secret-key put these key then it will ask you to give region don’t give anything.

for confirmation run one that insure that aws is configured successfully:
ii. aws s3 ls

it will give the list of all s3 bucket.

5. Now finally we all set to import AWS infrastructure:

Create a terraform file like init.tf and put these code also you can put your credential like access-key & secret-key:
provider “aws” {
region = “ap-south-1”
profile = “default”
access_key = “yytdytdddududuu6fd”
secret_key = “ddutd786ffu7uccut76r86udydyddututddf”
}

If you wants to import all the resources like ec2,route53,vpc,lambda all the resources run a single command and in some minute you have all the infrastructure in the .tf file.

terraformer import aws — resources=”*”

it will give you all the .tf file in a directory generated.

if you wants to import particular resources like rds use this command also in that condition use give the region:

terraformer import aws — resources=”rds” — region=ap-south-1

NOTE:- If you are facing issue in resources i am giving you reference from that you can resolve that issue: https://github.com/GoogleCloudPlatform/terraformer/issues/470

Thanks for Reading I hope it will help you.😎

--

--