Home > Net >  Terraform trying to replace existing resource
Terraform trying to replace existing resource

Time:04-19

Sorry if this is a dummy question but I'm new to terraform and I could find any documentation or anything that covers this scenario.

I have a Resource Group = rg1, which has a vnet = vnet1 and a subnet = subnet1.

I'm trying to create a new subnet (subnet2) in same vnet (vnet1), terraform is giving me the below error.

~ name = "subnet1" -> "subnet2" # forces replacement

Plan: 1 to add, 0 to change, 1 to destroy.

Could anyone please let me know why terraform replaces the already created subnet? Is there a possible work around?

Edit 1

Apologies for not including the code, please find the same below;

main.tf

resource "azurerm_resource_group" "rg" {
  name     = var.rgname
  location = var.rglocation
}

resource "azurerm_virtual_network" "vnet" {
  name                = var.vnetname
  address_space       = [var.vnet_address_space]
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

resource "azurerm_subnet" "vnet_subnets" {
  name                 = var.subnet_name
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = [var.subnet_prefix]
}

Tfvars

rglocation = "westus"
rgname     = "rg1"

vnetname           = "vnet1"
vnet_address_space = "10.0.0.0/16"

subnet_name   = "subnet2"
subnet_prefix = "10.0.2.0/24"

Variable.tf

variable "rglocation" {
}
variable "rgname" {
}
variable "vnetname" {
}
variable "vnet_address_space" {
}
variable "subnet_prefix" {
}
variable "subnet_name" {
}

Error Message

Terraform will perform the following actions:

  # azurerm_subnet.vnet_subnets must be replaced
-/  resource "azurerm_subnet" "vnet_subnets" {
      ~ address_prefix                                 = "10.0.1.0/24" -> (known after apply)
      ~ address_prefixes                               = [
          - "10.0.1.0/24",
            "10.0.2.0/24",
        ]
      ~ id                                             = "" -> (known after apply)
      ~ name                                           = "subnet1" -> "subnet2" # forces replacement
      - service_endpoint_policy_ids                    = [] -> null
      - service_endpoints                              = [] -> null
        # (4 unchanged attributes hidden)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

CodePudding user response:

This happens because you are basically overwriting your TF files. So previous subnet gets destroyed for the new one. The proper way of re-using the same code for different resources is through workspaces or by having different setups in different folders fully separated.

But it seems to me that in your case you want to add new subnet, so you will have two of them at the end. In that case, you should use count or for_each. This way you will create two subnets using a loop.

For that your variables should be lists:

subnet_name   = ["subnet1", "subnet2"]
subnet_prefix = ["10.0.1.0/24","10.0.2.0/24"]

then

resource "azurerm_subnet" "vnet_subnets" {
  count                = length(var.subnet_name)
  name                 = var.subnet_name[count.index]
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = [var.subnet_prefix[count.index]]
}
  • Related