Terraform Import (Part-16)

Terraform Import

Terraform Import is a command used to bring existing infrastructure that was not originally managed by Terraform under Terraform’s management. This is useful for integrating existing resources into your Terraform configuration without having to recreate them.

When to Use Terraform Import

  • Integrating Existing Resources: When you have resources created manually or by other tools that you now want to manage with Terraform.
  • Migrating to Terraform: When moving from another infrastructure management tool to Terraform and you want to start managing your existing resources with Terraform.
  • Recovering State: When the Terraform state file is lost or corrupted, and you need to recreate the state.

How Terraform Import Works

The terraform import command allows you to import existing resources into your Terraform state. The process involves:

  1. Identifying the resource in your infrastructure.
  2. Writing the corresponding Terraform configuration.
  3. Running the terraform import command to link the existing resource to the Terraform configuration.

Steps to Import Resources

Step 1: Write the Terraform Configuration

Before importing a resource, you need to define the resource in your Terraform configuration. This configuration should match the existing resource.

For example, if you have an existing AWS EC2 instance, you would write:

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  # Configuration will be filled after import
}

Step 2: Run the Import Command

Use the terraform import command to import the existing resource into Terraform’s state. You need to specify the resource identifier (resource type and name) and the resource ID.

For example, to import an existing AWS EC2 instance with ID i-1234567890abcdef0:

terraform import aws_instance.example i-1234567890abcdef0

Step 3: Update the Configuration with Actual Attributes

After the resource is imported, Terraform will update the state file, but the configuration file still needs the actual attributes to match the imported resource. You can use terraform show to get the current state of the resource and update the configuration file accordingly.

For example, update the aws_instance configuration with the attributes:

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  subnet_id     = "subnet-0bb1c79de3EXAMPLE"
  tags = {
    Name = "example-instance"
  }
}

Example: Importing an AWS S3 Bucket

Step 1: Write the Configuration

Create a configuration for the S3 bucket:

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  # Configuration will be filled after import
}

Step 2: Run the Import Command

Import the existing S3 bucket with the name example-bucket:

terraform import aws_s3_bucket.example example-bucket

Step 3: Update the Configuration with Actual Attributes

After importing, update the configuration with the actual attributes of the S3 bucket:

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "example-bucket"
  acl    = "private"

  tags = {
    Name        = "example-bucket"
    Environment = "Dev"
  }
}

Common Import Scenarios

AWS EC2 Instance:

terraform import aws_instance.example i-1234567890abcdef0

AWS S3 Bucket:

terraform import aws_s3_bucket.example example-bucket

AWS VPC:

terraform import aws_vpc.example vpc-12345678

Google Cloud Storage Bucket:

terraform import google_storage_bucket.example my-bucket

Limitations of Terraform Import

  • State Only: Importing resources only updates the state file; it does not update the configuration file. You must manually update the configuration file to match the imported state.
  • Single Resource: The terraform import command imports one resource at a time. You need to run the command multiple times to import multiple resources.
  • Complex Resources: Some resources with complex relationships or dependencies might require additional steps to import correctly.

Related Posts

Terraform Cheat Sheet: The Ultimate Reference for Infrastructure as Code (2025)

🧾 Terraform Cheat Sheet: The Ultimate Reference for Infrastructure as Code (2025) Learn and master Terraform with this complete cheat sheet β€” from basics to advanced automation….

Read More

Terraform Explained Through a Simple Story – From Manual Chaos to Automated Peace

🌍 Terraform Explained Through a Simple Story – From Manual Chaos to Automated Peace πŸšͺ Welcome to DevTown – The City of Applications Imagine a bustling city…

Read More

Terraform from Essentials to Advanced: The Complete Guide (2025)

🌍 Terraform from Essentials to Advanced: The Complete Guide (2025) Build, manage, and scale infrastructure like code – cloud-native, multi-cloud, and secure. πŸš€ What is Terraform? Terraform…

Read More

Terraform interview questions that are commonly asked in interviews

Basic Questions Intermediate Questions Advanced Questions Scenario-Based Questions Advanced and Scenario-Based Questions Mohammad Gufran Jahangir

Read More

Ultimate guide for terraform from basic to advance

What is the need of terraform IAAC? The need for Terraform Infrastructure as Code (IaAC) is driven by several key benefits: Summary: List of IaaC tools Here…

Read More

Terraform Pipeline (Part-18)

Terraform Pipeline A Terraform pipeline automates the process of planning, applying, and managing infrastructure as code (IaC) with Terraform. By integrating Terraform with a CI/CD pipeline, you…

Read More
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x