Terraform: Importing Existing Infrastructure

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

The following topics will be discussed to importing the infrastructure in terraform in this blog:

Before continuing to construct the IaC in Terraform, you’ll learn how to import pre-existing cloud resources in this tutorial. This article will bring you through an IaC import scenario that many teams face when they first start using Terraform for their tasks.

What is the point of terraform importing?

Terraform is a new technology, therefore learning how to use it to manage an organization’s cloud resources will require some time and effort.
Teams are starting to use cloud infrastructure simply via their individual web consoles due to a shortage of human resources and the high learning curve needed in using Terraform efficiently.

Any IaC approach necessitates some training and real-world situation handling experience. When it comes to ideas as states and remote backends, things get much more difficult. You may delete the terraform.tfstate file under the worst scenario. Fortunately, you could use the import function to recreate it.

Terraform import makes it easier to bring existing cloud resources inside Terraform administration. Import command that examines real-world infrastructure and modifies its state. As a result of which, infrastructure upgrades are easily applied by IaC in the long run.

The import functionality allows to change the state locally, but it does not immediately construct the necessary configuration. The Terraform team, on the other hand, is working to improve this feature in future editions.

Importing Made Easy

Let’s start with a small resource with an EC2 instance in AWS, to get a sense of why do we need to import cloud resources. I hope  Terraform is already installed on your server and setup the AWS credentials via AWS CLI. This article is based on the simple infrastructure, not the high level. Follow the instructions below to import a simple resource into Terraform.

1. Create an Amazon EC2 instance.

Assuming you’ve already installed Terraform and configured your AWS credentials in the AWS CLI, start by importing a simple resource—an EC2 instance in AWS. For the purposes of this article, we’ll manually set up an EC2 instance to import. If you have a resource to import, this step may be skipped.

Terraform can be used to set up an EC2 instance in an existent VPC.

so, Create an EC2 instance in your Amazon Web Services account. Here are some details about the EC2 instance that was generated as an example:

importing

2. Creating main.tf then configure the provider.

This step’s goal is to add this EC2 instance to the Terraform configuration. Create main.tf at your selected folder and specify the AWS provider. The file should be like the one seen below. Example of importing an EC2 instance into a Terraform configuration

provider aws {
  access_key = "Access key"
  secret_key = "Secret Key"
  region     = "ap-south-1"
}

To initialise the Terraform modules, run terraform init. The result can be seen below:

3. Creating a configuration file for the resources to be imported.

Terraform import somehow doesn’t create the configuration files on its own, as previously stated. As a result, you’ll have to manually create the EC2 instance’s setup. We’ll have to add or alter them once we import our EC2 instance in the state file, so there aren’t many arguments here.

You can start adding all the arguments you’re familiar with. However, this is not a surefire strategy because the infrastructure you may need to import was not established by you. As a result, it’s probably advisable to ignore a few arguments.

We’ll look at how to change our setup to match the exact resource in a moment. For the time being, append the EC2 config to the main.tf file. I’ve used the configuration below as an example. I added the AMI and instance type parameters just because they are mandatory arguments for the AWS instance resource block.

resource "aws_instance" "new_instance" {
  ami           = "unknown"
  instance_type = "unknown"
}

For rest of the points, Continue with Part-2

Keep Reading About Terraform:

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Jaswinder Singh
Jaswinder Singh
DevOps Engineer

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook