Terraform: Importing Existing Infrastructure Part-2

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:

4. Import

Consider what would happen if the EC2 instance and its associated configuration were stored in our files. It’s just a matter of mapping the two files into a state file. We execute this by using the import command, which is as follows:

$ terraform import aws_instance.new_instance <Instance ID>

The output should be shown as below:

Terraform infrastructure

The above command uses the ID to map the AWS instance.new_instance settings to the EC2 instance. By mapping, the state file recognizes the EC2 instance existing with the provided ID. Because it was fetched via the import command, the state file includes details about every attribute of an EC2 instance.

5. Examine terraform state files and make output plans

Please note that the terraform.tfstate file has been added to the directory. After the import operation was completed successfully, this file was created. Please take a few moments to read through the contents of this document.

Configuration doesn’t yet show all of the properties. If we try to run the plan/apply command on this configuration, it fails because the attribute values have not been changed. Run the command terraform plan then look at the output to narrow the gap between configuration and state files.

According to the plan, it would try to restore the EC2 instance. However, this is totally opposite to our goal. We could execute it even if we didn’t care about the current resources and instead used configuration to create new ones.

The best part is now Terraform has detected the presence of an EC2 instance linked to its current state.

6. Improve the configuration to avoid having to replace it.

It’s critical to realize at this time that the terraform.tfstate file is a critical component of Terraform reference. This status file is taken into account in all of the company’s future operations. You must examine the state file and modify the configuration to ensure that the differences between them are as small as possible.

The word “small” is used here on purpose. Right now, you should concentrate on not replacing the existing EC2 instance, but adjusting the configuration to avoid the need for replacement. You’d finally arrive at a condition with no difference.

Examine the plan’s output for all of the attributes that cause the replacement. The plan’s output will reflect this. The AMI ID is the only attribute that affects replacement in our case. Fixing this gap should prevent the EC2 instance from being replaced.

Run terraform plan again, changing the value of AMI from “unknown” to whatever is displayed in the plan output. Take note of the result.

The plan somehow doesn’t suggest that the EC2 instance will be replaced at this time.  If you have the same result then you’ve successfully imported the cloud resource in part. You are now in a low-risk condition. if we run the apply command now, a few attributes will be changed but the resource would not.
You are now in a low-risk condition. if we run the apply command now, a few attributes will be changed but the resource would not.

7. Make Configuration Improvements to Avoid Changes

we’ll need to align the resource block even further if we would like to get a state of no difference. The attribute changes are highlighted in the plan output using the sign. It also displays the gap among both numbers. It emphasizes the shift in the instance type value from “t2.micro” to “unknown,” for example.

In other words, Terraform wouldn’t even have requested a change when the value of instance type had been “t2.micro.” Consequently, you can see the tags that are highlighted have changed. Let’s make the necessary changes to the settings to close these gaps. The following is the final AWS instance resource block:

resource "aws_instance" "new_instance" {
  ami           = "ami-01124f5415d6n3556"
  instance_type = "t2.micro"
  tags = {
     "Name": "test-instance"
 }
}

Run terraform plan once more and look at the results.

aws_instance.myvm: Refreshing state... [id=i-034b29d4d0e7ac561]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

Congratulations if you get the same result; you’ve successfully imported the cloud resource into Terraform configuration. This setup may now be managed directly through Terraform, with no surprises.

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