Export Azure Resources to Terraform

This blog post will show how to export Azure resources into Terraform and manage them using infrastructure-as-code.

Many organisations run workloads in Azure that were deployed using Azure PowerShell, CLI, and the portal. We must export them and convert the deployment file to Terraform configuration to manage them with Terraform.

Aztfexport

To help us export resources to Terraform, Microsoft has created the aztfexport tool, which runs on Windows, Linux and macOS. This post will show how to install the tool on WSL.

Install on Linux \ WSL

Start by running the following command to Install the tool on WSL or Ubuntu.

curl -sSL https://packages.microsoft.com/keys/microsoft.asc

Copy the output and open the following file with Nano or any text editor (create if it doesn’t exist).

/etc/apt/trusted.gpg.d/microsoft.asc

Copy the output from the curl command into the file and save.

Run the following command (for Ubuntu 20.04, change to 20.04)

apt-add-repository https://packages.microsoft.com/ubuntu/22.04/prod

And finally, run the installation command

apt-get install aztfexport

The output should look like:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  aztfexport
0 upgraded, 1 newly installed, 0 to remove and 39 not upgraded.
Need to get 11.9 MB of archives.
After this operation, 74.9 MB of additional disk space will be used.
Get:1 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 aztfexport amd64 0.13.1 [11.9 MB]
Fetched 11.9 MB in 7s (1680 kB/s)                                                                                                                                                    
Selecting previously unselected package aztfexport.
(Reading database ... 84963 files and directories currently installed.)
Preparing to unpack .../aztfexport_0.13.1_amd64.deb ...
Unpacking aztfexport (0.13.1) ...
Setting up aztfexport (0.13.1) ...

Export Resources

We will export an Azure Resource Group with resources to show how the tool works. To get started, log in to Azure using.

az login 

Create an empty directory that will act as the terraform configuration directory.

aztfexport resource-group ResourceGroupName

Click N to append the configuration files to the directory.

Once the export is done, run

terraform init --upgrade

To check if the export works run

terraform plan

If the export was successful, you should see a message saying no changes are needed, as shown above.

No changes. Your infrastructure matches the configuration.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.