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.
Looking to import multiple Azure resources into a Terraform project? The Terraform: Import Existing Infrastructure article provides expanded guidance on importing many resources into a Terraform project, including auto generating the Terraform HCL code for those resources!
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.
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.
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.
Original Article Source: Terraform: Import Existing Azure Resource Group by Chris Pietschmann (If you’re reading this somewhere other than Build5Nines.com, it was republished without permission.)