Skip to main content

Terraform Import

Imagine you’ve already built part of your Lego city manually. Maybe a hospital or a park and now you want Terraform to manage it along with the rest of the city.

Instead of tearing it down and rebuilding, Terraform lets you import existing resources into its state. This way, Terraform becomes aware of resources that were created outside of it, and you can start managing them with code.


Key Concepts

1. What is Terraform Import?

  • Definition: Import allows Terraform to bring existing resources into its state file.
  • Purpose: Align manually created or externally managed resources with Terraform’s configuration.

Syntax:

terraform import <resource_type>.<name> <resource_id>

2. Example: Importing an AWS Instance

  1. You already created an EC2 instance manually in AWS.
  2. Terraform updates its state to include the instance.

Import the existing instance:

terraform import aws_instance.example i-1234567890abcdef0

Define the resource in Terraform:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

3. Example: Importing a VPC

terraform import aws_vpc.main vpc-0a1b2c3d4e5f6g7h
  • Terraform now manages the VPC with the configuration you define.

4. Limitations of Import

  • Import only updates the state file, not the configuration.
  • You must manually write the .tf code to match the resource.
  • If configuration doesn’t match the actual resource, Terraform may plan changes.
  • Not all providers/resources support import.

Hands‑On Lab / Demo

Lab: Importing an Existing Resource

  1. Create an S3 bucket manually in AWS.
  2. Run terraform plan → Terraform now manages the bucket.

Import the bucket:

terraform import aws_s3_bucket.mybucket my-existing-bucket

Define it in Terraform:

resource "aws_s3_bucket" "mybucket" {
  bucket = "my-existing-bucket"
}

Pro Tips & Best Practices

  • Always write .tf configuration before importing.
  • Verify resource IDs carefully.
  • Run terraform plan after import to check alignment.
  • Document imported resources for your team.
  • Consider refactoring manually created infra into Terraform modules after import.

Summary & Cheatsheet

  • Import = Bring existing resources into Terraform state.
  • Syntax: terraform import <resource_type>.<name> <resource_id>
  • Limitations: Updates state only, config must be written manually.
  • Best practice: Align .tf code with actual resource before import.
Quick mnemonic: Import = Adopt, Not Create

The Hackers Notebook

Terraform import is the bridge between manual infrastructure and Infrastructure as Code. It lets you bring existing resources under Terraform’s management, ensuring consistency and control. But remember: import only updates the state and you must still write the configuration.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 31, 2025