Guide · Ilia Dubovskii · 27 November 2026

Import existing AWS resources into Terraform: find, import, stay at 100%

To import existing AWS resources into Terraform, write an import block with the resource address and its AWS ID, run terraform plan -generate-config-out=generated.tf to get a first draft of the resource code, clean that code, then run terraform apply. Import blocks work in Terraform 1.5 and later and in OpenTofu. The old terraform import command still works, but it writes to state straight away with no plan to review. Before you import anything, list what is in the account and what your state files already manage, so you know what is missing.

The short version

1. The short answer: import blocks versus terraform import

There are two ways to bring an existing resource into state.

import block terraform import command
Where it livesIn your .tf files, reviewed in a pull requestOn the command line
Shows in terraform planYes, as "will be imported"No, it writes to state at once
Can generate the resource codeYes, with -generate-config-outNo
Many resources at oncefor_each in the blockOne command per resource
Terraform version1.5 and laterAll versions

Versions that matter, from the Terraform changelogs:

The latest Terraform release is v1.17.2. OpenTofu supports import blocks, id or identity, for_each, and tofu plan -generate-config-out. One difference: OpenTofu's docs say config generation does not work for import blocks that use for_each.

Config generation is still marked experimental in both tools. The Terraform docs call the output "Terraform's best guess" and expect you to edit it.

2. Step 1: find unmanaged resources in Terraform

You cannot import what you have not found. The method is a set difference: everything that exists, minus everything any state file manages.

  1. List every live resource. Turn on AWS Resource Explorer and make one Region the aggregator index, so one view covers all Regions. Resource Explorer is offered at no additional charge. Use list-resources, not search: the search command returns only the first 1,000 results.
# Read-only. Run in the Region that holds the aggregator index.
aws resource-explorer-2 list-resources \
  --query 'Resources[].Arn' --output text \
  | tr '\t' '\n' | sort -u > live.txt
  1. List what every state manages. terraform state list gives addresses such as aws_instance.foo, but you need ARNs and IDs to compare. terraform show -json prints the state with every attribute. Run it in every root module and every workspace.
# Read-only for infrastructure. "workspace select" only changes your local selection.
for ws in $(terraform workspace list | tr -d '* '); do
  terraform workspace select "$ws" >/dev/null
  terraform show -json \
    | jq -r '.. | objects | select(.mode? == "managed") | .values.arn // .values.id'
done | sort -u >> managed.txt
  1. Compare and count.
sort -u -o managed.txt managed.txt
comm -23 live.txt managed.txt > unmanaged.txt
echo "coverage: $(comm -12 live.txt managed.txt | wc -l) of $(wc -l < live.txt)"

That last line is your IaC coverage: the share of live resources that some state file manages. Expect noise. Resource Explorer only indexes supported resource types, and it lists things you may never want in code, such as default VPCs or resources that other AWS services create for you. Agree on an exclusion list once and apply it every time, or the number will move for no reason.

If you already pay for AWS Config, its select-resource-config command answers the same question with SQL. Config is billed per configuration item recorded; the pricing page example uses $0.003 per continuous configuration item in US East (N. Virginia).

3. Step 2: import one resource with an import block and generated config

Start with one security group and one S3 bucket. The AWS provider docs give the import ID for each: the security group ID for aws_security_group and the bucket name for aws_s3_bucket.

  1. Write the import blocks in imports.tf:
import {
  to = aws_security_group.web
  id = "sg-0123456789abcdef0"
}

import {
  to = aws_s3_bucket.logs
  id = "example-logs-bucket"
}
  1. Generate the code. The file must not exist yet, or Terraform stops with an error.
# Read-only: plans the import and writes generated.tf locally.
terraform plan -generate-config-out=generated.tf
  1. Clean the generated code. This is the real work:
  1. Plan again until the summary says N to import, 0 to add, 0 to change, 0 to destroy. Any change or destroy means the code does not yet match reality.
  1. Back up the state, then apply. This writes to state (not to the AWS resources):
# Read-only: saves a copy of the current state before the import.
terraform state pull > backup-before-import.tfstate

# STATE-CHANGING: records the imported resources in state.
terraform apply

To roll back an import, remove the address from state. This does not delete or change the AWS resource:

# STATE-CHANGING: Terraform forgets the resource; the resource itself is left alone.
terraform state rm aws_security_group.web aws_s3_bucket.logs

You can delete the import blocks afterwards or keep them as a record. Both Terraform and OpenTofu treat an import block for an address already in state as a no-op.

4. Step 3: terraform import all resources, in batches

Importing everything in one pull request produces a plan nobody can review. Work in batches.

Use for_each for groups of the same type. From the import block reference:

locals {
  buckets = {
    staging = "bucket1"
    uat     = "bucket2"
    prod    = "bucket3"
  }
}

import {
  for_each = local.buckets
  to       = aws_s3_bucket.this[each.key]
  id       = each.value
}

resource "aws_s3_bucket" "this" {
  for_each = local.buckets
}

If you need generated code first, generate it with one plain import block per resource, then fold the result into a for_each resource. OpenTofu cannot generate code for for_each import blocks.

Use terraform query to discover and generate in bulk (Terraform 1.14+). Put list blocks in a .tfquery.hcl file. The AWS provider documents list resources for many types, including aws_s3_bucket, aws_security_group and aws_instance. Each list block returns up to 100 results unless you set limit (bulk import docs).

