Search code examples
openstackterraform

Terraform openstack forces new resource


Im trying to deploy some infrastructure via terraform. The first time i run apply it deploys the vm no problem but if i run apply again it wants to run destroy and redeploy the vm even though there has been no changes to the configuration. What i can see from the output this is because of id and network.o.name and im not sure why this is causing an issue

resource "openstack_compute_instance_v2" "test" {
  name            = "test_server"
  image_id        = "image_id"
  flavor_id       = "flavour_id"
  key_pair        = "test"
  security_groups = ["default"]

  network {
    name = "Default Network"
  }
}

id: "16342cd6-7f26-4a77-9c3f-11c626400f4f" => (forces new resource)

network.0.name: "Default network" => "Default Network" (forces new resource)


Solution

  • I've not used OpenStack for a long time but it looks like the network name is not case sensitive in OpenStack so your instance is being put in the Default network network despite having a different cased network name specified in the configuration (Default Network).

    Because the network.name attribute is set as ForceNew then Terraform detects the drift between the configuration and the provider and says it needs to create a new resource. Simply updating your configuration to match what the provider is using should be fine in this case.

    The normal way to fix this properly in a provider is to use a DiffSuppresFunc which makes Terraform ignore the diff between configuration and provider. The openstack_compute_instance_v2 resource already uses this to suppress differences in availability zones.