hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.6k stars 4.64k forks source link

azurerm_route_table Detects recent apply as Changes outside of terraform #22796

Open kevinrosal opened 1 year ago

kevinrosal commented 1 year ago

Is there an existing issue for this?

Community Note

Terraform Version

latest

AzureRM Provider Version

latest

Affected Resource(s)/Data Source(s)

azurerm_route_table

Terraform Configuration Files

resource "azurerm_route_table" "sharedroutetable" {
  for_each = { for idx, route_table in var.route_tables : idx => route_table }

  name                          = each.value.name
  resource_group_name           = var.resource_group_name
  location                      = local.location
  disable_bgp_route_propagation = each.value.disable_bgp_route_propagation
  route                         = each.value.routes
  tags                          = local.tags
}

resource "azurerm_subnet_route_table_association" "sharedroutetable_assoc" {
  count = length(var.subnet_ids) * length(azurerm_route_table.sharedroutetable)

  subnet_id      = var.subnet_ids[count.index % length(var.subnet_ids)]
  route_table_id = values(azurerm_route_table.sharedroutetable)[count.index % length(azurerm_route_table.sharedroutetable)].id

  depends_on = [azurerm_route_table.sharedroutetable]
}

Debug Output/Panic Output

Note: Objects have changed outside of Terraform

Terraform detected the following changes made outside of Terraform since the
last "terraform apply":

  # module.sharedroutetable_module.azurerm_route_table.sharedroutetable["0"] has changed
  ~ resource "azurerm_route_table" "sharedroutetable" {
        id                            = "/subscriptions/8a124c97-c837-4bfd-bd19-56946813d534/resourceGroups/ISO-CLOUDLAB-01-CACN-RGP-KROSAL/providers/Microsoft.Network/routeTables/test-route"
        name                          = "test-route"
      ~ subnets                       = [
          + "/subscriptions/8a124c97-c837-4bfd-bd19-56946813d534/resourceGroups/ISO-CLOUDLAB-01-CACN-RGP-KROSAL/providers/Microsoft.Network/virtualNetworks/test-vnet/subnets/default",
          + "/subscriptions/8a124c97-c837-4bfd-bd19-56946813d534/resourceGroups/ISO-CLOUDLAB-01-CACN-RGP-KROSAL/providers/Microsoft.Network/virtualNetworks/test-vnet/subnets/default2",
          + "/subscriptions/8a124c97-c837-4bfd-bd19-56946813d534/resourceGroups/ISO-CLOUDLAB-01-CACN-RGP-KROSAL/providers/Microsoft.Network/virtualNetworks/test-vnet/subnets/default3",
        ]
        tags                          = {
            "_Business-Unit"   = "Cloud Architecture"
            "_Environment"     = "LAB"
            "_GL-Code"         = "KROSAL"
            "_Primary-Contact" = "Kevin Rosal"
            "_Project-Name"    = "Test"
            "_Review-Date"     = "2023-12-31"
        }
        # (4 unchanged attributes hidden)
    }

Expected Behaviour

The expected behavior is after the apply, no changes outside of terraform should be showing up.

Actual Behaviour

After doing the apply and the associations for the said subnets. re-running a plan or apply always shows the most recent apply as changes outside of terraform specifically for "subnets"

Steps to Reproduce

No response

Important Factoids

No response

References

No response

wuxu92 commented 1 year ago

hi @kevinrosal thanks for submitting this issue. I think this is an as-expected behavior for the azurerm_subnet_route_table_association will update the route_table resource (add subnet id to the route_table).

could you please confirm that you are using the latest terraform, as recommended in this document, this message should not be streamed when running terrafrom plan. and if you are using an older version of terraform, please run terraform apply -refresh-only to update the state file.

kevinrosal commented 1 year ago

hi @wuxu92 thanks for the response, its a little annoying that everytime we add new subnets to the route table and re-run a plan it detects recent apply as changes outside of terraform. it causes confusions, cause in a real-world scenario after the apply. re-running plan should say no changes outside of terraform.

Yes i tested this with latest terraform, while i do know that doing terraform apply -refresh-only updates the state file but we really shouldnt we doing this everytime we do an apply.

kevinrosal commented 1 year ago

Any updates on this? issue is still occuring