Import Existing Infrastructure
You'll learn how to bring pre-existing cloud resources under Terraform management without recreating them.
When you join a project or inherit infrastructure that wasn't originally built with Terraform, you don't have to rebuild everything from scratch. Terraform's import capability lets you take resources that already exist in your cloud provider and add them to your Terraform state, allowing you to manage them going forward.
Why Import Matters
Think of importing like adopting a pet that already has a name and personality—you're not creating something new, you're taking responsibility for something that already exists. The resource continues living its life, but now you're officially managing it.
How Import Works
The terraform import command connects an existing resource to a resource block in your configuration. You provide two things:
- The resource address in your Terraform config (e.g.,
aws_instance.my_server) - The resource's actual ID in your cloud provider (e.g.,
i-1234567890abcdef0)
terraform import aws_instance.my_server i-1234567890abcdef0
This adds the resource to your state file, but here's the catch: it doesn't automatically generate the configuration. You must manually write the resource block that matches the imported resource's actual settings, or use tools that help generate configuration from state.
The Two-Step Dance
- Write a matching resource block (even if minimal)
- Run
terraform importto link it to state - Run
terraform planto verify your configuration matches reality - Refine your configuration until plan shows no changes
Key Takeaway: Importing lets you adopt existing infrastructure into Terraform state, but you're responsible for writing configuration that accurately reflects those resources—import handles state, you handle code.