Importing your existing Azure resources under the management of Terraform using Azure Terrafy

Importing your existing Azure resources under the management of Terraform using Azure Terrafy

With the introduction of Azure Terrafy the goal is to import resources that are compatible with the Terraform AzureRM provider into the Terraform state whilst also generating the corresponding Terraform configuration.

It’s important to note that both the Terraform state and configuration must align with the resources’ remote state for consistency. This means that terraform plan should display no differences.

Once the import is complete and the Terraform state and configuration are aligned, the user is able to use Terraform to manage the resources as needed.

To learn how to install the Azure Terrafy tool locally, please refer to this link. The specific installation instructions will depend on your package manager.

For the purposes of this guide, I will be using MacOS Homebrew. Please execute the command “brew install aztfexport” to install the necessary package.

Next, please refer to the authentication guide to sign in to Azure using Azure CLI. Please note that you must ensure that Azure CLI is already installed on your system.

Once you have successfully authenticated, proceed to execute the “aztfexport” command.

Please note that as of the writing of this document, Azure Terrafy is capable of importing a single resource, a resource group with child resources, or a customized set of resources via an Azure Resource Graph query.

In this guide, we will be importing a resource group with its child resources using this cmdlet. aztfexport resource-group <resource group name>

Be sure to substitute the <resource group name> with a real value within your Azure subscription.

As the process runs, it will display a progress bar and provide updates on the resources being imported.

Upon completion of the import process, a list of all the resources discovered in the specified resource group will be displayed.

Please navigate to the end of the screen to review your available options. In this case, select the option to import.

After Azure Terrafy has finished running, you can open the designated folder and locate the newly created template files.

Imported Terraform TemplateFiles

The most effective way to validate the successful import of your resources is by executing the “Terraform Plan” command. After all, you are working with Terraform.

Please note, if a resource with sensitive information, like an admin password, was imported as an argument, its value will be disregarded during the import process. The admin_password for a virtual machine is a common example, and it will be assigned the value of “ignored-as-imported”. Therefore, it is important to pre-fill the appropriate value for the admin_password before executing the “Terraform Plan” command.

Your infrastructure matches the configuration.

As you can see, Terraform has compared my real infrastructure against my configuration and found no differences, so no changes are needed.

If you have any concerns or questions, please reach out to us.

If you found this guide helpful, please consider connecting on LinkedIn. Don’t forget to give this article a 👏 if you enjoyed reading it as a show of support.

Thanks! ✌️

Did you find this article valuable?

Support Emtwenty Co. by becoming a sponsor. Any amount is appreciated!