Terraform 1.5 import block — Best Feature

Tasrie IT Services
3 min readJun 15, 2023

--

HashiCorp released Terraform 1.5 version, which includes a config-driven import workflow and a new language primitive for infrastructure validations, is now generally available. In this article we will explore how to import AWS EC2 instance.

Terraform 1.5 Import block

When onboarding new teams and applications into a Terraform-based workflow, it is often necessary to bring existing infrastructure under management using Terraform. This task is commonly performed as part of standardization efforts or during mergers and acquisitions. However, the previous method of achieving this, the “terraform import” command, had some limitations.

Old Terraform Import Limitations

  1. Resources had to be imported one at a time.
  2. State modifications occurred immediately, without an opportunity to preview the results. This could lead to unintended changes or deletions of resources if another team member executed an apply operation on the shared state before the corresponding configuration was added.
  3. The matching resource code had to be manually written, often requiring multiple steps of running plans to determine the necessary attribute values for a successful execution.

Enhanced Import Capabilities

Moreover, Terraform 1.5 introduces automatic code generation for imported resources. This feature significantly reduces the time and effort required to write code that matches the imported resources. One of our customers expressed that this improvement will save them weeks and weeks of work.

We provide DevOps Consulting services, make sure to check out the page

Terraform 1.5 Import block example

The import block requires two parameters: the ID of the cloud resource to be imported and the HCL address for the new resource block. Here’s an example of how an import block is used for an Amazon resource:

import {
to = aws_instance.example
id = "i-abcd1234"
}
resource "aws_instance" "example" {
name = "hashi"
# (other resource arguments...)
}

Detailed Explanation

First lets create terraform.tf and we will add here our provider configuration

provider "aws" {
region = "eu-west-1"
profile = "tasrie-dev" # leave this if you have default profile
}

Now create another file ec2.tf and in this file we will mention the import block configuration.

import {
id = "i-abcd1234"
to = aws_instance.web
}

In the above code, we mentioned id which is the ec2 instance id.

Second parameter is where terraform will give the resource in state, consisting of the resource type and name

Now we run this following command which will generate our all the ec2 instance resource detail in a generated.tf file

terraform plan -generate-config-out=generated.tf

Once the command is run successfully. You have your terraform resource configuration imported to generated.tf

Finally you can run the terraform plan command to run the terraform to verify that your configuration matches the current settings of your ec2 instance.

terraform plan

Before proceeding to next steps make sure to verify all the parameters are matching. Since this feature is still in experimental stage. It is recommended to limiting any destructive changes.

Once you’re happy with the configuration we can successfully proceed to terraform apply step

Now, apply the configuration. Respond yes to the prompt to confirm the operation.

terraform apply

Now you have successfully imported the resource easily and quickly.

Terraform 1.5 import block with module

import {
to = module.instances.aws_instance.example
id = "i-abcd1234"
}

For more such content, make sure to check out our latest tech blog

Follow our LinkedIn Page

--

--

Tasrie IT Services
Tasrie IT Services

Written by Tasrie IT Services

Accelerating digital transformation with seamless DevOps, Cloud solutions and Services