Azure Terrafy: Import and Manage Existing Azure Resources with Terraform (2025)

As an Azure user, you may be looking for a way to manage your infrastructure with the power of Terraform. However, getting started with Terraform can be challenging, especially if you have existing resources that you want to include in your configuration. That's where Azure Terrafy comes in. Azure Terrafy is a tool that makes it easy to import your existing Azure resources into Terraform modules. In this blog post, I will introduce you to Azure Terrafy and show you how it can streamline your Azure resource management process. By the end, you will have a better understanding of how Terrafy can help you use Terraform to manage your infrastructure with ease.

Azure Terrafy is a tool that makes it easy to import your existing Azure resources into Terraform modules. Suppose you're an Azure user looking to manage your infrastructure with the power of Terraform. In that case, Azure Terrafy can save you time and effort by automating the process of incorporating your existing resources into your Terraform configuration. This is especially useful for those who have a "brownfield" environment, where their infrastructure already has a number of existing resources that need to be brought under the management of Terraform. It can save you a lot of time and effort. Without Terrafy, you would need to manually create a Terraform configuration file for each resource you want to manage. This can be tedious and error-prone, especially if you have many resources.

In addition to saving time and effort, using Azure Terrafy can also help you ensure that your Terraform configuration is accurate and up-to-date. By importing your existing resources into Terraform, you can ensure that your configuration reflects the current state of your resources rather than relying on potentially outdated documentation or manual configuration. This can help you avoid errors and ensure your infrastructure is managed effectively.

Features and benefits of using Terrafy:

How Azure Terrafy fits into the overall Azure and Terraform landscape:

  • Azure Terrafy is a tool specifically designed to help manage Azure resources in Terraform

  • It works alongside Azure Resource Manager to discover and import resources into Terraform modules

  • Azure Terrafy is just one piece of the puzzle regarding managing Azure infrastructure with Terraform. Other tools and technologies, such as Azure DevOps and Azure Functions, can also be used in conjunction with Terrafy to create a comprehensive infrastructure management solution.

Prerequisites

  • An Azure account and subscription

  • Terraform installed on your machine

  • The Azure CLI installed on your machine

  • A configured Azure CLI profile with the necessary permissions to read resources in your Azure subscription

  • Install aztfy from GitHub Releases - pre-compiled binaries on macOS, Windows & Linux platforms

Importing Existing Resources

  1. Run the aztfy command to create a new Terraform configuration file on a single resource, resource group or query for the list of resources. You can pass the —non-interactive option if you want to run in non-interactive mode. Here dapr-talk is the resource group name.

     **aztfy rg --non-interactive dapr-talk // or aztfy res <resource_id>**
  2. Review the generated Terraform configuration file to ensure that it accurately reflects the state of your Azure resources

    1. You may need to modify or expose certain configurations through terraform variables/sensitive variables.
  3. Run the terraform plan command to preview the changes that will be made to your resources when you apply the Terraform configuration

     ➜ terraform plan azurerm_resource_group.res-0: Refreshing state... [id=/subscriptions/xxxx/resourceGroups/dapr-talk] azurerm_service_plan.res-1: Refreshing state... [id=/subscriptions/xxxx/resourceGroups/dapr-talk/providers/Microsoft.Web/serverfarms/ASP-daprtalk-814c] azurerm_application_insights.res-8: Refreshing state... [id=/subscriptions/xxxx/resourceGroups/dapr-talk/providers/Microsoft.Insights/components/webappdapr] azurerm_windows_web_app.res-2: Refreshing state... [id=/subscriptions/xxxx/resourceGroups/dapr-talk/providers/Microsoft.Web/sites/webappdapr] azurerm_app_service_custom_hostname_binding.res-6: Refreshing state... [id=/subscriptions/xxxx/resourceGroups/dapr-talk/providers/Microsoft.Web/sites/webappdapr/hostNameBindings/webappdapr.azurewebsites.net] No changes. Your infrastructure matches the configuration.
  4. If the plan looks correct, run the terraform apply the command to apply the Terraform configuration and bring your Azure resources under the management of Terraform.

     terraform apply

Here are a few tips and best practices to keep in mind when using Terrafy to import your existing resources into Terraform:

  • Be sure to review the generated Terraform configuration file carefully before applying it. This will help you ensure that the configuration accurately reflects the state of your resources and avoid any errors or unintended consequences.

  • If you have many resources, it may take some time for Azure Terrafy to scan and generate the configuration file. Be patient and allow the process to complete before applying the configuration.

  • If you have resources that you don't want to include in your Terraform configuration, you can use the aztfy res or aztfy query command to import specific resources individually. This can be helpful if you only want to manage a subset of your resources with Terraform.

  • You must create terraform resources manually if there are not-supported / skipped resources.

  • Don't forget to run the terraform plan command before applying your configuration. This will help you identify potential issues or conflicts before they become a problem.

By following these steps and best practices, you can use Azure Terrafy to easily import your existing Azure resources into Terraform modules and start managing them with ease.

Limitations

Here are some limitations of Azure Terrafy to consider:

  1. Only works with Azure resources: Terrafy cannot import resources from other cloud providers.

  2. It may not be suitable for highly complex or customized infrastructure: Azure Terrafy does not support all resources. The resources which are not supported have to be done manually.

  3. Limited context: The review is necessary to convert the sensitive configurations to terraform-managed variables.

Conclusion

In conclusion, I have found Azure Terrafy to be a valuable tool for managing Azure resources in Terraform. It has saved me time and effort compared to manual resource management, and its integration with Azure and Terraform has made it easy to use in various infrastructure management scenarios. However, like any tool, it has its limitations, such as only working with Azure resources and potentially complex for more customized infrastructure.

Overall, I recommend giving Azure Terrafy a try if you are looking for a tool to help manage your Azure resources with Terraform. Just be sure to consider its limitations and whether it fits the needs of your specific infrastructure. By following the steps outlined in this blog post and keeping in mind best practices like reviewing the generated code before applying it, you can use Azure Terrafy to manage your Azure resources with Terraform effectively.

I'm Siva - Director, DevOps & Principal Architect at Computer Enterprises Inc from Orlando. I'm an AWS Community builder. I write blogs and tutorials about Cloud, Containers, IoT, and DevOps. If you are interested, please follow me @ksivamuthu on Twitter or check out my blogs at sivamuthukumar.com

Azure Terrafy: Import and Manage Existing Azure Resources with Terraform (2025)

FAQs

How to import existing Azure resources in Terraform? ›

The Import Process on Terraform

Write a Terraform resource block in your configuration file that corresponds to the resource you created. Make sure the name and type match the Azure resource. The simplest way to achieve this is by adding the import block of code into your tf file.

What must be provided with the import command for Terraform to successfully import resources? ›

You can use the import command to import an existing S3 bucket into your Terraform state. 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).

What is Azure Terrafy? ›

A tool brings existing Azure resources under the management of Terraform.

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 you manage existing resources in terraform? ›

Terraform Import Basics

Then, you can use the terraform import command to attach the existing settings of the resource to the name you specified. For example, to import an AWS instance with ID "i-abcd1234" into the aws_instance resource named "example", you would run terraform import aws_instance. example i-abcd1234.

What are the two steps to successfully import the existing configuration with terraform? ›

Using configuration to import resources involves the following steps:
  1. Identify the existing infrastructure you will import.
  2. Define an import block for the resources.
  3. Run terraform plan to review the import plan and optionally generate configuration for the resources.

What is Terra Azure? ›

Terra is Broad's flagship biomedical data platform, co-developed by Broad, Microsoft, and Verily. The open-source platform is used by over 65,000 individuals globally for biomedical data analysis, secure data sharing, and cross-enterprise data science collaborations.

What is Azure called now? ›

Azure AD is now Microsoft Entra ID

All licensing and functionality remain the same.

What did Azure used to be called? ›

Azure was first introduced at the Professional Developers Conference (PDC) in October 2008 under the codename "Project Red Dog". It was officially launched as Windows Azure in February 2010 and later renamed to Microsoft Azure on March 25, 2014.

How to integrate Azure with Terraform? ›

Quickstart
  1. Create an Azure resource group using Terraform.
  2. Create an AKS cluster.
  3. Create a Linux VM.
  4. Create a Windows VM.
  5. Create an Azure key vault and key using Terraform.
  6. Direct web traffic with Azure Application Gateway - Terraform.
  7. Create a single database in Azure SQL Database using Terraform.

Why is Terraform used in Azure? ›

The Terraform template-based configuration file syntax enables you to configure Azure resources in a repeatable and predictable manner. Automating infrastructure includes the following benefits: Lowers the potential for human errors while deploying and managing infrastructure.

How do I import resources into terraform module? ›

To import a simple resource into Terraform, follow the step-by-step guide below.
  1. Prepare the EC2 instance. ...
  2. Create main.tf and set provider configuration. ...
  3. Write config for the resource to be imported. ...
  4. Run the import command. ...
  5. Observe state files and plan output. ...
  6. Improve config to avoid replacement.

How do I import an existing IAM role in terraform? ›

Import
  1. Add resource block to . tf file. ...
  2. Import IAM Policy. Here is the command and the result example. ...
  3. Import IAM Role. Here is the command and the result example. ...
  4. Check the tfstate file. ...
  5. Update main.tf in IAM Policy and Role module directory. ...
  6. Plan to check differences.
Apr 25, 2024

How to import an existing S3 bucket in terraform? ›

  1. execute terraform import, something like this: terraform import aws_s3_bucket.my_bucket my-existing-bucket-name.
  2. do terraform plan.
  3. if your bucket still trying to be recreated, check terraform plan output and you will see how to update your code.
Mar 28, 2023

How to import an Azure storage account 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.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 5779

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.