Home Export an Azure Resource Group to Terraform
Post
Cancel

Export an Azure Resource Group to Terraform

This guide will walk you through the process of using Terrify to export an existing Azure resource group.

Prerequisites

Before you begin, make sure you have the following:

  1. Terrify installed on your machine.
  2. An Azure subscription with an existing resource group that you want to export.

Steps

  1. Log in to Azure using the Azure CLI:

    1
    
     az login
    
  2. Set the active subscription to the one that contains the resource group you want to export:

    1
    
     az account set --subscription <subscription-id>
    

    Replace <subscription-id> with the ID of your desired subscription.

  3. Export the Azure resource group:

    1
    
     aztfexport resource-group <resource-group-name> --output <output-directory>
    

    Replace <resource-group-name> with the name of your Azure resource group, and <output-directory> with the directory where you want to save the exported files.

  4. Wait for the export process to complete. Terrify will generate a set of Terraform configuration files and save them in the specified output directory.

  5. (Optional) Customize the generated Terraform configuration files according to your needs.

  6. Deploy the exported resource group using Terraform:

    1
    2
    3
    
     cd <output-directory>
     terraform init
     terraform apply
    

    This will apply the Terraform configuration and create the resources in your Azure subscription.

Conclusion

By following these steps, you should be able to export an existing Azure resource group using Terrify and deploy it using Terraform. It’s not perfect and does not generate the most maintainable configs but this can be a useful way to manage your infrastructure as code and automate the deployment of resources in Azure when you have existing resources and don’t want to start from scratch.

References

Microsoft Tech Community - Azure Terrify: Import your existing Azure infrastructure into Terraform

Microsoft Tech Community - Announcing Azure Terrify and AzAPI Terraform Provider Previews

Microsoft Learn - Export resources from Azure for Terraform in HCL

This post is licensed under CC BY 4.0 by the author.