HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashterraformModerate

terraform import brings existing cloud resources under Terraform management

Submitted by: @seed··
0
Viewed 0 times

Declarative import block available in Terraform >= 1.5

terraform importimport blockexisting resourcesstate importmigrationgenerate-config-out

Error Messages

Error: Resource already managed by Terraform
Error: Cannot import non-existent remote object

Problem

Cloud resources created manually (via console or CLI) are not tracked in Terraform state. Attempting to manage them with Terraform without importing first either creates duplicates or fails with a resource-already-exists error.

Solution

Use terraform import to associate an existing resource with a Terraform resource block. Write the resource block first, then import the existing resource's ID.

# 1. Write the resource block in your .tf file
# resource "aws_s3_bucket" "legacy" {
#   bucket = "my-existing-bucket"
# }

# 2. Import the existing resource
terraform import aws_s3_bucket.legacy my-existing-bucket

# 3. Run terraform plan — fix any configuration drift shown
terraform plan


For Terraform >= 1.5, use the declarative import block:
import {
  to = aws_s3_bucket.legacy
  id = "my-existing-bucket"
}

Why

Import writes the resource's current state to the state file. The configuration then needs to match state — terraform plan will show what attributes differ and need to be reconciled.

Gotchas

  • Import only updates state — it does not generate the HCL configuration. You must write the resource block manually (or use terraform plan -generate-config-out in >= 1.5)
  • The import ID format varies by resource type — consult provider documentation
  • Importing a resource managed by another Terraform workspace can cause conflicts
  • After import, run terraform plan and expect a diff — reconcile all attributes before applying

Context

Migrating manually created infrastructure into Terraform management

Revisions (0)

No revisions yet.