Importing Terraform State in Azure

Brass Contributor


Some engineers start to provision services manually before they find out this might not be a good thing for the long run. So, they must use Terraform import.
If you are using Hashicorp’s Terraform to manage your infrastructure, you can bring existing resources that have been provisioned outside of Terraform.

This tuto help you to import Azure resources into a terraform state file. You can do that locally or if you want to initialise the tfstate in a remote local (form a Storage account)

 

So,  we are going to import a resource group, a virtual network and a subnet that are created manually; 

 

Screenshot from the portal (for the manual resources) 

MohamedT_Trabelsi_3-1653399586965.pngMohamedT_Trabelsi_4-1653399634048.png

 

 

First step : create a tf configuration file  MohamedT_Trabelsi_1-1653399147067.png  using manually created resource information (See Screenshot)

 

MohamedT_Trabelsi_2-1653399305347.png

 

***** For you information, we use a tfstate stored remotely in a storage account 

MohamedT_Trabelsi_5-1653399898657.png

 

Second Step : Import Resource details to terraform State 

 

After creating the configuration tf file, we can import these resources into it by using the "terraform import" command : 

 

terraform import terraform_id azure_resource_id

 

1- Resource Group : 

 

terraform import "azurerm_resource_group.rg_name_auto" "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/d-210-rg-ado-si-p-to-6"

You can find :

**The terraform_id 

MohamedT_Trabelsi_7-1653400338595.png

 

**The azure_resource_id 

MohamedT_Trabelsi_6-1653400187612.png

 

2-  The Vnet :

 

terraform import "azurerm_virtual_network.vnet_auto" "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxid/resourceGroups/d-210-rg-ado-si-p-to-6/providers/Microsoft.Network/virtualNetworks/d-210-vnet-ado-si-p-to-1"

 

3-The Subnet : 

 

terraform import azurerm_subnet.sub_auto /subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxid/resourceGroups/d-210-rg-ado-si-p-to-6/providers/Microsoft.Network/virtualNetworks/d-210-vnet-ado-si-p-to-1/subnets/d-210-snet-ado-si-p-to-2

 

So, before use this commands, please : 

1- access the code folder : cd folder_code

2- connect to the subscription (where you have deployed the manual resources) : 

az login 

Select-AzSubscription -SubscriptionId "copy-past the id of the subsc" 

 

MohamedT_Trabelsi_9-1653400890785.png

 

3-  Terraform init : 

 

 terraform init -backend-config storage_account_name=xxxxxxxx -backend-config container_name=tfstate -backend-config resource_group_name=xxxxxxxx -backend-config key=xxxxxxx.tfstate

 

MohamedT_Trabelsi_10-1653401157174.png

 

Okey, now we can lunch the commands for import config : 

 

RG : 

MohamedT_Trabelsi_11-1653401310546.png

 

Vnet: 

MohamedT_Trabelsi_12-1653401408578.png

 

Subnet : 

MohamedT_Trabelsi_13-1653401513658.png

 

Now, you can see the result :  terraform state list

 

MohamedT_Trabelsi_15-1653401679963.png

 

you can see the content of each imported resource via the following commands: 

 

terraform state show azurerm_resource_group.rg_name_auto

 

terraform state show azurerm_virtual_network.vnet_auto

 

terraform state show azurerm_subnet.sub_auto

 

Third Step : Test by running the terraform plan 

MohamedT_Trabelsi_16-1653402119458.png

 

Finally, we can verify the imported resources in the tfstate file.

For example, we can see the imported subnet. 

MohamedT_Trabelsi_17-1653402305040.png

 

The purpose of this tutorial is to know the steps to import resources that are manually configured to the tfstate file

 

 

0 Replies