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:
- Identifying the resource in your infrastructure.
- Writing the corresponding Terraform configuration.
- 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.