How to Sync Your Existing Cloud Infrastructure to Terraform Code Using Terraformer

Introduction: Infrastructure as Code (IaC) has become a standard practice for reliably and at scale managing and provisioning cloud resources. Tools like Terraform allow you to define and manage infrastructure declaratively. But what if you already have resources deployed manually through the AWS Console, CLI, or other tools, and now you want to bring everything under Terraform management without recreating or disrupting it? Writing Terraform files for existing infrastructure can be tedious, error-prone, and time-consuming. Fortunately, there's a solution: Terraformer. In this blog, we'll cover: The problem of syncing the existing infrastructure. How Terraformer solves it. Step-by-step guide to import your deployed infrastructure as Terraform code. Problem: When infrastructure is deployed manually or without IaC tools, it becomes difficult to: Track changes. Apply version control. Automate deployments consistently across environments. Scale reliably. Manually converting each resource into Terraform code is inefficient, especially for environments with hundreds or thousands of resources. Solution: Terraformer Terraformer is a powerful CLI tool developed by Google that can automatically generate Terraform configurations and state files based on existing infrastructure across providers like AWS, GCP, Azure, and more. Using Terraformer, you can: Export existing cloud resources. Generate usable .tf (Terraform) files. Create a corresponding terraform.tfstate file. Save hours of manual work and avoid errors. Step-by-Step Guide to Sync AWS Infrastructure with Terraformer Install Terraformer If you're on macOS: brew install terraformer For Linux/Windows: Download from Terraformer GitHub Releases. Move the binary to a directory included in your PATH. Configure AWS Credentials Make sure AWS CLI is set up: aws configure Or export your credentials manually: export AWS_ACCESS_KEY_ID=your-access-key-id export AWS_SECRET_ACCESS_KEY=your-secret-access-key Import All AWS Resources To import everything, including EC2 instances, VPCs, IAM roles, S3 buckets, Lambdas, and more: terraformer import aws --resources=all --region=us-east-1 If you want to import specific services only: terraformer import aws --resources=ec2,vpc,iam,s3,lambda --region=us-east-1 Understand the Generated Files After running Terraformer, you’ll find: main.tf — Terraform configuration with your resources defined. terraform.tfstate — Terraform state file reflecting your existing resources. Resource-specific directories (sometimes, depending on provider and services). Example main.tf snippet: resource "aws_vpc" "example" { id = "vpc-abc123" cidr_block = "10.0.0.0/16" } resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" } Review and Refine the Terraform Code Although Terraformer does an excellent job, manual adjustments may still be needed: Simplify or clean up generated configurations. Separate resources logically into different .tf files (for readability). Handle complex IAM policies and nested configurations carefully. Manage Your Terraform State After importing, configure a proper backend (like S3 with DynamoDB locking for AWS) to manage your Terraform state files securely and collaboratively. Example S3 backend configuration: `terraform { backend "s3" { bucket = "my-terraform-state-bucket" key = "state/terraform.tfstate" region = "us-east-1" dynamodb_table = "terraform-lock" } } ` Conclusion: Migrating manually deployed infrastructure into Terraform doesn't have to be painful. With Terraformer, you can quickly generate Terraform code for existing resources, take control of your cloud infrastructure, and embrace best practices like version control, automation, and scalability. Start small, review carefully, and soon you'll have your entire environment under Terraform management!

Apr 23, 2025 - 06:43
 0
How to Sync Your Existing Cloud Infrastructure to Terraform Code Using Terraformer

Introduction:

Infrastructure as Code (IaC) has become a standard practice for reliably and at scale managing and provisioning cloud resources. Tools like Terraform allow you to define and manage infrastructure declaratively. But what if you already have resources deployed manually through the AWS Console, CLI, or other tools, and now you want to bring everything under Terraform management without recreating or disrupting it?

Writing Terraform files for existing infrastructure can be tedious, error-prone, and time-consuming. Fortunately, there's a solution: Terraformer.

In this blog, we'll cover:

The problem of syncing the existing infrastructure.

How Terraformer solves it.

Step-by-step guide to import your deployed infrastructure as Terraform code.

Problem:

When infrastructure is deployed manually or without IaC tools, it becomes difficult to:

Track changes.

Apply version control.

Automate deployments consistently across environments.

Scale reliably.

Manually converting each resource into Terraform code is inefficient, especially for environments with hundreds or thousands of resources.

Solution: Terraformer

Terraformer is a powerful CLI tool developed by Google that can automatically generate Terraform configurations and state files based on existing infrastructure across providers like AWS, GCP, Azure, and more.

Using Terraformer, you can:

Export existing cloud resources.

Generate usable .tf (Terraform) files.

Create a corresponding terraform.tfstate file.

Save hours of manual work and avoid errors.

Step-by-Step Guide to Sync AWS Infrastructure with Terraformer

  1. Install Terraformer

If you're on macOS:

brew install terraformer

For Linux/Windows:

Download from Terraformer GitHub Releases.

Move the binary to a directory included in your PATH.

  1. Configure AWS Credentials

Make sure AWS CLI is set up:

aws configure

Or export your credentials manually:

export AWS_ACCESS_KEY_ID=your-access-key-id
export AWS_SECRET_ACCESS_KEY=your-secret-access-key

  1. Import All AWS Resources

To import everything, including EC2 instances, VPCs, IAM roles, S3 buckets, Lambdas, and more:

terraformer import aws --resources=all --region=us-east-1

If you want to import specific services only:

terraformer import aws --resources=ec2,vpc,iam,s3,lambda --region=us-east-1

  1. Understand the Generated Files

After running Terraformer, you’ll find:

main.tf — Terraform configuration with your resources defined.

terraform.tfstate — Terraform state file reflecting your existing resources.

Resource-specific directories (sometimes, depending on provider and services).

Example main.tf snippet:

resource "aws_vpc" "example" {
  id         = "vpc-abc123"
  cidr_block = "10.0.0.0/16"
}

resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
  1. Review and Refine the Terraform Code

Although Terraformer does an excellent job, manual adjustments may still be needed:

Simplify or clean up generated configurations.

Separate resources logically into different .tf files (for readability).

Handle complex IAM policies and nested configurations carefully.

  1. Manage Your Terraform State

After importing, configure a proper backend (like S3 with DynamoDB locking for AWS) to manage your Terraform state files securely and collaboratively.

Example S3 backend configuration:


`terraform {
  backend "s3" {
    bucket = "my-terraform-state-bucket"
    key    = "state/terraform.tfstate"
    region = "us-east-1"
    dynamodb_table = "terraform-lock"
  }
}
`

Conclusion:

Migrating manually deployed infrastructure into Terraform doesn't have to be painful. With Terraformer, you can quickly generate Terraform code for existing resources, take control of your cloud infrastructure, and embrace best practices like version control, automation, and scalability.

Start small, review carefully, and soon you'll have your entire environment under Terraform management!