DEV Community

Cover image for ->> Day-25 Terraform Import In AWS
Amit Kushwaha
Amit Kushwaha

Posted on • Edited on

->> Day-25 Terraform Import In AWS

managing Existing AWS Infrastructure Using Terraform Import!

When working in real-world cloud environments, infrastructure is not always created using infrastructure as Code from day one.

Sometimes resources already exist - created manually through the AWS Console.

So the question becomes:

How do we bring those existing resources under Terraform management safely?

That’s exactly what this project demonstrates.


The Problem
You already have:

  • A VPC
  • A Security Group
  • Possibly EC2 instances

But none of them are managed through Terraform.

Managing infrastructure manually:

  • X Is not version controlled
  • X Is not reproducible
  • X Is error-prone

We need a structured way to manage it using Iac - without recreating everything.


The Solution: terraform import

Terraform provides a command that allows you to map existing cloud resources into Terraform state:

terraform import <resource_type.resource_name> <resource_id>
Enter fullscreen mode Exit fullscreen mode

This command does not create infrastructure.
It simply tells Terraform:

| "This resource already exists. start managing it."


Architecture Overview

The workflow looks like this:

  1. Write Terraform configuration files (.tf)
  2. Configure AWS provider
  3. Reference existing VPC using a data source
  4. Define the Security Group in Terraform
  5. Use terraform import to attach the real AWS resource to Terraform state
  6. Validate using terraform plan

Once imported, Terraform can now track and manage that resource.


Project Structure

terraform/
├── main.tf           # Provider configuration
├── variables.tf      # Region and VPC input
├── vpc.tf            # Fetch existing VPC using data source
├── security_group.tf # Define Security Group to import
Enter fullscreen mode Exit fullscreen mode

Import Workflow

  1. Initialize Terraform
terraform init
Enter fullscreen mode Exit fullscreen mode
  1. Import Existing Security Group
terraform import aws_security_group.app_sg sg-xxxxxxxx
Enter fullscreen mode Exit fullscreen mode

Terraform now maps the real AWS Security Group to the resource block.


Terraform validate

terraform plan
Enter fullscreen mode Exit fullscreen mode

If everything matches, you’ll see No changes.

That means Terraform and AWS are in sync.

Conclusion

Adopting Infrastructure as Code doesn’t mean you need to rebuild everything from scratch.

With terraform import, you can gradually transition manual cloud infrastructure into a version-controlled, structured Terraform workflow.

This is a practical and realistic DevOps approach - especially in environments where infrastructure already exists.

Resources:

>> Connect With Me

If you enjoyed this post or want to follow my #30DaysOfAWSTerraformChallenge journey, feel free to connect with me here:

💼 LinkedIn: Amit Kushwaha

🐙 GitHub: Amit Kushwaha

📝 Hashnode / Amit Kushwaha

🐦 Twitter/X: Amit Kushwaha

Found this helpful? Drop a ❤️ and follow for more AWS and Terraform tutorials!

Questions? Drop them in the comments below! 👇

Top comments (0)