Azure Terraform Export: Importing Resources with Aztfexport (2024)

In this article, we will explore the aztfexport tool that can be used to bring existing Azure resources under Terraform’s management. We will look at the tool itself, explaining what it is, what it does, and the typical workflow you will use with it. Then, we will move to a step-by-step setup tutorial with examples of how to use it.

What we will cover:

  1. What is the Azure Terraform Export tool (formerly Aztfy)?
  2. Azure Terraform Export features and benefits
  3. Aztfexport workflow
  4. How to use Aztfexport?
  5. Azure Terraform Export limitations

What is the Azure Terraform Export tool (formerly Aztfy)?

Aztfexport is an open-source export tool created by Microsoft. It allows you to migrate existing Azure resources to Terraform state files using a single command to bring them under Terraform’s control. The main benefit of this tool is consistent and automated resource management across all Azure environments. Aztfexport is also formerly known as aztfy.

Azure Terraform Export tool aims to take an existing resource group, individual resources, or a Graph query string from Azure and export it as Terraform code.

Azure Terraform Export features and benefits

Benefits of using aztfexport features include:

  • Automated and simplified importingAztfexport streamlines the process of transitioning existing resources to Terraform. It enables the automatic import of existing resources into the Terraform state without having to do that separately. This also saves you the manual effort of creating Terraform configurations from scratch.
  • Improved IaC — By exporting Azure resources to Terraform, you embrace the IaC paradigm. Changes to your infrastructure become declarative, version-controlled, and reproducible.
  • Easy integration — Aztfexport seamlessly integrates with your existing Terraform workflows. You can incorporate the exported resources into your existing Terraform projects.
  • Community support Aztfexport is part of the Azure community ecosystem. There, you can find support, contribute, and collaborate with other users.

Aztfexport workflow

Now, let’s explore how does the Azure Export tool operate with Terraform and the workflow it follows when exporting resources.

  1. Identify which existing Azure resources you want to export.
  2. Decide whether to export the Azure resources into the Terraform state, or generate HCL code.
  3. Install aztfexport (see the section below[link] for this). Execute commands specifying the resource to be exported.
  4. Inspect the generated Terraform code and make any necessary adjustments, such as adding variables, modules, or customizations.
  5. Integrate the exported resources into your existing Terraform project. Once imported into the state, you can use Terraform commands (e.g., terraform plan, terraform apply) to manage the resources.

Aztfexport leverages another tool called Aztft to identify the Terraform resource type corresponding to an Azure resource ID.

Aztft is a Go program and library that identifies the correct Terraform AzureRM provider resource type on the Azure resource ID. It then runs Terraform import under the hood to import each resource into Terraform.

After importing, aztft uses tfadd to generate the Terraform HCL code for each imported resource. Tfadd is another Go program and library for generating Terraform configuration from the Terraform state.

How to use Aztfexport?

In this tutorial section of the article, we will show you how to install Aztfexport and use it to export existing Azure resources to Terraform.

1. Prerequisites

  • An Azure subscription containing some existing resources.
  • aztfexportrequires a Terraform executable installed in your $PATH with a version greater than or equal to v0.12.

2. Install Azure Export for Terraform

You can install aztfexport for various platforms like Windows, Linux, macOS, Ubuntu, Red Hat Linux, and Go Toolchain.

Windows

To install Azure Export on Windows, run:

winget install aztfexport

Azure Terraform Export: Importing Resources with Aztfexport (1)

Precompiled binaries and Windows MSI are also available in theReleaseson GitHub.

Linux / MacOS:

To get aztfexport on Linux or MacOS run:

brew install aztfexport

Ubuntu 20.04 or 22.04

The Azure Export installation process for Ubuntu is as follows:

#Import the Microsoft repository key:curl -sSL https://packages.microsoft.com/keys/microsoft.asc > /etc/apt/trusted.gpg.d/microsoft.asc#Add packages-microsoft-com-prod repository:ver=20.04 # or 22.04apt-add-repository https://packages.microsoft.com/ubuntu/${ver}/prod#Install:apt-get install aztfexport

Red Hat Linux 8 or 9

To install aztfexport on Red Hat Linux 8 or 9, follow the process below:

#Import the Microsoft repository key:rpm --import https://packages.microsoft.com/keys/microsoft.asc#Add packages-microsoft-com-prod repository:ver=8 # or 9dnf install -y https://packages.microsoft.com/config/rhel/${ver}/packages-microsoft-prod.rpm#Install:dnf install aztfexport

Go Toolchain

This command installs the Azure Export for Terraform with Go:

go install github.com/Azure/aztfexport@latest

3. Create Azure resources

