Skip to main content

The new way of importing resources in Terraform

Terraform has recently launched the version 1.5.0 which introduced the much awaited feature of creation of configurations for imported resources. With the introduction of this new feature, users don't need to write the imported resources configuration manually. 

Let's understand in detail about this new way of importing the resources to bring them under the management of terraform.

The new way of Terraform Import

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.

Example: Let's say, we have manually created a vpc and two subnets inside it in google cloud. Now we want to import and manage these manually created resources using terraform.

Step 01

Write the import block for the resources to be imported. In our case, import blocks will look like below. Make sure to change the resource local names and resource ids as per your need.

# Write required import blocks
import { to = google_compute_network.tst_vpc id = "projects/prj-tf-training/global/networks/tst-vpc" } import { to = google_compute_subnetwork.tst_vpc_subnet_01 id = "projects/prj-tf-training/regions/asia-south1/subnetworks/tst-vpc-subnet-01" } import { to = google_compute_subnetwork.tst_vpc_subnet_02 id = "projects/prj-tf-training/regions/us-central1/subnetworks/tst-vpc-subnet-02" }

Step 02

Execute the `terraform plan` command with the `-generate-config-out` flag by supplying a new file path. Generated terraform configuration will be stored in this new file. Make sure to not supply a path to an existing file otherwise terraform will throw an error.

> terraform plan -generate-config-out=networks.tf

Once the terraform configuration is generated, you can review the generated configuration and update it as needed. You may wish to move the generated configuration to another file, add or remove resource arguments, or update it to reference input variables or other resources in your configuration.

Step 03

Execute the `terraform apply` command to import your existing resources. You are now fully ready to manage these manually created resources to manage under terraform.

> terraform apply -auto-approve

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.

Limitations

Terraform generates configuration for importable resources during a plan by requesting values for resource attributes from the provider. For certain resources with complex schemas, Terraform may not be able to construct a valid configuration from these values. The configuration will still be generated but you may need to manually check the conflicting arguments to avoid further issues.

References