How to export existing infrastructure to Terraform code?

Matt
3 min readJan 3, 2021
How to do reverse-terraforming using Terraformer

Building Infrastructure as Code (IaC) using Terraform is very easy. Maybe you are already working with it every day. You write a code and then your cloud infrastructure starts to build like magic! But what if you land into already existing infrastructure which is not reflected as code in Terraform? It seems like the only way is to import existing resources. Yeah, that’s sounds hella boring. But it’s the only option we have? Fortunately for us… no!

Imagine that you push the enter button and after a few seconds your Terraform code has been entirely built! This is possible thanks to Terraformer.

Terraformer generates tf/json + tfstate files from existing infrastructure. This is called reverse-terraforming. You can use it with AWS, GCP and Azure. Unfortunately Terraformer works only with supported resources. For Microsoft Azure you can find it here.

Prerequisites:

  • Microsoft Azure Subscription :D
  • Already existing resources ;)
  • Terraform already installed

1. Make sure you are logged into Azure

To avoid future problems, make sure you are correctly logged into Microsoft Azure. Run this command in your Linux terminal and check your subscription:

$ az account list

If you do not have Azure CLI, install it.

2. Install Terraformer

There are many ways to install Terraformer. You can find them in the official installation guide. Here I will provide quick installation for Linux users.

Firstly, install Homebrew(official installation guide). Be patient, it takes some time.

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

If installation completed, make the program available on the system. To do this, execute these commands:

$ test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
$ test -d /home/linuxbrew/.linuxbrew &&
eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
$ test -r ~/.bash_profile && echo eval" ($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile
$ echo "eval $($(brew --prefix)/bin/brew shellenv)" >>~/.profile

Then install Terraformer with just one click:

$ brew install terraformer

After all, check your installation by typing:

$ terraformer version
terraformer version

2. Set Azure provider

Firstly download the latest MS Azure provider from here. When you download it, unzip a package and move the provider to the following location:

$ mv terraform-provider-azurerm_v2.41.0_x5  /home/<your_username>/.terraform.d/plugins/linux_amd64

If folder plugins/linux_amd64 does not exist, create it.

Moreover, to generate Terraform code from your Azure infrastructure, we have to set Azure Subscription ID:

with name$ export ARM_SUBSCRIPTION_ID=[SUBSCRIPTION_ID]

3. Export your Azure infrastructure to Terraform code

Now is time to play! Are you ready to get hands-on?

Firstly, let’s take a look at my existing resources on my Azure Account:

azure resources terraformer
My Azure resources

As you can see, I have a few resources. Let me import Virtual Machine named “VM-OAT-EUW-000”.

$ terraformer import azure -r virtual_machine
reverse-terraforming in action
reverse-terraforming in action

And now our code is generated! Let me see it:

$ cat generated/azurerm/virtual_machine/windows_virtual_machine.tf
terraformer output
Our generated Terraform code

As we can see, everything has been imported correctly, with tfstate file included.

Final thoughts

Terraformer is a relatively young tool, so it doesn’t work perfectly. Although it is still improving and being updated, not all of all resources are supported. For example, we cannot import the Azure Key Vault.

Overall Terraform is very useful tool in case you are moving to a project where infrastructure already exists but the Terraform environment is not set up.

So have a nice reverse-terraforming! :D

If you have any questions please ask in comments, you can always contact me on LinkedIn.

--

--