While applying my terraform script, I’m getting the following error:
╷
│ Error: A resource with the ID "/subscriptions/***/resourceGroups/rg-subnet-test/providers/Microsoft.Network/virtualNetworks/subnet-test-vnet/subnets/subnet-test-subnet" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_subnet_network_security_group_association" for more information.
│
│ with azurerm_subnet_network_security_group_association.association1,
│ on main.tf line 47, in resource "azurerm_subnet_network_security_group_association" "association1":
│ 47: resource "azurerm_subnet_network_security_group_association" "association1" {
│
╵
This is my script:
# Resource Group
resource "azurerm_resource_group" "rg_subnet_test" {
location = "germanywestcentral"
name = "rg-subnet-test"
}
# VNET
resource "azurerm_virtual_network" "vnet_subnet_test" {
address_space = ["10.100.2.0/24"]
location = azurerm_resource_group.rg_subnet_test.location
name = "subnet-test-vnet"
resource_group_name = azurerm_resource_group.rg_subnet_test.name
}
# Subnet
resource "azurerm_subnet" "subnet_germany" {
name = "subnet-test-subnet"
address_prefixes = ["10.100.2.0/24"]
resource_group_name = azurerm_virtual_network.vnet_subnet_test.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet_subnet_test.name
}
# First Network Security Group
resource "azurerm_network_security_group" "app_server_nsg_1" {
name = "subnet-test-nsg-1"
location = azurerm_resource_group.rg_subnet_test.location
resource_group_name = azurerm_resource_group.rg_subnet_test.name
}
# Second Network Security Group
resource "azurerm_network_security_group" "app_server_nsg_2" {
name = "subnet-test-nsg-2"
location = azurerm_resource_group.rg_subnet_test.location
resource_group_name = azurerm_resource_group.rg_subnet_test.name
}
# Association between first NSG to subnet
resource "azurerm_subnet_network_security_group_association" "association1" {
subnet_id = azurerm_subnet.subnet_germany.id
network_security_group_id = azurerm_network_security_group.app_server_nsg_1.id
}
# Association between second NSG to subnet
resource "azurerm_subnet_network_security_group_association" "association2" {
subnet_id = azurerm_subnet.subnet_germany.id
network_security_group_id = azurerm_network_security_group.app_server_nsg_2.id
}
I don’t understand the error, because I only create the subnet once. Why is the second association trying to create the subnet again? What am I doing wrong here?
2
Answers
As mentioned in the error we need to import the subnet resource as per the requirement as this only the approach which works. When I test this configuration, I face the similar issue you mentioned
terraform_move command was wont work here as we need move the configuration changes as per the resource we need to move in the state file as Rui Jarimba mentioned it better remove the resource info from the state and reimport it as per the requirement only works as expected.
The error we are faced only due to miss match of configuration with the state which we tried not with the way the resources are provisioned.
configuration goes as per the repo you shared in github i only shared the part of the configuration where we import the resource.
configuration:
modules/german/main.tf
modules/network/main.tf:
continue with the rest of the configuration
Deployment:
run the command
terraform state list
remove the resource
this resource was imported into different module as per the requirement
Refer:
https://github.com/azurerm/terraform-azure-resources/tree/v1.0.10/modules/subnet_network_security_group_association
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_network_security_group_association
https://github.com/Azure/terraform-azurerm-network-security-group
There are several possible scenarios that can lead to this error:
Please refer to Vinay B’s answer or Moving resources for details on how to fix this issue.