In this example, we will create a resource group named my-rg-test01. Inside this resource group, we will create a virtual network named my-vnet-test01 with two subnets: default and my-subnet-test01.

To do this, go to the Azure portal, search for virtual networks, and hit ‘create virtual network’. Create a new resource group called my-rg-test01 and specify the name as my-vnet-test01. We have selected the region as (Europe) UK South.

Azure Terraform Export: Importing Resources with Aztfexport (2)

Hit Next, and on the IP addresses tab, enter the address space as 10.0.0.0/16 (default) and create two subnets. One called ‘default’ with address 10.0.1.0/24, and the other called my-subnet-test01 with address space 10.0.2.0/24.

Azure Terraform Export: Importing Resources with Aztfexport (3)

Press ‘review and create’ and then finally ‘create’.

4. Export the Azure resource

The syntax for the aztfexport command is shown below:

aztfexport [command] [option] <scope>

There are three options for the command, resource, resource-group, or query. They can be used depending on what you need to export. Note that the resource-group option also exports the nested contents.

For example, to export the resource group and its nested resources:

aztfexport resource-group my-rg-test01

After running this command, aztfexport will initialize and display a list of the resources to be exported.

You can also use an Azure graph query such as the one below to export the network resources:

aztfexport query -n "resourceGroup =~ 'my-rg-test01' and type contains 'Microsoft.Network'"

5. View the results

The exported resources will be converted into Terraform code. We’ve successfully imported the infrastructure to Terraform!

You’ll find a .aztfexport suffix added to the generated files (e.g., main.aztfexport.tf) to avoid potential filename conflicts.

Now, let’s inspect the generated Terraform code and make any necessary adjustments. Incorporate the exported VNet resource into your existing Terraform project.

The result should look something like this:

provider "azurerm" { features {}}resource "azurerm_virtual_network" "my_vnet" { name = "my-vnet-test01" address_space = ["10.0.0.0/16"] location = "UK South" resource_group_name = "my-rg-test01" subnet { name = "default" address_prefix = "10.0.1.0/24" } subnet { name = "my-subnet-test01" address_prefix = "10.0.2.0/24" }}

6. Clean up

To avoid unexpected costs, don’t forget to remove the test resources you created from the portal.

Azure Terraform Export limitations

There are some limitations that come with the Azure Terraform export tool. For example:

  • The Terraform configurations generated by aztfexport are not meant to be comprehensive and do not ensure that the infrastructure can be fully reproduced from said generated configurations.
  • It only works with Azure resources.
  • Azure Export for Terraform is currently able to declare only explicit dependencies. You must know the mapping of the relationships between resources to refactor the code to include any needed implicit dependencies.

Key points

aztfexport aims to make life simpler when bringing existing Azure resources under Terraform control by generating the code for them and bringing them into Terraform state management automatically.

We encourage you also to explore how Spacelift makes it easy to work with Terraform. If you need any help managing your Terraform infrastructure or building more complex workflows based on Terraform and other IaC tools, Spacelift is a fantastic tool for this. It supports Git workflows, policy as code, programmatic configuration, context sharing, drift detection, and many more great features right out of the box.

If you want to learn more about Spacelift working with Azure , check our documentation or book a demo with one of our engineers.

Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.

Manage Terraform Better with Spacelift

Spacelift effectively manages Terraform state, more complex workflows, supports policy as code, programmatic configuration, context sharing, drift detection, resource visualization and includes many more features.

Start free trial

Azure Terraform Export: Importing Resources with Aztfexport (2024)

FAQs

How do I export an Azure resource to Terraform? ›

Identify which existing Azure resources you want to export. Decide whether to export the Azure resources into the Terraform state, or generate HCL code. Install aztfexport (see the section below[link] for this). Execute commands specifying the resource to be exported.

How do I import an existing Azure resource in Terraform? ›

Importing resources
  1. Create a storage account resource.
  2. Check for a clean terraform plan.
  3. Add a minimal resource block.
  4. Import the resource.
  5. Configure until terraform plan is clean again.
  6. Apply the config with the --refresh-only switch.

What is AzTFexport? ›

A tool to bring existing Azure resources under Terraform's management.

Can you use Terraform on Azure? ›

Terraform is built into Azure Cloud Shell and authenticated to your subscription, so it's integrated and ready to go.

How do I export Azure resources? ›

Go to the Azure portal:
  1. Go to the All resources page.
  2. Click the Export to CSV button.
  3. In the Export options dialog box, select the resources you want to export.
  4. Click Export.
Sep 22, 2023

What is export in Terraform? ›

Azure Export for Terraform allows you to migrate Azure resources to Terraform using a single command. Export user-specified sets of resources to Terraform HCL code and state with a single command. Azure Export for Terraform enables you to specify a predetermined scope to export.

