Importing Existing Resources into Terraform
Scenario
Your team created S3 buckets via the AWS console before adopting Terraform. Now you want to bring them under Terraform management without deleting and recreating them. This is importing.
Your Objectives
- Use the import block (Terraform 1.5+) to import a resource declaratively.
- Understand the legacy terraform import CLI command.
- Use terraform plan -generate-config-out to auto-generate HCL.
- Verify that after import, terraform plan shows no changes.
Additional Context
Import block (1.5+): declarative, added to your config. The recommended approach. Legacy CLI: terraform import aws_s3_bucket.name bucket-id — imperative. -generate-config-out auto-generates the resource block from the cloud resource.
Step 1: Create a bucket outside Terraform
aws s3 mb s3://my-existing-bucket-to-import-123456main.tf (with import block)
terraform {
required_version = ">= 1.5.0"
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
}
}
provider "aws" { region = "us-east-1" }
# IMPORT BLOCK — tells Terraform this resource already exists
import {
to = aws_s3_bucket.imported
id = "my-existing-bucket-to-import-123456"
}
resource "aws_s3_bucket" "imported" {
bucket = "my-existing-bucket-to-import-123456"
tags = { ManagedBy = "terraform", Imported = "true" }
}Workflow Commands
terraform init
# Plan shows the import
terraform plan
# Plan: 0 to add, 0 to change, 0 to destroy, 1 to import.
terraform apply # imports into state
# Auto-generate config: terraform plan -generate-config-out=generated.tf
# Legacy CLI: terraform import aws_s3_bucket.imported my-existing-bucket-to-import-123456
terraform destroy
✓ Import Plan Output
aws_s3_bucket.imported: Preparing import... [id=my-existing-bucket-to-import-123456] aws_s3_bucket.imported: Refreshing state... Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.