# main.tfquery.hcl
list "aws_s3_bucket" "all" {
  provider = aws
  limit    = 500
}
# Read-only: prints what it found, then writes import and resource blocks locally.
terraform query
terraform query -generate-config-out=generated.tf

Group by owner, not by type. Put each resource where it will live: the module and repository of the team that changes it. A batch of "all security groups" across ten services leaves you moving code twice.

Order matters. Import foundations first, so later batches can reference them instead of hard-coding IDs:

  1. Networking: VPCs, subnets, route tables, security groups.
  2. IAM roles and policies, KMS keys.
  3. Data stores: S3, RDS, DynamoDB.
  4. Compute and services: instances, Lambda, ECS, load balancers.
  5. Everything else: alarms, log groups, DNS records.

Keep plans reviewable. Aim for a batch small enough that a reviewer can read the plan, and require 0 to change, 0 to destroy on every import pull request.

5. Terraform import tools and Terraformer alternatives

Tool What it does What it does not do
TerraformerGenerated Terraform files and state from existing resourcesArchived on 16 March 2026: "no longer maintained and is deprecated"
Former2Scans an AWS account from your browser and generates CloudFormation, Terraform and other outputsDoes not write state or measure how much is managed; some services need its browser extension
CloudFormation IaC generatorScans a Region for resources not managed by a stack; up to 500 resources per templateProduces CloudFormation (or a CDK app), not Terraform
Console-to-CodeRecords console actions in supported services (EC2, VPC, RDS, S3, Lambda and others) and suggests codeOutputs CDK and CloudFormation, not Terraform
Terraform query and -generate-config-outBuilt-in discovery and code draftsOnly for types the provider supports; output needs cleaning

For measuring coverage rather than generating code: driftctl is free but in maintenance mode, with its last release in 2023. Firefly reports the percentage of assets managed by IaC on AWS, Azure and Google Cloud. env zero Cloud Compass shows the IaC-managed share on AWS and Google Cloud.

6. Keeping it at 100%: drift detection and new unmanaged resources

Importing once is not enough. Two things undo it: changes to managed resources made outside Terraform (drift), and new resources created outside Terraform (new unmanaged resources).

  1. Terraform drift detection in CI. Run a scheduled plan in every root module. With -detailed-exitcode, exit code 0 means no changes, 1 means error, 2 means the plan has changes.
# Read-only. Fails the job when live state differs from code.
terraform plan -detailed-exitcode -input=false -lock=false
  1. Block console changes. A service control policy can deny write actions unless the caller is your CI role, using the aws:PrincipalArn condition key. Keep a break-glass role in the exception list. SCPs do not affect the management account or service-linked roles, so keep workloads out of the management account.
  1. Run the Step 1 diff every week. A new line in unmanaged.txt is a resource someone created by hand. Import it, or delete it.

7. Have it done for you

Getting to full coverage is a long series of small, careful pull requests, and staying there means watching for drift and new resources every week. The IaC coverage subscription is a subscription to that result: every resource in the agreed accounts brought under Terraform or OpenTofu and kept there. The target is 100%, guaranteed above 97%, measured by Compass or by a tool you prefer, agreed at the start. A month that ends at 97% or below is not billed, and neither is the ramp-up. Imports and drift fixes arrive as pull requests in your repositories. It costs $920 per account per month and covers AWS, Azure and Google Cloud. A free coverage measurement shows your starting number.

Prefer to do it yourself? Compass is open source (Apache 2.0): it compares live AWS resources with Terraform state and opens import pull requests: https://github.com/beamreach-ai/compass . AWS only.

8. Sources

9. FAQ

How do I import existing AWS resources into Terraform state?

Add an import block with to (the resource address) and id (the AWS ID the provider expects, such as sg-... for a security group or the bucket name for S3). Write or generate a matching resource block, run terraform plan to check it shows only imports, then terraform apply. On Terraform older than 1.5, use terraform import ADDRESS ID.

Can terraform import all resources in an AWS account at once?

Not in one command. Terraform 1.14 and later can discover resources with terraform query and generate import code for every result, but you still review, place and clean that code. In practice you import in batches by owner and dependency order.

How do I find unmanaged resources in Terraform?

List every live resource (AWS Resource Explorer list-resources, or AWS Config), list every ARN and ID in all your state files with terraform show -json, and take the difference. What is left is unmanaged.

What is a good Terraformer alternative now that it is archived?

For most teams, Terraform's own import blocks with -generate-config-out, plus terraform query on 1.14 and later. Former2 is not archived (its last commit was in July 2026) and generates Terraform from a browser scan of an AWS account.

Can AWS console to code generate Terraform?

No. Console-to-Code outputs AWS CDK (Java, Python, TypeScript) and CloudFormation (JSON, YAML). The CloudFormation IaC generator also produces CloudFormation. For Terraform, use import blocks with generated config.

How does Terraform drift detection work?

terraform plan refreshes state from AWS and compares it with your code. Run it on a schedule with -detailed-exitcode: exit code 2 means something changed outside Terraform. It only sees resources already in state, so pair it with a check for new unmanaged resources.

What is IaC coverage?

The share of live cloud resources managed by infrastructure as code: matched resources divided by all resources, after an agreed exclusion list. Measure it the same way every time so the number means something.

Last checked: 27 November 2026. Written from the provider's documentation and pricing pages, linked below. Prices are list prices; confirm against your own bill.

Related: IaC coverage subscription · IaC coverage and drift detection · IaC Watch: Terraform releases