Changing implicit dependencies of a resource without destroying the resource

Terraform version 0.13.7

I have a resource which uses a provider that is configured using the outputs of a module. According to the state file, my dependent resource directly depends on the resource of that module:

module "service_account_old" { ... }

provider "kafka" {
  bootstrap_servers = [module.service_account_old.bootstrap_servers]
  sasl_username     = module.service_account_old.sasl_user
  sasl_password     = module.service_account_old.sasl_password
}

resource "kafka_topic" "my_topic" {
  name      = "my-topic"
  lifecycle {
    prevent_destroy = true
  }
}

In the state file, I can see the dependency for the topic as "dependencies": ["module.service_account_old.restapi_object.service_account"]

Now, I want to replace the old module with a new one, so I have applied the following:

module "service_account_old" { ... }

module "service_account_new" { ... } # Has no references to old module

provider "kafka" {
  bootstrap_servers = [module.service_account_new.bootstrap_servers]
  sasl_username     = module.service_account_new.sasl_user
  sasl_password     = module.service_account_new.sasl_password
}

resource "kafka_topic" "my_topic" {
  name      = "my-topic"
  lifecycle {
    prevent_destroy = true
  }
}

Even after the apply, the kafka_topic dependency still points to the old module, and thus when I attempt to destroy the old module (which I have to do with a manual target since it has a nested provider), it also tries to destroy the kafka_topic resource, which is blocked due to it’s lifecycle config.

My question is, why does the topic resource still have the old module resources as a dependency? Is there some way for me to change this dependency without destroying the kafka_topic resource?

I think I have found a workaround. By applying an update to the kafka_topic resource now that the provider is configured with the new module output, the dependency has updated.

After the update is applied, I am able to destroy the old module without issues, since the dependency is broken.

I am still left wondering why the update needed to happen to the resource in order to update its dependency. I would have expected that to happen after the provider config was updated.

Hi @stalbot15,

First thing I notice is that you are using a very old version of Terraform. The tracking of dependencies has been refined since that release, and the experience may be different with a current release.

The general idea however, is when resources are being destroy, the dependencies stored in the state take precedence when possible. Changes to the configuration may not leave enough information to determine what the original dependencies were, or the new configuration may even conflict with the stored dependencies. Significant changes to the dependencies (especially if they are being reversed) may still require refreshing the state to solidify the ordering you are looking for, before making changes which removes or replaces resources.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.