The short version
- Use
importblocks, notterraform import. They show up interraform plan, so a reviewer sees the import before state changes. - Let Terraform draft the code with
terraform plan -generate-config-out=generated.tf, then edit it until the plan says0 to change. - Find what is unmanaged first: AWS Resource Explorer lists live resources at no additional charge;
terraform show -jsonlists what each state manages. The difference is your import list. - For many resources, use
for_eachin import blocks (Terraform 1.7+) orterraform querywithlistblocks (Terraform 1.14+). - Keep it at 100% with a scheduled
terraform plan -detailed-exitcode(exit code 2 means drift) and a weekly coverage diff.
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 lives | In your .tf files, reviewed in a pull request | On the command line |
Shows in terraform plan | Yes, as "will be imported" | No, it writes to state at once |
| Can generate the resource code | Yes, with -generate-config-out | No |
| Many resources at once | for_each in the block | One command per resource |
| Terraform version | 1.5 and later | All versions |
Versions that matter, from the Terraform changelogs:
- 1.5:
importblocks andterraform plan -generate-config-out=PATH. - 1.6: the
idfield accepts expressions, as long as the value is known at plan time (1.6 changelog). - 1.7:
for_eachin import blocks (1.7 changelog). - 1.12: the
identityargument, an object instead of a single ID string. The AWS provider docs show it foraws_s3_bucketandaws_security_group. - 1.14:
terraform queryandlistblocks in.tfquery.hclfiles, to search for existing resources and generate import code in bulk (1.14 changelog).
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.
- 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, notsearch: thesearchcommand 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
- List what every state manages.
terraform state listgives addresses such asaws_instance.foo, but you need ARNs and IDs to compare.terraform show -jsonprints 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
- 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.
- 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"
}
- 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
- Clean the generated code. This is the real work:
- Hard-coded IDs. Replace literal
vpc_id, subnet IDs, KMS key ARNs and account IDs with references to other resources, or with variables if they live elsewhere. - Defaults. Remove arguments that just repeat the provider default. Keep the ones that differ, or the next plan will try to change the resource.
- Security group rules. The provider docs advise against inline
ingressandegressblocks and warn against mixing them with separate rule resources. Move each rule intoaws_vpc_security_group_ingress_ruleoraws_vpc_security_group_egress_ruleand import it by itssgr-rule ID. - S3 sub-resources. Arguments such as
versioning,server_side_encryption_configurationandpolicyare marked deprecated onaws_s3_bucket. Useaws_s3_bucket_versioning,aws_s3_bucket_server_side_encryption_configuration,aws_s3_bucket_public_access_blockandaws_s3_bucket_policy. Each of these imports with the bucket name as its ID. - Conflicts. For some resources the generated file contains two arguments that cannot be set together. The docs show
ipv6_address_countagainstipv6_addressesonaws_instance. Remove one and plan again.
- 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.
- 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:
- Networking: VPCs, subnets, route tables, security groups.
- IAM roles and policies, KMS keys.
- Data stores: S3, RDS, DynamoDB.
- Compute and services: instances, Lambda, ECS, load balancers.
- 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 |
|---|---|---|
| Terraformer | Generated Terraform files and state from existing resources | Archived on 16 March 2026: "no longer maintained and is deprecated" |
| Former2 | Scans an AWS account from your browser and generates CloudFormation, Terraform and other outputs | Does not write state or measure how much is managed; some services need its browser extension |
| CloudFormation IaC generator | Scans a Region for resources not managed by a stack; up to 500 resources per template | Produces CloudFormation (or a CDK app), not Terraform |
| Console-to-Code | Records console actions in supported services (EC2, VPC, RDS, S3, Lambda and others) and suggests code | Outputs CDK and CloudFormation, not Terraform |
Terraform query and -generate-config-out | Built-in discovery and code drafts | Only 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).
- 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
- Block console changes. A service control policy can deny write actions unless the caller is your CI role, using the
aws:PrincipalArncondition 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.
- Run the Step 1 diff every week. A new line in
unmanaged.txtis 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
- Terraform: Import overview, Import a single resource, Generating configuration, Import in bulk, import block reference, terraform import, terraform plan, terraform state list, terraform show
- Terraform changelogs: 1.5.0, 1.6.0, 1.7.0, 1.14.0, releases
- OpenTofu: Import, Generating configuration
- AWS provider: aws_security_group, aws_s3_bucket, list resources in the provider repository
- AWS: Resource Explorer pricing, aggregator index, supported resource types, list-resources, search, AWS Config pricing, select-resource-config, CloudFormation IaC generator, Console-to-Code, SCPs, IAM condition keys
- Tools: Terraformer, Former2, driftctl, Firefly terminology, env zero Cloud Compass
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
Beamreach