Terraform Advanced β Bulk Import Strategies
Strategies for importing hundreds of existing resources into Terraform.
Strategy 1: Script-Generated Import Blocks
#!/bin/bash
# generate-imports.sh β Generate import blocks from AWS CLI
# List all S3 buckets
aws s3api list-buckets --query 'Buckets[].Name' --output text | tr '\t' '\n' | while read bucket; do
cat <<EOF
import {
to = aws_s3_bucket.this["${bucket}"]
id = "${bucket}"
}
EOF
done > imports.tf
Strategy 2: Terraformer (Third-Party)
# Install
$ brew install terraformer
# Import all S3 buckets
$ terraformer import aws --resources=s3 --regions=us-east-1
# Import multiple resource types
$ terraformer import aws \
--resources=vpc,subnet,security_group \
--regions=us-east-1
Strategy 3: Incremental Import
Week 1: Import networking (VPC, subnets, route tables)
Week 2: Import compute (EC2, ASGs, ALBs)
Week 3: Import data (RDS, ElastiCache, S3)
Week 4: Import IAM (roles, policies)
Week 5: Import DNS (Route53)
π‘ Production Tip: Import in dependency order β networking first, then compute, then application-level resources. This matches the natural
terraform applyorder.
Whatβs Next?
Gracefully remove resources from state.