Course contentsShow
← Cloud and Infrastructure
Lesson 826 of 2,98915. Terraform in PracticeFree lesson

Import Existing Infrastructure

Learn to import existing resources into Terraform state and generate configuration from existing infrastructure.

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:

  1. The resource address in your Terraform config (e.g., aws_instance.my_server)
  2. 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

  1. Write a matching resource block (even if minimal)
  2. Run terraform import to link it to state
  3. Run terraform plan to verify your configuration matches reality
  4. 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.