Importing existing infra into terraform using Terraformer

Sonal KumarSonal Kumar
4 min read

There are many tools to manage creation, maintenance, configuration and updation of infrastructure using Iac tools.

One such Iac tool is terraform.

What is terraform ?
Terraform is like a remote control for managing your cloud infrastructure.With terraform, it's like having a blueprint for your house. You write down all your requirements in a clear and organised plan (these are your Terraform configuration files). Then, you hand this plan over to a contractor (Terraform) who takes care of everything according to your plan, making sure all parts are built and set up correctly without you needing to manage each step manually.
Benefit : it supports multi-cloud

How it helps to maintain Infrastructure?
Here are some of the benefits of using terraform -

Code-Based Configuration: All infrastructure is defined in code, ensuring consistent setups.
Automated Deployment: Terraform can automatically create, update, and delete infrastructure resources based on the configurations.
Reusable Modules: Terraform allows you to create reusable modules for common setups, which can be shared and reused across different projects, promoting scalability and efficiency.
Change Detection: Before applying changes, Terraform shows a plan of what it will do, so you can review and approve changes.
State File: Terraform maintains a state file that keeps track of the current state of your infrastructure. This state file allows Terraform to understand the differences between your configuration and the actual deployed resources, enabling efficient and accurate updates.

Suppose you already have an existing setup in cloud for example say AWS.
Lets say, you have an existing infra setup of VPC in the AWS cloud and going forward you want to manage the infrastructure from terraform. For that you'll need to import the existing infra into terraform , this importing will technically create a terraform.tfstate file which will inform terraform about the existing resources in our case the existing VPC resources so that in future we can directly tweak the configuration from the terraform script itself and terraform will know that we need not create new resources rather we need to update existing ones.

One such way to achieve it is through terraformer.

What is terraformer ?
Terraformer is an open-source tool that automates the generation of Terraform configuration files from existing infrastructure. It allows you to reverse-engineer your current setup in cloud providers and convert it into Terraform code. This can be particularly useful if you have an existing environment that wasn't initially managed with Terraform and you want to start using Terraform to manage it going forward.

Alternatives for importing in terraform
We have another alternate option that is terraform import block.
Earlier we had terraform import command which used to take each resource id one by one to import them
In case of import block we have the flexibility of importing all the ids of a particular resource together by passing then through loop.

Benefits of using terraformer over terraform import
Benefits which I found were -

1. Terraformer gives us the configuration files upon running the import command. We don't have to create the whole configuration files. We just need to update few parameters if needed.
2. It also gives us the possibility to import all the resources in a particular region
Ex: terraformer import aws --resources=* --regions=ap-south-1
3. It allows us to import all the id's of a particular resource in a region
# terraformer import aws --resources=vpc --regions=ap-south-1
In this case all the VPC's present in ap-south-1 region will be imported
4. Can import multiple resources together
Ex : terraformer import aws --resources=vpc,rds --regions=ap-south-1

Limitations of using terraformer
Some limitation which I found were -

1. It create a separate terraform.tfstate file for each imported resources.
suppose if you have imported VPC and subnets then 2 different statefiles for these two resources will be created during import.
Ex : imported two resources vpc and subnet it created separate folders and statefiles for both the resources

2. The values will be hard-coded in the configurations file.

Demo :

In the demo, we'll see an example of importing VPC's form a particular region
In my case I am importing VPC's from AWS cloud of region ap-south-1
# terraformer import aws --resources=vpc --regions=ap-south-1

Before running this command we need to have a provider.tf file so that terraform is able to install all the plugins needed to interact with AWS.

provider.tf

provider "aws" {
  region = "ap-south-1"
}

Next after creating this file run
# terraform init

Now we'll run the terraformer command to import all VPC's from region ap-south-1

# terraformer import aws --resources=vpc --regions=ap-south-1

The following folder structure gets created -

in here,
generated is the folder created followed by cloud (in our case its aws) then resource_name
generated -> aws -> vpc

And when you run terraform plan no changes are shown means terraform has understood in its statefile that these VPC's exist in aws and going forward terraform will maintain them.

I hope this blog helps you to understand terraformer as a tool for importing.

0
Subscribe to my newsletter

Read articles from Sonal Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sonal Kumar
Sonal Kumar