Terraform 1.5 import block - config-driven

Tasrie IT Services

Tasrie IT Services

·3 min read
Terraform 1.5 import block - config-driven

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.

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:

hcl
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

hcl
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.

hcl
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

bash
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.

bash
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.

bash
terraform apply

Now you have successfully imported the resource easily and quickly.

Terraform 1.5 import block with module

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

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

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

Follow our LinkedIn Page

illustration
Need Expert Help ?

At Tasrie IT, we assist businesses in their growth and addressing intricate issues by utilizing advanced cloud technologies and contemporary platform engineering techniques.

Related Posts

Terraform backend S3 Example
·awsterraform

Terraform backend S3 Example

Image by Freepik In this article we will explore terraform backend s3 example. In the world of infrastructure management, Terraform has emerged as a powerful tool. It allows you to define and provis...

Tasrie IT Services

Tasrie IT Services

Easily create Terraform RDS using aws_db_instance
·terraform

Easily create Terraform RDS using aws_db_instance

Web illustrations by Storyset With Terraform RDS, you can simplify your database provisioning and management process. In this article, we will explore the power of Terraform and how it can revolution...

Tasrie IT Services

Tasrie IT Services

How to use Terraform Output
·terraform

How to use Terraform Output

Are you struggling to manage your infrastructure while using Terraform? Do you find it challenging to output the right information that you need? If yes, then this article is for you! In this beginner...

Tasrie IT Services

Tasrie IT Services