How to use Terraform
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). 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). 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). 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:
$ 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). Here I will provide quick installation for Linux users. Firstly, install Homebrew(official installation guide (opens new window)). Be patient, it takes some time.
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Then install Terraformer with just one "click":
$ brew install terraformer
After all, check your installation by typing:
terraformer version
You will get the version information.
Set Azure provider
Firstly download the latest MS Azure provider from here (opens new window). When you download it, unzip a package and move the provider to the following location:
$ 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:
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:
As you can see, I have a few resourcesn. Now let me import the App Service Plan "ASP-Stockkarte-a796".
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.