Import Existing Infrastructure to Terraform

Terraform is a powerful infrastructure-as-code (IaC) tool that allows developers to define and manage their infrastructure using a simple declarative language. As you work with Terraform, you'll often find yourself with existing infrastructure that you need to import into Terraform to manage. In this blog post, we'll walk through the process of generating Terraform JSON files and tfstate files based on existing infrastructure.
February 14, 2023
min read
Yehonatan Rumyantsev
Cloud Specialist
Tags
No items found.
Related Resources
No items found.

TL;DR

Terraform is a powerful infrastructure-as-code (IaC) tool that allows developers to define and manage their infrastructure using a simple declarative language. As you work with Terraform, you'll often find yourself with existing infrastructure that you need to import into Terraform to manage. In this blog post, we'll walk through the process of generating Terraform JSON files and tfstate files based on existing infrastructure.

Prerequisites

Before you begin, ensure that you have the following installed and configured on your machine:

  1. Terraform (v0.13 or newer)
  2. Access to your cloud provider account (AWS, Azure, Google Cloud, etc.)

Step 1: Discovering Your Existing Infrastructure

The first step is to discover and gather information about the existing infrastructure. To do this, you'll need to use the specific tools and APIs provided by your cloud provider. For example, if you're working with AWS, you can use the AWS CLI or SDKs to list and describe your resources.

check out Lightlytics which can help you discover your entire cloud footprint.

Step 2: Generating the Terraform JSON Configuration

Once you have gathered information about your existing infrastructure, you'll need to generate a Terraform JSON configuration file that describes the resources. You can create this file manually, but there are also third-party tools that can help automate this process. One popular option is Terraformer, which supports multiple cloud providers and can generate Terraform JSON configuration files based on existing infrastructure.

To use Terraformer, install the tool according to the instructions on the GitHub repository and then run the appropriate command for your cloud provider. For example, to generate Terraform JSON for AWS resources, you would run:

terraformer import aws --resources=<resource_types> --regions=<regions> --profile=<aws_profile>

Replace <resource_types> with a comma-separated list of AWS resource types, <regions> with the AWS regions where your resources are located, and <aws_profile> with the name of your AWS CLI profile.

Step 3: Initializing and Importing the Terraform State

With the generated Terraform JSON configuration, you can now initialize the Terraform backend and import the existing resources into the Terraform state.

First, initialize the Terraform backend by running:

terraform init

Next, import each resource in your configuration into the Terraform state by running:

terraform import <resource_type>.<resource_name> <resource_id>

Replace <resource_type> with the type of the resource, <resource_name> with a unique name you assigned to the resource in the configuration, and <resource_id> with the ID of the resource in your cloud provider.

Step 4: Converting JSON to HCL (Optional)

If you prefer working with Terraform's HashiCorp Configuration Language (HCL) instead of JSON, you can convert the generated JSON files to HCL using a tool like json2hcl. Install the tool and then run:

json2hcl -reverse < input.json > output.tf

Step 5: Verify and Manage Your Infrastructure

Finally, verify that your Terraform configuration matches your existing infrastructure by running:

terraform plan

If everything looks correct, you can now manage your existing infrastructure using Terraform commands like terraform apply, terraform destroy, and more.