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
Step 01
# Blank resource block to import an existing subnetworkresource "google_compute_subnetwork" "tst_subnet" {}
Step 02
> terraform import google_compute_subnetwork.tst_subnet projects/prj-tf-training/regions/us-central1/subnetworks/subnet-01
Step 03
> terraform state show google_compute_subnetwork.tst_subnet
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 blockimport { 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.
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.