New Terraform Import 1.5

Chandur Dissanayake
Technoid Community
Published in
5 min readJun 27, 2023
Terraform Import 1.5

Terraform Import : Version 1.5

HashiCorp’s Terraform 1.5 is now officially available, including for use on Terraform Cloud. As a leading tool for multi-cloud provisioning and automation, this version introduces a key feature: a config-driven import workflow. The former offers a declarative process to include existing resources in Terraform state, overcoming the constraints of the prior import command.

Incorporating pre-existing infrastructure into Terraform often occurs when assimilating new teams and applications into a Terraform-centric workflow, typically during standardization initiatives or mergers and acquisitions. Previously, this could only be achieved through the terraform import command, which came with several restrictions:

Table 1 : Cons of older terraform version

Terraform 1.5 revolutionizes this process with a fresh config-driven import mechanism. A novel import block permits defining import operations in the code, enabling batch execution of import operations as part of the standard plan and apply cycle. Import is now a plannable operation, reducing the chances of unanticipated state modification.

What’s more, Terraform 1.5 provides automated code generation for imported resources, significantly decreasing the time needed for writing code that matches the imported resources. As one customer aptly noted, “this is a tremendous time-saver.”

The import block requires two parameters: the ID of the cloud resource to import and the HCL address for the new resource block. Here’s an illustration of an import block for a Google Cloud Compute instance:

import {
# ID of the cloud resource
# Check provider documentation for importable resources and format
id = “instance-1234”

# Resource address
to = google_compute_instance.example
}

After incorporating import blocks into your Terraform code, execute a plan using the new -generate-config-out parameter to automatically generate the matching resource blocks in a designated file (e.g., terraform plan -generate-config-out=generated_resources.tf). Upon examining the produced code, simply conduct a regular apply operation to finalize the import to state.

Example:

  1. Add the required_version = “>= 1.5.0” to use the new config-driven import. Note: make sure you have updated your computer’s Terraform version to 1.5 as well.
  2. Simply create a file called import.tf and add in the following block.
  3. Execute the command terraform plan -generate-config-out=generated_resources.tf where “generated_resources” will be a new file created as the response.
Figure 1 : Update terraform version
Figure 2 : Add import block
Figure 3 : Generated import output

Terraform Import : Version < 1.5

The following works for terraform versions less than 1.5. The following way of Terraform import is very tedious and has a lot of manual effort. The following table describes the cons :

Getting Started

If you want to import existing resources into terraform script we can use the terraform import command in the terminal.

Steps

  1. Have some existing resources in the AWS console. Only one resource can be imported at a time. Let’s take an EC2 instance as an example.

2. Next we configure the provider and then create an empty resource block however defining the resource type as shown below.

3. Then you initialize the terraform using the terraform init command.

4. Next you type the following code in the terminal and get a terminal message as shown below.

Example code :

terraform import aws_instance.ec2_instance i-0219349278937

Below tells us the meaning of each segment in the above command :

aws_instance” is the terraform resource name which is the same for any instance

ec2_instance” is the reference name which you provide in the terraform script and the one which is there as the name in the console. (Please note that the name doesn’t matter and doesn’t have to be the same)

i-329138120” is the resource ID which you can retrieve from the console

5. Next, you type terraform show which displays the EC2 instance configuration which has just been imported.

6. You can then copy and paste the configuration into your terraform script. Note that most of the variables shown in the terminal may be default values hence carefully clean up the code and only show the required attributes.

Example: ami, arn

7. Now when repeating the same procedure as above for other resources for example security group. With this make sure to connect the EC2 instance with the security group using the ID attribute of the security group and calling the reference name given.

Example :

security_groups = [aws_security_group.ec2_sg2.id ]

8. Lastly, enter terraform plan and terraform apply when you are done with importing. Note that if you apply or plan and the resources are still existing in the AWS account then the terminal will give out a message saying there are no changes to be made, however, if the resource is deleted then the normal procedure will take place.

Note: terraform state rm <resource-name>.<reference-resource-name>

References

--

--