Skip to content

Import Terraform configuration

Overview

In this section, you will import an existing Docker container and image into an empty Terraform workspace. By doing so, you will learn strategies and considerations for importing real-world infrastructure into Terraform.

The default Terraform workflow involves creating and managing infrastructure entirely with Terraform.

Write a Terraform configuration that defines the infrastructure you want to create.

Review the Terraform plan to ensure that the configuration will result in the expected state and infrastructure.

Apply the configuration to create your Terraform state and infrastructure.

Terraform workflow diagram

After you create infrastructure with Terraform, you can update the configuration and plan and apply those changes. Eventually you will use Terraform to destroy the infrastructure when it is no longer needed. This workflow assumes that Terraform will create an entirely new infrastructure.

However, you may need to manage infrastructure that wasn’t created by Terraform. Terraform import solves this problem by loading supported resources into your Terraform workspace’s state.

The import command doesn’t automatically generate the configuration to manage the infrastructure, though. Because of this, importing existing infrastructure into Terraform is a multi-step process.

Bringing existing infrastructure under Terraform’s control involves five main steps:

Identify the existing infrastructure to be imported. Import the infrastructure into your Terraform state. Write a Terraform configuration that matches that infrastructure. Review the Terraform plan to ensure that the configuration matches the expected state and infrastructure. Apply the configuration to update your Terraform state. Terraform import workflow diagram

In this section, first you will create a Docker container with the Docker CLI. Next, you will import it into a new Terraform workspace. Then you will update the container’s configuration using Terraform before finally destroying it when you are done.

Warning: Importing infrastructure manipulates Terraform state in ways that could leave existing Terraform projects in an invalid state. Make a backup of your terraform.tfstate file and .terraform directory before using Terraform import on a real Terraform project, and store them securely.