If you find yourself with an existing Azure Resource Group that you need to import into a Terraform project, you can use the terraform import command to do it. This article goes through the steps necessary to take you through the process of importing an existing Azure Resource Group into a Terraform project. When imported, the state for the Terraform project will have information about the existing Azure Resource Group, and you will be able to start using Terraform to manage this existing Azure resource.


Import Existing Azure Resource Group into Terraform Project

Let’s walk through the steps necessary to import an existing Azure Resource Group into a Terraform project.

Step 1: Declare Azure Resource Group in Terraform

First, the Terraform code needs to be added to the Terraform project to define the Azure Resource Group that will be imported.

resource azurerm_resource_group build5nines_web {
  name = "SCUS-prod-build5nines-web-rg"
}

This example is showing an Azure Resource Group named SCUS-prod-build5nines-web-rg. You’ll need to make sure to specify the name of your Azure Resource Group to be imported. And, we’ve named the Terraform resource build5nines_web.

This example also assumes you’re using the AzureRM Terraform Provider to manage the Azure Resources within the Terraform project. With this provider, the type for an Azure Resource Group is the azurerm_resource_group resource type; as is seen in this code example.

Step 2: Retrieve Azure Resource ID

Next, you need to grab the Resource ID for the Azure Resource Group.

Use the Azure Portal

This can be done by looking at the Azure Resource Group pane in the Azure Portal, then clicking on Settings -> Properties on the left of the blade. On the Properties pane, you can locate and copy the Resource ID. Be sure to copy the entire value.

Terraform: Import Existing Azure Resource Group 3
Resource ID highlighted within the Resource Group pane of the Azure Portal

Use the Azure CLI

Alternatively, the Resource ID for the Azure Resource Group can also be retrieved from the console using the Azure CLI using the following command while specifying the name of the Resource Group:

az group show -n SCUS-prod-build5nines-web-rg

The Azure CLI az group show command will output the details of the Azure Resource Group in JSON format. You can find the Resource ID as the value of the id property of the JSON object.

Terraform: Import Existing Azure Resource Group 4
Azure CLI output with ‘id’ of the Azure Resource Group highlighted

Step 3: Run Terraform Import

Now that you have the Azure Resource Group defined within the HashiCorp Terraform code, and you’ve retrieved its Resource ID, you can now run the terraform import command to import the existing Azure Resource Group into the Terraform state.

terraform import "azurerm_resource_group.build5nines_web" "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SCUS-prod-build5nines-web-rg"

Be sure to use the correct Terraform resource name and Azure Resource ID for your existing Azure Resource Group. Also, the terraform init command will need to be run on the Terraform project before the terraform import command can be run.

Once this command finishing running successfully, the existing Azure Resource Group will now be imported into the Terraform state for the Terraform project. After this is done, you’ll want to make sure you continue to use Terraform to manage the Azure Resource Group resource going forward so the Terraform state remains consistent.

Related: You can read the “Terraform: Import Existing Azure Resources into State (.tfstate)” article for more information about importing Azure resources into a Terraform project.

Microsoft MVP

Chris Pietschmann is a Microsoft MVP (Azure & IoT) and HashiCorp Ambassador (2021) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to Fortune 100. He is also a Microsoft Certified Azure Solutions Architect and developer, a Microsoft Certified Trainer (MCT), and Cloud Advocate. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador (2021) Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect