I recently wrote a blog post about importing existing infrastructure into terraform, it got a lot of views, and somebody pointed out about an elegant tool that could automate the import.

The tool was called _terraformer, _andis much simpler to use, and can automate the reverse engineer back into a usable terraform resource.

It has some caveats, but something this post will explore further.

Importing Existing Infrastructure into Terraform

It is always much simpler to create the terraform first, you would only ever import when you need to do something…

medium.com

So what is terraformer?

A CLI tool that generates _tf_/_json__ and _tfstate_ files based on existing infrastructure (reverse Terraform)._

Terraformer uses Terraform providers and is designed to easily support newly added resources. To upgrade resources with new fields, all you need to do is upgrade the relevant Terraform providers.

I’ll go over the exact same scenarios I went through, so you can see exactly how much better and simpler it is to import using terraformer, but first of all;

Why would you desire to import existing infrastructure?

Because like everything else in life, it is sometimes impossible to plan for the future. Without adequate planning with the creation of infrastructure, it can lead to situations where infrastructure needs to be created manually due to time pressures, emergency releases or just the fact that the infrastructure exists, and terraform was never used in the first instance.

Example: You have already defined the resource and want to tell the state that this resource already exists.

Imagine that something was going wrong in production, and a change had to be applied quickly to prevent an outage. A change was added manually in route53 to add a DNS record.

Once things had settled down, the same record was defined as a terraform resource, but when apply is ran, a messages is returned to say that the resource already exists. It causes the apply stage to fail.

What needs to happen, is to import the state with the existing resource, so that next time a terraform apply is run, the terraform software will consider the resource in its state. Going forward, this means any changes made will be picked up as modifications, rather than additions.

In this hypothetical situation, let us imagine that the following resources were created from within the AWS console:

Route53 Record Set Name: ``[www.mywebsite.com.](http://www.mywebsite.com./)

Route53 Record Set Type: CNAME

Route53 Record Set Value: mywebsite.com.

#software-development #code #infrastructure #terraform #devops

Automating importing of existing infrastructure in terraform.
7.60 GEEK