Forum Discussion
MohamedT_Trabelsi
May 24, 2022Brass Contributor
Importing Terraform State in Azure
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)
First step : create a tf configuration file using manually created resource information (See Screenshot)
***** For you information, we use a tfstate stored remotely in a storage account
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
**The azure_resource_id
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"
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
Okey, now we can lunch the commands for import config :
RG :
Vnet:
Subnet :
Now, you can see the result : terraform state list
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
Finally, we can verify the imported resources in the tfstate file.
For example, we can see the imported subnet.
The purpose of this tutorial is to know the steps to import resources that are manually configured to the tfstate file
- cristhianbiniCopper Contributor
Obrigado por compartilhar seus conhecimentos!