0
0
Terraformcloud~5 mins

Import state verification in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you create resources outside Terraform and want Terraform to manage them. Importing lets Terraform track these resources. Import state verification checks that Terraform knows about the resource correctly.
When you have a cloud server created manually and want Terraform to manage it.
When you moved resources from one Terraform project to another and need to sync state.
When you want to avoid recreating existing resources by importing them into Terraform.
When you want to confirm Terraform's state matches the real resource after import.
When you want to fix Terraform state after manual changes outside Terraform.
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 set all attributes here
  # Just define the resource block with the correct name
}

This file sets the AWS provider region and declares an AWS EC2 instance resource named "example". The resource block is empty because the instance already exists and will be imported. Terraform needs this block to link the imported resource to the configuration.

Commands
Initializes Terraform in the current directory to download providers and prepare the working directory.
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.
Imports the existing AWS EC2 instance with ID i-0abcd1234efgh5678 into Terraform state under the resource name 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.
Shows the difference between the current Terraform state and the configuration to verify the import was successful and no changes are pending.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... No changes. Infrastructure is up-to-date. This means that Terraform did not detect any differences between your configuration and real physical resources that exist.
Key Concept

If you remember nothing else from this pattern, remember: terraform import links existing resources to Terraform state so Terraform can manage them.

Common Mistakes
Running terraform import without defining the resource block in the configuration file.
Terraform needs the resource block to know how to manage the imported resource; without it, import fails or state is incomplete.
Always declare the resource block with the correct resource type and name before running terraform import.
Using the wrong resource ID when running terraform import.
Terraform cannot find the resource to import, causing an error or importing the wrong resource.
Verify the exact resource ID from your cloud provider before importing.
Not running terraform plan after import to verify the state.
You might miss configuration mismatches or pending changes that could cause resource replacement or errors later.
Always run terraform plan after import to confirm the state matches the configuration.
Summary
Initialize Terraform with terraform init to prepare the working directory.
Declare the resource block in main.tf matching the resource you want to import.
Run terraform import with the correct resource ID to link the existing resource to Terraform state.
Run terraform plan to verify Terraform state matches the real resource and no changes are pending.