Write the Terraform configuration for the resource first
Find the resource's ID in AWS
Run the import command for each resource
Verify and adjust the configuration to match the actual state
2. Import Blocks (Terraform 1.5+)
A newer declarative approach that allows you to define imports in your configuration:
hcl
import {
id = "i-1234567890abcdef0"
to = aws_instance.example
}
resource "aws_instance" "example" {
# Configuration will be generated or needs to be written
}
Then run terraform plan to generate configuration or validate existing config.
3. Third-Party Tools
Terraformer - Generates both Terraform configurations and state files from existing infrastructure:
5. AWS Control Tower Account Factory for Terraform (AFT)
For AWS Control Tower users, AFT can help manage and import organizational infrastructure at scale.
Best Practices
When importing infrastructure:
Start with read-only access to scan and understand your infrastructure
Import in stages - begin with stateless resources like VPCs and security groups
Use data sources for resources you don't want to manage but need to reference
Version control everything before making changes
Test in a non-production environment first
Document dependencies between resources carefully
The choice of method depends on your infrastructure size, complexity, and whether you're doing a one-time migration or ongoing imports. For large-scale imports, tools like Terraformer can save significant time, while for selective imports of specific resources, the manual import or import blocks approach gives you more control.