> ## Content Index
> Fetch the complete content index at: https://blog.bajonczak.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to use Terraform
- URL: https://blog.bajonczak.com/terraformer/
- Published: 2021-05-18T22:00:00.000Z
- Updated: 2022-10-23T15:31:54.000Z
- Author: Sascha Bajonczak
- Tags: DevOps

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 (opens new window)](https://www.terraform.io/docs/import/index.html?ref=blog.bajonczak.com). Yeah, that’s sounds 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 (opens new window)](https://github.com/GoogleCloudPlatform/terraformer?ref=blog.bajonczak.com). 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 (opens new window)](https://github.com/GoogleCloudPlatform/terraformer?ref=blog.bajonczak.com#use-with-azure). Prerequisites:

- Microsoft Azure Subscription 😄
- Already existing resources 😉
- Terraform already installed

# 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:

```bash
$ az account list

```

If you do not have Azure CLI, 8install it\](https://docs.microsoft.com/pl-pl/cli/azure/install-azure-cli).

# Install Terraformer

There are many ways to install Terraformer. You can find them in the [official installation guide (opens new window)](https://github.com/GoogleCloudPlatform/terraformer?ref=blog.bajonczak.com#installation). Here I will provide quick installation for Linux users. Firstly, install Homebrew([official installation guide (opens new window)](https://brew.sh/?ref=blog.bajonczak.com)). Be patient, it takes some time.

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

```

Then install Terraformer with just one "click":

```bash
$ brew install terraformer

```

After all, check your installation by typing:

```bash
terraformer version

```

You will get the version information.

# Set Azure provider

Firstly download the latest MS Azure provider [from here (opens new window)](https://releases.hashicorp.com/terraform-provider-azurerm/?ref=blog.bajonczak.com). When you download it, unzip a package and move the provider to the following location:

```bash
$ mv terraform-provider-azurerm_v2.59.0  /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:

```bash
with name$ export ARM_SUBSCRIPTION_ID=[SUBSCRIPTION_ID]

```

# 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:

![My Azure Infrastructrue](https://notes.bajonczak.com/assets/img/azureinfrastructure.e3824eb9.png)

As you can see, I have a few resourcesn. Now let me import the App Service Plan "ASP-Stockkarte-a796".

```bash
terraformer import azure -r app_service

```

And now our code is generated! The output will be shown in the file `app_service.tf`, it will also include a *tfstate* file, so you can start now with this template from now on.

# Final thoughts

Terraformer is a relatively young tool, so it doesn’t work perfectly. But for tha mein usecases it will help you to export the components.

On the other hand you actually didn't have a chance to get it working in PowerShell or s.th.. But for this reason there exists the WSL on Windows, that installs an ubuntu as subsystem four you.