Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

How to Import Existing IaC Resources into Terraform

DevOps teams the world over rely on Terraform as an infrastructure-as-code (IaC) tool for building, changing, and managing cloud resources.

Last summer’s Terraform version 1.5 release included several significant upgrades, one of which is the ability to seamlessly integrate external resources from platforms like AWS and Azure into Terraform. 

This article will explain how to utilize this feature effectively, including step-by-step instructions for identifying existing resources, importing them into Terraform, and managing them.

The significance of the 1.5 update

Prior to the 1.5 update, users could still import infrastructure into Terraform using the “terraform import” command, but it was a more manual and error-prone process. The biggest drawback is that users had to manually update their Terraform configuration files with the correct attributes and parameters to match the imported resources.

The new update introduced configuration-driven import blocks, which make it easier to template configurations when importing existing resources. With the new -generate-config-out=PATH flag, Terraform can also generate HCL configuration for resources in import blocks that lack associated configuration, saving it to a file at the specified PATH. 

These capabilities allow organizations to leverage their existing infrastructure investments without having to spend extensive time and resources to migrate and reconfigure these assets. Having everything under one umbrella reduces operational complexity, with Terraform becoming the single, consistent management interface for all infrastructure resources.

Understanding import blocks in Terraform

Terraform Import blocks form the basis for bringing existing infrastructure resources that aren’t initially managed in Terraform under its control. They do this by mapping the current state of the resources into Terraform’s state file without recreating them.

The purpose of import blocks is to allow teams to manage all of their infrastructure in one place, even if the resources were created manually or via different tools. 

Let’s say you have a virtual network (VNet) in Azure that was initially configured using Azure’s web portal. Managing the VNet manually through the portal risks human error due to repetitive tasks and the challenge of maintaining consistency across teams and different environments.

By importing the VNet into Terraform, you gain the ability to manage its configuration through code. This allows you to collaborate more effectively with your team by using version control systems and automate the deployment of new configurations.

Prerequisites for importing external resources

While importing resources into Terraform is relatively straightforward, there are a few prerequisites you need to keep in mind:

  • Terraform version: Ensure that Terraform 1.5 or later is installed, as earlier versions do not support the import blocks capability.
  • Access to existing resource details and provider configs: You must have detailed information about the resources you want to import, which includes exact identifiers and configurations. These can be found in the management consoles or APIs of your cloud provider, whether it’s AWS, Azure, GCP, or other.
  • Be aware of dependencies: Some of the resources you want to import may have dependencies or relationships with other resources, configurations, or network settings. In that case, you must replicate those as well in Terraform to maintain functionality.

Step-by-step guide to importing resources

Once all prerequisites are met, it’s time to initiate the import process. Follow these steps.

  1. Define the resource in configuration.

    Begin by adding the resource configuration to your Terraform files. This configuration should match the attributes and settings of the resource you intend to import. For example, if you are importing an AWS S3 bucket, you would define it in your configuration file as follows:

    resource “aws_s3_bucket” “example” {
  1. Initialize Terraform.

    To do so, run the following command, which will download the necessary provider plugins and prepare your working directory for other Terraform commands:

    terraform init
  1. Run the import command.

    Next, use the terraform import command to link the existing resource to the resource defined in your configuration For example, to import an AWS S3 bucket named my-bucket, you would use the following command:

    terraform import aws_s3_bucket.example my-bucket
  1. Verify the import.

    After running the import command, verify that the resource has been imported correctly. You can do this by running:

    terraform plan

    This command reviews how Terraform will import the resource based on the current configuration and state. 
  1. Check and update configuration.

    Review the imported resource in your state file and ensure that all configurations are correct. If necessary, update your Terraform configuration to match any additional settings or attributes of the imported resource.
  1. Apply changes.

    Once verified, apply the changes to bring your infrastructure in line with the updated configuration. Run:

    terraform apply

    This command applies the changes required to reach the desired state of the configuration.

Best practices for using the “import block” feature

To avoid unnecessary difficulties when importing blocks into Terraform, consider the following best practices:

  • Be consistent with resource naming and tagging: Establish a standard naming convention and tagging strategy for assets to make it easy to identify and manage resources, and collaborate across teams. This is especially important in larger and more complex environments.
  • Create regular state file backups: Errors and misconfigurations are fairly common in online environments, so having a backup can save you a lot of headache by allowing you to restore your infrastructure to a previous state.
  • Continuously monitor imported resources: Since imported resources aren’t created in Terraform, it’s best to regularly check them for any changes or discrepancies that may occur.

Conclusion

The addition of the import blocks feature in Terraform is a much-welcomed enhancement, significantly simplifying the process of integrating infrastructure resources from different platforms into one place.

Organizations can now enjoy streamlined management of their entire infrastructure, taking full advantage of IaC over manual configurations and ad-hoc scripts.

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x