Terraformer: A Powerful Tool for Importing Infrastructure into Terraform

The provisioning and management of cloud infrastructure resources can be automated using the well-liked open source Terraform Infrastructure as Code tool by developers and IT specialists. You may construct and manage cloud resources using Terraform on a variety of cloud computing infrastructures, such as Microsoft Azure, Amazon Web Services (AWS), and Google Cloud Platform (GCP).

One of the most useful features of Terraform is its ability to import existing resources into your infrastructure as code. This allows you to take control of resources that may have been created manually or by another team and bring them under the management of your infrastructure as code. However, the process of importing resources can be time-consuming and error-prone, particularly if you are dealing with many resources or complex configurations. This is where Terraformer comes in.

What is Terraformer?

Terraformer is an open-source tool written in Go Language, originally developed by Waze SRE that automates the process of importing existing resources into Terraform. It allows you to generate Terraform code from existing resources, making it easy to manage them using Terraform. Terraformer supports a wide range of cloud providers, including AWS, GCP, Azure, Kubernetes, and more. Terraformer currently supports sixteen clouds and more than fifteen providers like Datadog, Kubernetes, PagerDuty, GitHub, and more.

How does Terraformer stand apart from its competitors?

  1. Terraformer differs from other competitors in a few keyways.
  2. Terraformer is a command-line tool, while some of its competitors are web-based tools. This makes Terraformer more portable and easier to use in a CI/CD pipeline.
  3. Terraformer eliminates the manual intervention needed in other IaC tools by automatically generating configurations after importing the infrastructure.
  4. Terraformer supports a wider range of infrastructure sources than some of its competitors. Terraformer currently supports AWS, Azure, GCP, and Kubernetes, while some of its competitors only support a subset of these providers.
  5. Finally, Terraformer is easier to use as it has a simpler user interface and provides more helpful documentation.

How to use Terraformer?

Using Terraformer is straightforward. First, you need to install it on your local machine. You can do this using the command-line interface (CLI) or a Docker container. Once installed, you can use Terraformer to generate Terraform code for existing resources.

To install Terraformer on a Linux machine, run the below commands:

export PROVIDER={aws}

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

By running the below command, you can check the Terraformer version.

terraformer -v

To import resources with an AWS provider, first authenticate to the AWS account in which the resource is located. To configure Terraformer with your AWS credentials, you would use the following command

terraformer configure –provider aws –credentials ~/.aws/credentials

AWS configuration, including environmental variables, a shared credentials file (~/.aws/credentials), and a shared configuration file (~/.aws/config) will be loaded by the tool by default. To use a specific profile, you can use the following command:

After authenticating to the AWS account, run terraform init against a provider.tf file to install the plugins required for your platform.

terraform {

  required_providers {

    aws = {

      source  = “hashicorp/aws”

      version = “~> 4.0”

    }

  }

}

provider “aws” {

  region = “us-east-1”

}

To import all AWS Elasticache and RDS into Terraform, use the below command:

terraformer import aws –-path-pattern=”{output}/” –compact=true –regions=eu-central-1 –resources=elasticache,rds

The above command tells Terraformer to import all the Elastic cache and RDS in the region eu-central-1 and generate Terraform code, and the –compact=true flag tells Terraformer to write all the configurations in a single file.

Terraformer also supports importing multiple resources at once, and you can use filters to import only specific resources that meet certain criteria.

For example, you can import all AWS EC2 instances that have a certain tag by using the following command:

terraformer import aws –path-pattern=”./ec2/” –compact=true –regions=eu-central-1 –resources=ec2_instance –filter=”Name=tags.NodeRole;Value-node”

The above command tells Terraformer to create Terraform code in the directory./ec2/ and import all EC2 instances with the tag Noderole=node.

By default, Terraformer separates each resource into a file that is placed in a specified service directory. Each provider may have a different default path for resource files, which is {output}/{provider}/{service}/{resource}.tf.

We can manage the resources with Terraform using the plan, apply, and remove actions now that the configuration files have been created with Terraformer.

Also Read this informative blog on How to Secure an AWS Environment with Multiple Accounts.

Benefits of using Terraformer

Achieve Infrastructure as Code: Terraform promotes the principle of infrastructure as code, where infrastructure resources are defined in a declarative language. The tool allows users to import existing resources into Terraform, making it easier to maintain a consistent and reproducible infrastructure state.

Version and manage resources: By importing resources into Terraform, users can take advantage of Terraform’s versioning and management capabilities. This includes tracking changes, applying modifications, and collaborating on infrastructure changes through version control systems.

Apply infrastructure changes: With imported resources, users can modify and apply changes to their infrastructure using Terraform. This provides a standardised and automated way to manage the lifecycle of resources, ensuring consistency and reducing the risk of manual errors.

Leverage the Terraform ecosystem: Importing resources into Terraform allows users to leverage the extensive ecosystem of Terraform providers, modules, and other tooling. This enables the use of various integrations and extensions to enhance infrastructure management and provisioning.

Start automating your cloud infrastructure with Terraformer today and streamline your resource provisioning and management.

Click here

Conclusion

Terraformer is a valuable tool for organizations that are looking to improve the speed, efficiency, and reliability of their infrastructure deployments. By automating the process of converting existing infrastructure to Terraform configuration files, providing a consistent and repeatable way to provision infrastructure resources, and enabling organizations to track changes to infrastructure resources and to roll back changes, if necessary, Terraformer can help organizations to save time and reduce the risk of errors and disruptions.