Terraform import of existing resources in Azure

We tried to import the multiple resources of same resource type using the below commands to the state file. Though it says the resource is imported successfully to the state file, we can’t see the resource in the state file. And when we execute Terraform plan, it is still recognizing the resources as new creation.

terraform import module.resource_group.azurerm_resource_group.resource_group /subscriptions/3221edxx-6712-1d80-b763-rt42b2gfdb1b/resourceGroups/RGName1

terraform import ‘module.resource_group.azurerm_resource_group.resource_group[“RGName2”]’ /subscriptions/3221edxx-6712-1d80-b763-rt42b2gfdb1b/resourceGroups/RGName2

With For each loop

terraform import module.local_network_gateway.azurerm_local_network_gateway.local_network_gateway /subscriptions/3221edxx-6712-1d80-b763-rt42b2gfdb1b /resourceGroups/GxSSGCXNet/providers/Microsoft.Network/localNetworkGateways/Portal-PROD1,Portal-PROD2,Portal-PROD3

Terraform Plan results: Below is for Resource groups

module. resource_group.azurerm resource_group.resource_group must be replaced

-/+ resource “azurerm_resource group” " resource_group" {

id = " /subscriptions/3221edxx-6712-1d80-b763-rt42b2gfdb1b/resourceGroups/RGName1" → (known after apply)

name = “RGName1” → “RGName2” # forces replacement

(1 unchanged attribute hidden)

  • timeouts {}


Terraform Plan results: Resources are not available in state file and it recognizes as new resources for Local network gateway

There is insufficient information here to give a clear answer, as you haven’t included the relevant portions of Terraform source code that define the resources you are importing.

However I do spot one thing that looks odd:

This says you imported once to the resource address without any square-bracketed for_each key, and once with - that doesn’t seem right.

Loop over what? Changing what?

Hello Max, Module code is given below for the resource group which we are trying to import.

When we tried the terraform import with same resource type without square braces, below message is displayed.

│ Error: Resource already managed by Terraform

│ Terraform is already managing a remote object for module.resource_group.azurerm_resource_group.resource_group. To import to this address you must first remove the
│ existing object from the state.

What will be the recommended approach for declaring the multiple resource groups in tfvars file and import the existing resources to the state file ?

Please also show the code that calls your modules/azure/resources/resource-group module.

Copy/pastes of text, with each block surrounded by ``` markers to make it a code block, are generally much better than image screenshots, as then it is possible to quote sections when responding.

Here is the Module Code:


resource "azurerm_resource_group" "resource_group" {
  location = var.resource_group_location
  name = "${var.resource_group_name_prefix}${var.resource_group_name}"

  tags  = merge({
    "tag_component_name" : "${var.resource_group_name_prefix}${var.resource_group_name}"
  }, {for common_tag, common_tag_val in var.resource_common_tags: "${common_tag}" => "${common_tag_val.value}"
  }, {for tag, val in var.resource_group_tags: "${tag}" => "${val.value}"})


output "resource_group_name" {
  value = azurerm_resource_group.resource_group.name

output "resource_group_location" {
  value = azurerm_resource_group.resource_group.location


variable "resource_group_name_prefix" {
  description = "Prefix of the resource group name."

variable "resource_group_name" {
  description = "Resource group name."

variable "resource_group_location" {
  description = "Location of the resource group."

variable "resource_common_tags" {
  type = map(object({
    value = string
  description = "Define resource comman tags."
  default = {}

variable "resource_group_tags" {
  type = map(object({
    value = string
  description = "Define resource group tags."
  default = {}

Code calling the resource group module:

module "resource_group" {
  source = "../../modules/azure/resources/resource-group"
  resource_group_name_prefix = var.resource_name_prefix
  resource_group_name        = var.resource_group_name
  resource_group_location    = var.resource_location
  resource_common_tags = var.resource_common_tags
  resource_group_tags        = var.resource_group_tags

The code that you have shown us here creates one and only one resource group.

Therefore you cannot import multiple resource groups into one Terraform state for use along with this code, because that is not what the code has been written to support.

What will be the recommended approach for declaring the multiple resource groups and import the existing resources to the state file?

Any articles will be helpful.