0
0
Terraformcloud~5 mins

Terraform import command - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you have resources created outside Terraform and want to manage them with Terraform. The terraform import command lets you bring those existing resources into Terraform's control without recreating them.
When you have a cloud server created manually and want Terraform to manage it.
When you want to track a database instance created by another team using Terraform.
When migrating existing infrastructure to Terraform for easier updates and version control.
When you accidentally created a resource outside Terraform and want to avoid deleting it.
When you want to add a resource created by a different tool into your Terraform state.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  # The instance will be imported, so no need to define all attributes here
  # After import, you can add full configuration
}

This file sets up the AWS provider for Terraform to connect to AWS in the us-east-1 region.

The resource block defines an AWS EC2 instance named example. It is initially empty because the instance already exists and will be imported.

Commands
This command initializes the Terraform working directory, downloads provider plugins, and prepares Terraform to run.
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! You may now begin working with Terraform. Try running "terraform plan" to see any changes required for your infrastructure.
This command imports the existing AWS EC2 instance with ID i-0abcd1234efgh5678 into the Terraform resource aws_instance.example.
Terminal
terraform import aws_instance.example i-0abcd1234efgh5678
Expected OutputExpected
aws_instance.example: Importing from ID "i-0abcd1234efgh5678"... aws_instance.example: Import prepared! Prepared aws_instance for import aws_instance.example: Refreshing state... [id=i-0abcd1234efgh5678] Import successful! The resources that were imported are now in your Terraform state and will be managed by Terraform.
This command shows the changes Terraform will make. After import, it helps verify that Terraform matches the existing resource.
Terminal
terraform plan
Expected OutputExpected
No changes. Infrastructure is up-to-date. This means that Terraform's state matches the actual resource and no updates are needed.
Key Concept

If you remember nothing else from this pattern, remember: terraform import connects existing resources to Terraform's management without recreating them.

Common Mistakes
Trying to import a resource without defining a matching resource block in the Terraform configuration.
Terraform needs a resource block to map the imported resource to; without it, import fails.
First define the resource block in your .tf file with the correct resource type and name, then run terraform import.
Using the wrong resource ID or identifier when running terraform import.
Terraform cannot find the resource to import and the command fails.
Double-check the exact resource ID from your cloud provider or system before running terraform import.
Not running terraform init before terraform import.
Terraform is not initialized and does not have the necessary plugins to communicate with the provider.
Always run terraform init first to set up the working directory.
Summary
terraform init prepares Terraform to work with your cloud provider.
terraform import brings an existing resource into Terraform's state for management.
terraform plan verifies that Terraform's state matches the actual resource after import.