Photo by chuttersnap on Unsplash

How to Import an Existing Azure Resource in Terraform

Guillermo Musumeci

--

In this story, we will learn how to import an existing Azure Resource in Terraform.

This allows us to use resources that we have created by some other means and bring it under Terraform management.

Update February 27, 2020: This procedure is valid for both AzureRM v1.x and AzureRM v2.x.

How to Import an Existing Azure Resource in Terraform

Terraform import can only import resources into the state. It does not generate configuration.

Importing an Existing Azure Resouce Group

We have an existing Azure Resource Group called late-motiv-rg, in the Azure Portal, we click on the properties and copy the Resource ID.

Or from the Azure Cloud Shell, we run the following command:

az group show --name late-motiv-rg --query id --output tsv

The result is the ID of the subscription:

az group show --name late-motiv-rg --query id --output tsv
/subscriptions/8c247875-6978-4ff9-899b-2aab2f4h7443/resourceGroups/late-motiv-rg

We will create the file import.tf, used to manage imported variables:

resource "azurerm_resource_group" "late-motiv-rg" {}

We initialize Terraform:

terraform init

then from the Azure CLI or Azure Cloud Shell type:

terraform import azurerm_resource_group.late-motiv-rg /subscriptions/8c247875-6978-4ff9-899b-2aab2f4h7443/resourceGroups/late-motiv-rg

this is the result of the import process:

terraform import azurerm_resource_group.late-motiv-rg /subscriptions/8c247875-6978-4ff9-899b-2aab2f4h7443/resourceGroups/late-motiv-rg
azurerm_resource_group.late-motiv-rg: Importing from ID "/subscriptions/8c247875-6978-4ff9-899b-2aab2f4h7443/resourceGroups/late-motiv-rg"...
azurerm_resource_group.late-motiv-rg: Import prepared!Prepared azurerm_resource_group for import…

--

--

Guillermo Musumeci

Certified AWS, Azure & GCP Architect | HashiCorp Ambassador | Terraform SME | KopiCloud Founder | ex-AWS | Entrepreneur | Book Author | Husband & Dad of ✌