Do you have a cloud environment that needs to import existing azure resources into terraform?
Importing Azure resources into Terraform is the bridge between unmanaged “click-ops” deployments in the Azure Portal and fully governed Infrastructure as Code (IaC). At its core, the import process establishes a binding between three distinct entities: the Live Azure Resource, the Terraform State File, and the HCL Configuration Code.
Importing manually created Azure Portal resources into Terraform brings click-ops infrastructure under Infrastructure as Code (IaC) governance. The import process requires mapping the Azure Resource ID to two components: Terraform State (so Terraform tracks it) and Terraform Configuration (.tf code) (so Terraform knows how to manage it).
Let’s dive into it:
- Sign in to Microsoft Azure Portal
• On your browser key into https://portal.azure.com
• Log in with your Microsoft account (personal or work or school) - Search bar has been provided to make the work easier for you: just input what you want to do. E.g. resource
groups, subscription, virtual machine, etc.

- Click on Resource groups.
• To create a new resource group →click on create
• Fill in the details.
✓ Select the Subscription.
✓ Enter your Resource Group name e.g. Ernest-RG
✓ Choose the region e.g. East US, Africa South North. - Add Tags (optional)
✓ Key value that makes it easier to track ownership, projects, reporting and costs.
- Review and create.
✓ This helps you to review everything before deploying. - Click on create once all entries are confirmed.

- Let open the newly created resource group →settings →properties →Resource ID
• Resource ID is what we will import.

Once the resource group is created in the Azure portal let’s hit back to our vs code;
- On our folder, these files were created main.tf, variables.tf and provider.tf•
- Provider.tf: terraform required provider→ azurerm→ random string →provider called Azurerm

- Variables.tf:

Variable for resource group and location,
- Main.tf: In the main.tf file, resource group which was created in the portal that we’re importing a resource virtual network into it.
• The state file of the resource group is in the cloud, and the virtual network is in the local host.
• Run terraform init to pull down the required provider plugins and initiate it.
• Run az login to connect Azure CLI onto your azure account being it work or school or personal account.


- Run terraform plan to lay down all the execution plan ready to be created with the command terraform apply.
- Run terraform import with the name of the resource in the cloud with the Resource ID of it.
✓ Inside the VS code:
import{
to = azurerm_resource_group.rg
id = “subscriptions/subscription_id /resourceGroups/Ernest-RG”
}
This spells out where to generate the Resource ID from and import
✓ On your terminals run it.

- Run terraform apply to create the new resource inside the imported state file.
✓ By the value, yes validate all resources in the process of creating.

After creation, let’s confirm the resources from the portal

Once this link is established, Terraform assumes full management lifecycle ownership over that Azure Portal resource.
