Using Azure Export for Terraform to import existing Azure resources into Terraform
Tags: terraform, azure
Published: 2023-06-20 12:00:00

 ![Using Azure Export for Terraform to import existing Azure resources into Terraform](https://www.techielass.com/content/images/2023/05/Using-Azure-Export-for-Terraform-to-import-existing-Azure-resource-into-Terrafor.png)

It can be tricky trying to maintain the state of your resources within the cloud. This is why lots of people are turning to [Infrastructure as Code](https://www.techielass.com/infrastructure-as-code-the-benefits-and-the-tools/) to help create, manage and maintain those resources in a DevOps manner.

Lots of people are turning to [Terraform](https://www.techielass.com/introduction-to-terraform/) as their Infrastructure as Code (IaC) language/tool.

What happens if you’ve got a bunch of resources already created and you want to start to use Terraform to manage them?  

This is where [Microsoft Azure Export for Terraform](https://github.com/Azure/aztfexport?ref=techielass.com) can help.  It’s worth noting that this tool has gone under a rebrand and used to be called Azure Terrafy.

Microsoft Azure Export for Terraform is a tool that makes it easy to import your existing Azure resources into Terraform modules.

In this blog post, I will show you how to install Microsoft Azure Export for Terraform, and important existing Azure resources and discuss some best practices and limitations of the tool.

## Prerequisites

- An Azure account and subscription
- Terraform installed on your machine
- Azure CLI  installed on your machine
- Azure profile configured within Azure CLI to read resources in your Azure subscription

## Install Microsoft Azure Export for Terraform

There are a few ways to install Microsoft Azure Export for Terraform onto your machine.  You can use brew, [Windows Package Manager](https://www.techielass.com/get-started-with-windows-package-manager/), Go, [Chocolatey](https://www.techielass.com/chocolatey-what-is-it/) or you can get the latest binaries from the [releases](https://github.com/Azure/aztfy/releases/?ref=techielass.com) in GitHub.

My preference is using Windows Package Manager, using the simple command:

<!--kg-card-begin: markdown-->
```
winget install Microsoft.Azure.Aztfy

```
<!--kg-card-end: markdown-->

![Using Azure Export for Terraform to import existing Azure resources into Terraform](https://www.techielass.com/content/images/2023/05/aztfy-step1.PNG)
_Install Microsoft Azure Export for Terraform_

## Import Existing Resources

Once you have the tool installed you can specify the single resource or resource group you want to import into a Terraform file.

In this tutorial I am going to import an Azure resource group and all the resources inside it.

I want to import the resource group called “rg-speakingrecord”, so I need to issue the command:

<!--kg-card-begin: markdown-->
```
aztfy rg rg-speakingrecord

```
<!--kg-card-end: markdown-->

![Using Azure Export for Terraform to import existing Azure resources into Terraform](https://www.techielass.com/content/images/2023/05/aztfy-step2.PNG)
_Import Azure Resources using MIcrosoft Azure Export for Terraform_

Microsoft Azure Export for Terraform will now begin the import process.

![Using Azure Export for Terraform to import existing Azure resources into Terraform](https://www.techielass.com/content/images/2023/05/aztfy-step3.PNG)
_Scanning Azure to create the Terraform files_

Once the tool has scanned the resources within your resource group it will show you some options.  These are at the bottom of the screen and sometimes difficult to see depending on your terminal settings.

![Using Azure Export for Terraform to import existing Azure resources into Terraform](https://www.techielass.com/content/images/2023/05/aztfy-step4.PNG)
_Import Azure resources to Terraform_

We can show results, show errors or recommendations as well as initial the import.

Type _ **w** _ to import all the resources it has found.  Microsoft Azure Export for Terraform will not start to generate the Terraform scripts for you.  It will include any dependencies and also update the Terraform state file for you.

Once it has finished you can open up the files and folders it has created and view the newly generated Terraform files.

![Using Azure Export for Terraform to import existing Azure resources into Terraform](https://www.techielass.com/content/images/2023/05/aztfy-step5.PNG)
_View generated Terraform files_

The best way to validate that the files are correct is to run _ **terraform plan** _ and it should confirm the files match the current configuration and there are no changes to be made.

## Best Practices & Tips

- Be sure to review the generated Terraform configuration files carefully before you apply it.
- If you have a lot of resources it may take some time for the tool to scan it and then generate the relevant files, so be patient.
- Microsoft Azure Export for Terraform doesn’t generate variable files, everything is defined in a static way.  So for reusability you need to manually edit and then declare variables before reusing elsewhere.

## Limitations

Currently there are some limits to using the tool:

- It can only import Azure resources, it can’t import resources from other cloud providers.
- Complex or customised infrastructure might not import well via this tool.
- The tool currently declares dependencies explicitly. You must know the mapping of the relationships between resources to refactor the code to include any implicit dependencies.

## Conclusion

In conclusion, Microsoft Azure Export for Terraform is a very valuable tool.  
  
It can save you time and effort configuring those existing Azure resources into Terraform files.

No tool is perfect and as long as you are aware of its limitations it is a great tool to have within your toolbox when dealing with existing resources and bringing them under management via Terraform.