From CloudFormation to Terraform: Simplifying Infrastructure Management

Sravya BollaSravya Bolla
2 min read

As cloud infrastructure continues to evolve, many developers are shifting from traditional tools like AWS CloudFormation to Terraform, thanks to its flexibility, readability, and provider-agnostic approach.

Let’s walk through a scenario where X, a developer, is transitioning from CloudFormation and wants to manage existing AWS resources (like an EC2 instance or VPC) using Terraform — without recreating them or affecting live resources.

Here’s where Terraform's import and auto-generate features come in handy.

Scenario: Managing an Existing VPC and EC2 Instance

X wants to manage a VPC and an already running EC2 instance in a specific AWS region. Since these resources are already live, he doesn’t want to delete or recreate them. Instead, he wants to bring them under Terraform's control.

Let’s break down the simple steps he followed.

Start with the Terraform provider block:

provider "aws" {
  region = "us-east-1"
}

Replace "us-east-1" with your target AWS region.

Step 2: Import the Existing Resource

To manage the EC2 instance using Terraform, we need to import it into the Terraform state:


import{
id=provide the existing id of ec2
to=aws_instance.example 
}

Now run this command in terminal to generate Terraform configuration based on the live instance:

terraform plan -generate-config-out=generated.tf

This command:

  • Analyzes your AWS environment

  • Detects the resource

  • Outputs the configuration into generated.tf

Copy the relevant resource block from this file and paste it into your main .tf file, like so in place of import:

resource "aws_instance" "my_instance" {
  # Paste contents from generated.tf
}

Now, import the actual AWS instance into Terraform state with:

terraform import aws_instance.my_instance id of instance

Replace the instance ID with your actual EC2 instance ID.

This step tells Terraform:
"Hey, this EC2 instance already exists — start managing it using this Terraform resource block."

And You're Done!

Now you can safely run terraform plan and terraform apply to manage this instance declaratively.

Why This Is Better Than Manual Setup

Manually defining live resources in .tf files and mapping them to state is error-prone. But with Terraform’s import and plan -generate-config-out, the process becomes far more efficient and less risky — perfect for migrating from tools like CloudFormation.

Hope you like it:)

0
Subscribe to my newsletter

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

Written by

Sravya Bolla
Sravya Bolla