Unified, automated, and ready to turn data into intelligence.
Discover how to unlock the true value of your data.
March 16-19 | Booth #935
San Jose McEnery Convention Center
Terraform import is a crucial command in the Terraform toolkit. It allows users to bring existing infrastructure under Terraform management, which is essential for integrating Terraform into existing environments without needing to rebuild infrastructure from scratch.
In this article, we’ll take an in-depth look at Terraform import, its significance, and how to use it effectively to automate and improve the consistency of your infrastructure management.
Terraform import allows you to take resources that were created outside of Terraform and bring them under its management. This process involves mapping the existing infrastructure resources to corresponding Terraform configurations. By doing this, Terraform can track the state of these resources, including their attributes and relationships, and manage them alongside resources explicitly defined in your Terraform code.
This process offers various benefits, including:
Terraform import allows you to bring existing infrastructure under Terraform management without having to recreate resources. This short tutorial will guide you through the process of using Terraform import to manage an existing AWS EC2 instance. By the end of the guide, you'll understand how to import resources into Terraform and manage them effectively.
Before starting, ensure you have the following:
First, create a directory for your Terraform project and navigate into it. Then, create a new file named main.tf:
$ mkdir terraform-import-tutorial
$ cd terraform-import-tutorial
$ touch main.tf
In the main.tf file, define the provider and the resource you plan to import. For this tutorial, we'll use an AWS EC2 instance.
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
# The configuration will be filled in after importing the instance.
}
Before using Terraform, initialize your working directory with the necessary plugins:
$ terraform init
Locate the EC2 instance ID that you want to import. You can find this in the AWS Management Console or by using the AWS CLI. For example, let's assume the instance ID is i-1234567890abcdef0.
Use the terraform import command to import the existing EC2 instance into Terraform. Run the following command in your terminal:
$ terraform import aws_instance.example i-1234567890abcdef0
This command tells Terraform to associate the resource aws_instance.example in your configuration with the existing EC2 instance i-1234567890abcdef0.
After the import, Terraform will update the state file with the information about the imported resource. However, your main.tf file will still be incomplete. To fill in the configuration, use the terraform show command to inspect the imported resource and update main.tf accordingly:
$ terraform show
You will see the imported resource's details. Update main.tf to match the resource's actual configuration. Here's an example of what it might look like:
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Replace with your instance's AMI ID
instance_type = "t2.micro" # Replace with your instance type
key_name = "my-key-pair" # Replace with your key pair name
# Additional attributes as per the `terraform show` output
}
Now that your configuration file accurately reflects the imported resource, you can manage it with Terraform. Run terraform plan to see the planned actions:
$ terraform plan
If everything looks good, apply the changes:
$ terraform apply
You can now manage your imported resource using Terraform. For example, you can update the instance type or other attributes in main.tf and apply the changes with terraform apply.
While Terraform import offers a powerful way to manage existing infrastructure, there are some key considerations to keep in mind:
Following these best practices ensures you effectively manage your imported resources alongside those defined in Terraform code:
Terraform import bridges the gap between existing infrastructure and Terraform's management power. It enables you to seamlessly integrate your entire infrastructure stack, regardless of creation method. This unified approach reduces complexity, improves visibility, and streamlines infrastructure management.
As your infrastructure evolves, storage becomes a critical consideration. Explore Everpure solutions, like Portworx® for containerized workloads and Everpure Cloud Dedicated for cloud deployments, to optimise storage management alongside Terraform. This combination ensures efficient, scalable infrastructure for your modern applications.
Mark your calendars. Registration opens in February.
Access on-demand videos and demos to see what Everpure can do.
Charlie Giancarlo on why managing data—not storage—is the future. Discover how a unified approach transforms enterprise IT operations.
Modern workloads demand AI-ready speed, security, and scale. Is your stack ready?