Skip to main content
In this guide, we will explain how to import existing infrastructure into your Terraform configuration using the Terraform import command. Typically, you create and manage resources with Terraform. However, in many real-world projects, some resources might be provisioned with tools like the AWS Management Console or Ansible. Importing these resources into Terraform helps streamline provisioning, updates, and deletion. For instance, consider the following diagram illustrating AWS services managed by Terraform, Ansible, and the AWS Management Console:
The image shows AWS services managed by Terraform, Ansible, and AWS Management Console, including EC2, DynamoDB, S3, Route 53, Elastic Block Store, and VPC.
This diagram sets the stage for your inquiry: How can you bring externally created resources under Terraform’s direct management?

Accessing Existing Resources Using Data Sources

Initially, you might leverage data sources in Terraform to fetch details from resources not currently managed by your configuration. Data sources allow you to read attributes and integrate existing infrastructure into your workflow without enabling Terraform to update or delete these resources. For example, the configuration below reads attributes of an existing AWS instance using its instance ID:
When you run:
Notice that while Terraform outputs the instance’s public IP, it remains unmanaged by Terraform since it’s accessed as a data source.

Importing an Existing Resource into Terraform

To fully control an existing resource, you need to import it into Terraform’s state. The syntax for the import command is as follows:
Before running the import command, ensure that the corresponding configuration exists. If the resource block isn’t defined, Terraform will return an error.
If the resource configuration hasn’t been created, you might see an error like:
Terraform import only updates the state file and does not alter configuration files. Hence, ensure that you create an appropriate resource block beforehand.

Step 1: Create an Empty Resource Block

Begin by defining an empty resource block in your configuration file:

Step 2: Run the Import Command

After the empty block is defined, run the import command again. It should output something similar to:
The command imports the resource into your Terraform state file, allowing Terraform to manage it moving forward.

Step 3: Complete the Resource Configuration

Next, update the resource block with the necessary configurations. You can retrieve the required attribute values from the AWS Management Console or by inspecting the state file. For example, update the resource configuration as follows:
Running a Terraform plan now ensures that your configuration matches the imported infrastructure:
This output confirms that Terraform has successfully imported the resource. Any future changes to the infrastructure can be managed by modifying this configuration and following the standard Terraform workflow: init, plan, and apply.

Next Steps

Proceed to the hands-on labs to practice using the Terraform import command, and continue streamlining your infrastructure management by bringing all resources under Terraform control. For further information, check out:

Watch Video

Practice Lab