Skip to main content

Importing Resources into Terraform

After Terraback scans your cloud account and generates .tf files, the next step is to bring those resources under Terraform management by importing them into state. Terraback ships a fast, provider-level import command that imports every scanned resource for you, so you do not have to write or run terraform import one resource at a time.

terraback <aws|azure|gcp> import [OPTIONS]

Why Not Raw terraform import?

The built-in terraform import command imports a single resource per invocation and requires you to type the Terraform address and cloud ID by hand:

# Slow and manual - one command per resource
terraform import aws_vpc.main vpc-12345678
terraform import aws_s3_bucket.logs my-logs-bucket
terraform import aws_instance.web i-0abc123...
# ...repeat hundreds of times

For an account with hundreds of resources, this is hours of manual work and is easy to get wrong. Terraback already knows every resource's Terraform address and cloud ID from the scan (stored in the import/ directory), so it imports them all in one command.

The Fast Path

After a scan, run the provider import command against the output directory:

# 1. Scan and generate Terraform + import metadata
terraback scan all aws -r us-east-1 -o ./output

# 2. Initialize Terraform in the output directory
cd ./output
terraform init
cd ..

# 3. Import everything Terraback discovered
terraback aws import -o ./output

Terraback prints a summary of what it will import (grouped by resource type) and asks for confirmation before making any changes. Use --yes to skip the prompt in automation.

Import Methods

The --method (-m) flag selects how resources are imported:

MethodDescriptionSpeed
auto (default)Detect the best method based on your Terraform version-
bulkUses Terraform 1.5+ import blocks to import all resources in a single operationFastest: roughly 2-3 minutes for hundreds of resources
sequentialImports resources one by one; slower but avoids state-lock conflictsRoughly 4 seconds per resource
# Let Terraback pick the best method (recommended)
terraback aws import -o ./output --method auto

# Force bulk import (requires Terraform 1.5 or newer)
terraback aws import -o ./output --method bulk

# Force sequential import (most reliable, e.g. older Terraform)
terraback aws import -o ./output --method sequential

With auto, Terraback uses bulk when your installed Terraform supports import blocks (1.5+) and falls back to sequential otherwise.

Validating Before You Import

Use --plan-check to run a full terraform plan validation before any import happens. If the plan detects drift (resources that would be added, changed, or destroyed), Terraback aborts the import so you can review the generated code first:

# Validate with a plan; abort if drift is detected
terraback aws import -o ./output --plan-check

If you have reviewed the drift and want to proceed anyway, add --allow-drift:

# Proceed even though plan-check reported drift
terraback aws import -o ./output --plan-check --allow-drift

You can also preview drift earlier, right after the scan, with the --preview flag on scan all. It runs a terraform plan once the scan finishes so you can see how close the generated configuration is before importing:

terraback scan all aws -r us-east-1 --preview -o ./output

Options

OptionDescription
-o, --output-dir PATHDirectory containing the generated import files (default: generated)
-t, --terraform-dir PATHTerraform working directory (defaults to --output-dir)
-m, --method TEXTauto, bulk, or sequential (default: auto)
--plan-checkRun terraform plan validation before importing; aborts on drift
--allow-driftAllow import to proceed even when --plan-check detects drift
-y, --yesSkip the confirmation prompt
  1. Scan - terraback scan all <provider> -r <region> -o ./output
  2. Init - cd ./output && terraform init
  3. Review - inspect the generated .tf files
  4. Validate (optional) - terraback <provider> import -o ./output --plan-check
  5. Import - terraback <provider> import -o ./output
  6. Verify - terraform plan should show no (or only expected) changes

Troubleshooting

  • Resource already managed by Terraform - the resource is already in state. Remove it with terraform state rm <address> before re-importing, or skip it.
  • Bulk import not available - bulk requires Terraform 1.5+. Either upgrade Terraform or use --method sequential.
  • Plan-check reports drift - review the generated configuration. Drift usually means the generated .tf does not yet match the live resource exactly. Use --allow-drift to import anyway and refine the configuration afterwards.

See the CLI Reference for the full command listing and the Troubleshooting Guide for more help.