DEV Community

Cover image for πŸš€ Terraform Day 25: Importing Existing AWS Resources into Terraform State
Jeeva
Jeeva

Posted on

πŸš€ Terraform Day 25: Importing Existing AWS Resources into Terraform State

🧠 The Core Problem Terraform Import Solves
In many environments:
AWS resources already exist
They were created manually (console / CLI)
Terraform has no knowledge of them
Terraform tries to recreate them β†’ ❌ conflict

Example error:
Error: resource already exists

Why this happens:
Terraform only tracks what exists inside its state file
If a resource is not in state, Terraform assumes it does not exist

Terraform import fixes this by syncing reality into state.

🧱 Understanding Terraform State
terraform.tfstate is Terraform’s source of truth

It stores:
Resource IDs
Attributes
Metadata

Terraform uses state instead of calling AWS APIs repeatedly

If state is lost:
Terraform forgets everything
Import can rebuild state safely

πŸ”„ Terraform Import Workflow
Resource already exists in AWS
Write a minimal Terraform resource block
Run terraform import
Terraform maps the live resource β†’ state file
Terraform now manages the resource
Import updates state only, not the live resource.

πŸ“Œ Terraform Import Commands Used
Import Security Group
terraform import aws_security_group.web_sg sg-xxxxxxxx

Import EC2 Instance
terraform import aws_instance.web ec2-xxxxxxxx

Verify Imported State
terraform state list
terraform state show aws_security_group.web_sg

πŸ” Terraform Import vs Other Tools
Terraform Import (Recommended)
βœ… Officially supported
βœ… Covers all AWS resources
βœ… Safe and predictable
❌ Requires manual config writing

Terraformer / AWS2TF
βœ… Auto-generate config
❌ Limited resource coverage
❌ Community-maintained
❌ Often outdated or buggy

Production best practice:
Use Terraform import for accuracy and control.

🏁 Conclusion
Day 25 highlights a non-negotiable Terraform skill.

Terraform import bridges the gap between:
Manual infrastructure
Fully automated Infrastructure as Code

Without import:
Terraform fails
Conflicts occur
Automation breaks

With import:
Legacy infrastructure becomes manageable
Terraform regains control
IaC adoption becomes possible

This is how Terraform is used in real AWS environments, not just greenfield projects.

Top comments (0)