What is Terraform import? ›

Use the terraform import command to import existing infrastructure to Terraform state. The terraform import command can only import one resource at a time. It cannot simultaneously import an entire collection of resources, like an AWS VPC.

What is terrafy? ›

An open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services.

When should I not use Terraform? ›

When Not to Use Terraform
  1. You need to make immediate changes to your infrastructure. Terraform is not designed for real-time changes, so it is not a good choice for situations where you need to make quick changes to your infrastructure.
  2. You have a small and simple infrastructure. ...
  3. You are not familiar with IaC.
Dec 1, 2023

What is Azure equivalent of Terraform? ›

Two leading IaC options are Hashicorp Terraform and Bicep. Terraform is an open-source tool that helps DevOps professionals manage on-premises and cloud services using declarative code. Microsoft Bicep utilizes declarative syntax to simplify the deployment of Azure resources.

Why is Terraform better than Ansible? ›

Both Terraform and Ansible can orchestrate and provision infrastructure. Terraform is more comprehensive in managing infrastructure, while Ansible excels in configuration management. Terraform is best suited for Day 0 activities like infrastructure creation, while Ansible is ideal for Day 1 activities and beyond.

How do I import resources into terraform module? ›

First, you must create a Terraform resource block for the S3 bucket in your configuration. Then, you can run the import command, specifying the resource address and the bucket's ID (the bucket name). This will add the existing S3 bucket to your Terraform state, allowing you to manage it going forward.

How do you deploy resources in Azure using terraform? ›

  1. Configure your environment. Azure subscription: If you don't have an Azure subscription, create a free account before you begin. ...
  2. Create and apply the Terraform plan. Browse to the Azure portal. ...
  3. Fork the test project. ...
  4. Deploy from GitHub to your deployment slots. ...
  5. Test the app deployments. ...
  6. Swap the two deployment slots.
Aug 13, 2024

How do I transfer files from Azure to local? ›

Log in to your Azure account using the Azure CLI by running the command az login . Once you are logged in, navigate to the directory where you want to copy the files to on your local machine. Use the Azure CLI to copy the files from the Azure Web App to your local machine.

Top Articles
‘The Big Door Prize’ Review: Chris O’Dowd in an Apple TV+ Charmer That Stumbles Over Its Ambitions
oxycodone-acetaminophen oral: Uses, Side Effects, Interactions, Pictures, Warnings & Dosing - WebMD
Tiny Tina Deadshot Build
Joi Databas
Mrh Forum
Ventura Craigs List
Jesus Revolution Showtimes Near Chisholm Trail 8
Morgan Wallen Pnc Park Seating Chart
Sotyktu Pronounce
Full Range 10 Bar Selection Box
What Is Njvpdi
Pvschools Infinite Campus
Cnnfn.com Markets
Walmart Windshield Wiper Blades
What is Cyber Big Game Hunting? - CrowdStrike
Bad Moms 123Movies
Khiara Keating: Manchester City and England goalkeeper convinced WSL silverware is on the horizon
使用 RHEL 8 时的注意事项 | Red Hat Product Documentation
Swgoh Turn Meter Reduction Teams
Invert Clipping Mask Illustrator
Jalapeno Grill Ponca City Menu
Nine Perfect Strangers (Miniserie, 2021)
Hennens Chattanooga Dress Code
Ups Drop Off Newton Ks
Doublelist Paducah Ky
The EyeDoctors Optometrists, 1835 NW Topeka Blvd, Topeka, KS 66608, US - MapQuest
Sandals Travel Agent Login
Wat is een hickmann?
Culver's.comsummerofsmiles
Ticket To Paradise Showtimes Near Cinemark Mall Del Norte
Parent Management Training (PMT) Worksheet | HappierTHERAPY
Brenda Song Wikifeet
Vlocity Clm
Rocketpult Infinite Fuel
RALEY MEDICAL | Oklahoma Department of Rehabilitation Services
Rage Of Harrogath Bugged
Zasilacz Dell G3 15 3579
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
Gvod 6014
'Guys, you're just gonna have to deal with it': Ja Rule on women dominating modern rap, the lyrics he's 'ashamed' of, Ashanti, and his long-awaited comeback
Guy Ritchie's The Covenant Showtimes Near Grand Theatres - Bismarck
Rush Copley Swim Lessons
Pulaski County Ky Mugshots Busted Newspaper
Pike County Buy Sale And Trade
Spurs Basketball Reference
Iman Fashion Clearance
Minterns German Shepherds
Kushfly Promo Code
Food and Water Safety During Power Outages and Floods
antelope valley for sale "lancaster ca" - craigslist
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 5781

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.