Import your existing Azure infrastructure into Terraform using Azure Terrafy (2025)

Martin

Posted on

Import your existing Azure infrastructure into Terraform using Azure Terrafy (2) Import your existing Azure infrastructure into Terraform using Azure Terrafy (3) Import your existing Azure infrastructure into Terraform using Azure Terrafy (4) Import your existing Azure infrastructure into Terraform using Azure Terrafy (5) Import your existing Azure infrastructure into Terraform using Azure Terrafy (6)

#azure #terraform #devops #cloud

Overview

Terraform is a great tool for defining your infrastructure using code and getting it deployed to Azure or your chosen cloud provider. One of the many challenges you may face is importing existing infrastructure into Terraform so that you can then manage it through code along with the benefits that come along with that.

You will often hear the term state file. Terraform must store state about your managed infrastructure and configuration. Thee state is used by Terraform to map real world resources in the cloud back to your configuration and to keep track of metadata.

This state is stored by default in a local file named terraform.tfstate however it can also be stored remotely and more commontly in an Azure storage account as a blog which is often what's chosen by large scale enterprises to avoid having local state files.

There are usually 3 steps that typically need to be completed in order to import your cloud resource into Terraform using the process provided by Hashicorp:

  • Define your infrastructure using Terraform.
  • Run Terraform import to update your state file.
  • Run Terraform plan to verify the import is successful.

This method is well defined in the Terraform docs.

In this tutorial we will be talking about a new tool called Azure Terrafy developed by Microsoft which aims to automate the import process by generating both the configuration and state file for your existing infrastructure making it much easier to get started with Terraform.

Installation

You can install Azure Terrafy using the Go Tool chain by running:

go install github.com/Azure/aztfy@latest

If you haven't got Go then you can install it by following the instructions on the official Go docs.

You can also manually download the latest Binary from the Azure Terrafy Github page.

Just make sure to update your environment path so you can run Terrafy from your chosen shell.

This Stack Overflow article contains instructions for setting the PATH on Windows through the user interface.

For Mac OS you can run echo $PATH and place the executable/binary into one of the listed paths.

Verify you have it correctly installed by running aztfy.

Import your existing Azure infrastructure into Terraform using Azure Terrafy (7)

We are now ready to start importing our infrastructure into Terraform .

Importing your Infrastructure

Before we start the import we need to get the environment ready. Let's deploy an Azure Resource Group and a static web app resource using the Azure CLI and try to import it into Terraform using Terrafy to prove the concept.

Create Azure Resource Group

az group create --location "uksouth" --name "terafform-import-rg" --subscription "7309f068-5a47-4a28-851c-09979529cd8e"

You should get an output in the CLI once the RG provisioning is successful:

Import your existing Azure infrastructure into Terraform using Azure Terrafy (8)

Create an Azure Static Web App Resource

Let's create an Azure Static Web App in the RG we created:

az staticwebapp create \-n terraform-import-webapp \ -g "terafform-import-rg" \

You should get an output in the CLI that the provisioning was successful. Validate the Static Web App is deployed by navigating to the Resource Group in the Azure Portal:

Import your existing Azure infrastructure into Terraform using Azure Terrafy (9)

Set up a new directory

Set up a new directory/folder where you want to Terrafy to store the Terraform files. By default it uses the root directory however you can also override this with a custom path.

1: Run mkdir <folder-name> to create a new folder.
2: Navigate into the directory above cd <path>

Import your existing Azure infrastructure into Terraform using Azure Terrafy (10)

Import the Infrastructure using Terrafy

Make sure you have logged into Azure using by running az login and set the subscription that contains the RG and web app created earlier by running az account set -s <subscription_id>

1: Run the import command

aztfy terafform-import-rg

Terrafy will begin the import process by scanning the RG and presenting you with a list of resources to import. Navigation instructions are given at the bottom of your shell window.

2: Select the resource you want to import. If you want to import all of them just hit W on your keyboard.

Import your existing Azure infrastructure into Terraform using Azure Terrafy (11)

In this case we want to import all our resources (static web app and RG) so we are going to hit W on our keyboard.

Import your existing Azure infrastructure into Terraform using Azure Terrafy (12)

Terrafy will begin the import process and the Terraform files will be stored in the root directory we created earlier. You will be prompted once import is complete:

Import your existing Azure infrastructure into Terraform using Azure Terrafy (13)

3: Verify the files have been generated:
Import your existing Azure infrastructure into Terraform using Azure Terrafy (14)

