DEV Community

Cover image for Terraform Import Made Easy: Bring Your Azure Resources Under IaC!
kristarking
kristarking

Posted on

Terraform Import Made Easy: Bring Your Azure Resources Under IaC!

Managing cloud resources efficiently is a key skill for DevOps engineers. If you've already created Azure resources manually and want to bring them under Terraform's management, importing them is the way to go. In this guide, I'll walk you through importing an existing Azure Resource Group and Virtual Network into Terraform.

Table of Contents

  1. Prerequisites
  2. Set Up Terraform
  3. Initialize Terraform
  4. Import Resource Group
  5. Import Virtual Network
  6. Verify and Manage Resources
  7. Conclusion

Prerequisites

Before we begin, ensure you have the following:

  • Azure CLI installed and authenticated (az login)
  • Terraform installed on your machine
  • Visual Studio Code (VS Code) for writing Terraform configurations
  • An existing Azure Resource Group and Virtual Network (created manually)

Azure resources created manually Image description

Azure resources created manually Image description 2

Set Up Terraform

  1. Open Visual Studio Code and create a new directory for your Terraform project.
  2. Inside the directory, create a new file named main.tf and define the provider block for Azure:
   provider "azurerm" {
     features {}
   }
Enter fullscreen mode Exit fullscreen mode
  1. Add the Terraform configuration for your Resource Group and Virtual Network:
   resource "azurerm_resource_group" "KingRG" {
     name     = "KingRG"
     location = "Central US"
   }

   resource "azurerm_virtual_network" "KingVnet" {
     name                = "KingVnet"
     location            = azurerm_resource_group.KingRG.location
     resource_group_name = azurerm_resource_group.KingRG.name
     address_space       = ["10.0.0.0/16"]
   }
Enter fullscreen mode Exit fullscreen mode

Azure resources on terraform Image description

Initialize Terraform

Navigate to your Terraform project directory in the terminal and run:

terraform init
Enter fullscreen mode Exit fullscreen mode

terraform Image description

This initializes Terraform, downloads required provider plugins, and prepares your project for execution.


Import Resource Group

To import the existing KingRG Resource Group into Terraform state:

  1. Retrieve the resource group’s Azure ID:
    • Go to the Azure Portal → Resource Groups → Click on KingRG.
    • Select Json Template and copy the id field.

json Image description

  1. Run the following command to import the resource:
   terraform import azurerm_resource_group.KingRG "/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/KingRG"
Enter fullscreen mode Exit fullscreen mode

Replace YOUR_SUBSCRIPTION_ID with your actual subscription ID.

import Image description

Import Image description 2

  1. Verify the import by running:
   terraform state list
Enter fullscreen mode Exit fullscreen mode

Import Virtual Network

Next, import the existing KingVnet Virtual Network:

  1. Retrieve the Virtual Network’s Azure ID:
    • Go to the Azure Portal → Virtual Networks → Click on KingVnet.
    • Select Jason Template and copy the id field.

Azure console Image description

  1. Run the following command:
   terraform import azurerm_virtual_network.KingVnet "/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/KingRG/providers/Microsoft.Network/virtualNetworks/KingVnet"
Enter fullscreen mode Exit fullscreen mode

Vnet import Image description

  1. Verify the import:
   terraform state list
Enter fullscreen mode Exit fullscreen mode

Verify and Manage Resources

Now that the resources are imported, run:

terraform plan
Enter fullscreen mode Exit fullscreen mode

This will show any differences between your Terraform configuration and the actual Azure state. If needed, modify main.tf to match the current state of your Azure resources.

To apply any necessary updates, run:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Conclusion

By following this guide, you’ve successfully imported existing Azure resources into Terraform. This allows you to manage them as Infrastructure as Code (IaC), ensuring consistency and automation in your deployments.

Top comments (0)