0
0
Terraformcloud~5 mins

Why importing existing resources matters in Terraform - Why It Works

Choose your learning style9 modes available
Introduction
When you start managing infrastructure with Terraform, you might already have resources created manually or by other tools. Importing existing resources lets Terraform know about these resources so it can manage them without recreating or deleting them.
When you want to start managing a cloud server that was created manually before using Terraform.
When you have a database instance created outside Terraform but want to include it in your Terraform setup.
When migrating infrastructure management from another tool to Terraform without downtime.
When you want to track and update existing network settings using Terraform.
When you want to avoid accidentally deleting resources that Terraform does not know about.
Commands
This command initializes the Terraform working directory. It downloads necessary provider plugins and prepares Terraform to work with your configuration.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command tells Terraform to link the existing AWS EC2 instance with ID i-0abcd1234efgh5678 to the Terraform resource named aws_instance.my_server. This way, Terraform knows this resource already exists and can manage it.
Terminal
terraform import aws_instance.my_server i-0abcd1234efgh5678
Expected OutputExpected
aws_instance.my_server: Importing from ID "i-0abcd1234efgh5678"... aws_instance.my_server: Import prepared! Prepared aws_instance.my_server for import aws_instance.my_server: Refreshing state... [id=i-0abcd1234efgh5678] Import successful!
This command shows what changes Terraform will make. After importing, it should show no changes if the configuration matches the existing resource.
Terminal
terraform plan
Expected OutputExpected
No changes. Infrastructure is up-to-date. This means that your Terraform configuration matches the imported resource.
Key Concept

If you remember nothing else from this pattern, remember: importing existing resources lets Terraform manage what you already have without breaking or recreating it.

Common Mistakes
Trying to import a resource without defining it in the Terraform configuration first.
Terraform needs to know the resource structure before it can link to an existing resource. Without a matching resource block, import fails.
Write the Terraform resource block with the correct type and name before running terraform import.
Importing a resource with the wrong ID or identifier.
Terraform cannot find the resource to import, causing errors or importing the wrong resource.
Double-check the exact resource ID from your cloud provider or infrastructure before importing.
Not running terraform plan after import to verify the state matches the configuration.
You might miss differences that could cause Terraform to change or destroy resources unexpectedly.
Always run terraform plan after import to confirm no unexpected changes.
Summary
Initialize Terraform with terraform init to prepare the environment.
Define the resource in your Terraform configuration before importing.
Use terraform import to link existing resources to Terraform state.
Run terraform plan to verify Terraform's view matches reality.