Skip to main content

The comparison between Old vs New way of Terraform Import

Recently, Terraform has launched the version 1.5.0 with the feature of configuration generation for resources to be imported and bring them under terraform management. But make a note of it that configuration generation is available in Terraform v1.5 as an experimental feature.

Terraform Import before Terraform v1.5

Before v1.5, `terraform import` command was used to import existing resources into Terraform. 

The import command takes two arguments - the resource address and ID. Import will find the existing resource using its ID and import it into your Terraform state at the given ADDRESS in your state file.

Syntax

> terraform import ADDRESS ID

Let's see the import workflow in steps to understand it better.

Example: Let's say, we have manually created a subnet inside a VPC in google cloud. Now we want to import and manage this manually created subnet using terraform.

Step 01

Write a blank resource block according to the resource being imported. In our case, we will use the "google_compute_subnetwork" resource type for our resource block.

# Blank resource block to import an existing subnetwork
resource "google_compute_subnetwork" "tst_subnet" {

}

Step 02

Execute `terraform import` command with resource id and local resource address. In our case, the required arguments for the `terraform import` command would look like below. Make sure, You are passing the correct Resource ID as per your existing resource.

Resource Address: google_compute_subnetwork.tst_subnet
Resource ID: projects/prj-tf-training/regions/us-central1/subnetworks/subnet-01

Once you have all the required details handy, execute the `terraform import` command.

terraform import google_compute_subnetwork.tst_subnet projects/prj-tf-training/regions/us-central1/subnetworks/subnet-01

The resource will be imported to your state file. But still there is a step required in which you need to manually write down at least all the required arguments of your imported resource in the blank resource block.

Step 03

You can now execute “terraform state show <RESOURCE_ADDRESSS>” to get the properties captured. In our case, the command will look like below -

> terraform state show google_compute_subnetwork.tst_subnet

Now copy all the arguments (or at least all the required arguments) and fill your resource block created in step 01. You are now fully ready to manage this manually created subnet to manage under terraform. You have to repeat these three steps for all the resources you want to import and manage using terraform. Pretty hectic, Isn't it.

Drawbacks

In this old way of terraform import, we can see a couple of drawbacks.

1. The `terraform import` command can only import one resource at a time.

2. The `terraform import` can only import resources into the state. It will not generate the configuration of the imported resources. Because of this, prior to running `terraform import` it is necessary to manually write a resource configuration block for the resource, to which the imported object will be mapped.

Once the resource import is successful, Then the user has to manually write down the rest of the configuration of imported resources.

3. The `terraform import` command is required to be executed manually. Hence users cannot incorporate import of resources in CICD pipelines.

Terraform Import after Terraform v1.5

The above drawbacks are catered in new versions of Terraform to make the import of resources more seamless.

In new way of terraform import, Terraform has introduced a new import block which you can add to any Terraform configuration file. A common pattern is to create a separate file for all of your import blocks with the name "imports.tf".

Syntax

# Write an import block
import { to = RESOURCE_ADDRESS id = RESOURCE_ID }

Let's see the new workflow for importing the resources.

1. Define an import block for the resource(s). You can write down multiple import blocks, if you need to import more resources.

2. Add a corresponding resource block to your configuration, or generate configuration for that resource.

3. Run the terraform plan to review how Terraform will import the resource(s).

4. Run the terraform apply to import the resources and update your Terraform state.

* Go through this blog for detailed workflow steps.

Note

1. The import block is idempotent, meaning that applying an import action and running another plan will not generate another import action as long as that resource remains in your state.

2. If you want, you can remove import blocks after completing the import or safely leave them in your configuration as a record of the resource's origin for future module maintainers.

Benefits

In this new way of terraform import, we can see a couple of benefits which were there in old way.

1. Multiple resources can be imported at a time.

2. Terraform will generate the configuration of the imported resources when you execute the `terraform plan` command with the `-generate-config-out` flag and supply a new file path. Although, if you want, you can manually write down the configuration of resources and skip/bypass the auto-generated terraform configuration.

3. The import of existing resources can be incorporated in CICD pipelines.