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
- Prerequisites
- Set Up Terraform
- Initialize Terraform
- Import Resource Group
- Import Virtual Network
- Verify and Manage Resources
- 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)
Set Up Terraform
- Open Visual Studio Code and create a new directory for your Terraform project.
- Inside the directory, create a new file named
main.tf
and define the provider block for Azure:
provider "azurerm" {
features {}
}
- 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"]
}
Initialize Terraform
Navigate to your Terraform project directory in the terminal and run:
terraform init
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:
- 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.
- Run the following command to import the resource:
terraform import azurerm_resource_group.KingRG "/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/KingRG"
Replace YOUR_SUBSCRIPTION_ID
with your actual subscription ID.
- Verify the import by running:
terraform state list
Import Virtual Network
Next, import the existing KingVnet Virtual Network:
- 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.
- Run the following command:
terraform import azurerm_virtual_network.KingVnet "/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/KingRG/providers/Microsoft.Network/virtualNetworks/KingVnet"
- Verify the import:
terraform state list
Verify and Manage Resources
Now that the resources are imported, run:
terraform plan
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
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)