We can see we now have our state and Terraform files. Viewing the main.tf file shows the definition for our static web app and resource group:

Import your existing Azure infrastructure into Terraform using Azure Terrafy (15)

NOTE
You should note that currently Azure Terrafy does not support all resource types so if you see "skip" listed next to the resource this means it cannot be imported.

Validate the Terraform state

The final step is to validate that the Terraform state has been imported successfully by running Terraform.

1: Run terraform init
2: Run terraform plan
Import your existing Azure infrastructure into Terraform using Azure Terrafy (16)

We can see that Terraform is reporting that no changes are required and that our infrastructure in Azure matches our configuration or desired state in Terraform.

You can also specify a remote backend before running aztfy:

aztfy --backend-type=azurerm --backend-config=resource_group_name=<resource group name> --backend-config=storage_account_name=<account name> --backend-config=container_name=<container name> --backend-config=key=terraform.tfstate <importing resource group name>

We have successfully imported our infrastructure into Terraform using Azure Terrafy!

Limitations

  • Currently the configuration is imported into a single main.tf files. Modules should be considered.
  • Dependencies. Aztfy uses many of the dependencies in your resource group to map those out in the templates (see the 'depends_on' line declared in the main.tf above), much of that can be cleaned up once naming conventions and modules are implemented.
  • Currently you can only import at the resource group level however support will be added for subscriptions and management groups.
  • Not all resources are currently supported for the import process.

This is certainly a great start to fully automating the TF import process and the tool will develop and grow making it much easier for enterprises to get started in managing their existing infrastructure with Terraform.

The above import process can be fully automated by running all the tasks in a DevOps pipeline and specifying only the RG name. Terrafy supports non-interactive "batch" mode which is CI/CD friendly.

Import your existing Azure infrastructure into Terraform using Azure Terrafy (2025)

FAQs

How to import existing Azure infrastructure to Terraform? ›

Import Azure Terraform Tutorial — Step-by-Step Guide
  1. Create Cloud Provider Credentials. Start by creating credentials for your Azure account. ...
  2. Launch Import. Terraform import all existing resources. ...
  3. Resource Mapping. ...
  4. Generate Terraform Code. ...
  5. Cleanup and Verification. ...
  6. Final Steps. ...
  7. Design Considerations.
Jun 8, 2024

Is Terraform able to import existing infrastructure? ›

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

What is Azure Terrafy? ›

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

How do I provision Azure infrastructure with 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

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.

How do I import terraform configuration? ›

To import a resource using import blocks, you must:
  1. Define an import block for the resource(s).
  2. Add a corresponding resource block to your configuration , or generate configuration for that resource.
  3. Run terraform plan to review how Terraform will import the resource(s).

What are the limitations of Terraform import? ›

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.

Why use Terraform for infrastructure? ›

Using Terraform has several advantages over manually managing your infrastructure: Terraform can manage infrastructure on multiple cloud platforms. The human-readable configuration language helps you write infrastructure code quickly. Terraform's state allows you to track resource changes throughout your deployments.

What happens if 2 people are working on the same infrastructure with Terraform? ›

Terraform handles and prevents conflicts between multiple users working on the same infrastructure by using a state file to track provisioned resources and their current state.

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 do I import Azure infrastructure into 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.

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.

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

What CLI tool to generate terraform files from existing infrastructure? ›

Terraformer is a CLI level tool that allows you to easily create terraform scripts with already existing resources in your environment. Performing the reverse of what terraform is designed to do, this tool can be thought of as Infrastructure to Code.

How to use existing resource groups in terraform? ›

To use an existing Azure Resource Group without destroying it, you should import the resource group into your Terraform state.
  1. Remove the Resource Group resource from your code.
  2. Use Data Source to Reference the Resource Group.
  3. Reference the Data Source in Your Resources.
  4. Run terraform apply.
Jan 18, 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

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Duncan Muller

Last Updated:

Views: 5783

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Duncan Muller

Birthday: 1997-01-13

Address: Apt. 505 914 Phillip Crossroad, O'Konborough, NV 62411

Phone: +8555305800947

Job: Construction Agent

Hobby: Shopping, Table tennis, Snowboarding, Rafting, Motor sports, Homebrewing, Taxidermy

Introduction: My name is Duncan Muller, I am a enchanting, good, gentle, modern, tasty, nice, elegant person who loves writing and wants to share my knowledge and understanding with you.