I found this blog post that references using the removed block to remove single instances of a resource that was created with count or for_each but also includes a method to remove all instances by simply specifying the resource without an identifier (e.g. kubernetes_secret.docker).
However, when trying this, I get the error:
`A single static variable reference is required: only attribute access and indexing with constant keys. No calculations, function calls, template expressions, etc are allowed here.`
Why would it care if the names were calculated if it is removing all instances?
I posted this on an open Github issue, but was redirected here.
Can you show what you tried to get that error?
Using the resource address as the from argument will remove all instances of that resource.
So, the resource is defined in one of our TF modules like so:
resource "kubernetes_secret" "docker_config" {
for_each = local.docker_registries_default_namespace
The local.docker_registries_default_namespace is defined like so:
docker_registries_default_namespace = var.use_legacy_vault ? merge(
{ for registry in local.docker_registry_list_default_namespace : "${registry.name}-${registry.namespace}" => registry },
{ for registry in local.additional_docker_registry_list_default_namespace : "${registry.name}-${registry.namespace}" => registry }
) : {}
I am assuming it is complaining because we are using a for loop in the definition of the instance name that is used in the for_each loop.
Here was the removed block that I tried:
removed { from = "kubernetes_secret.docker_config" lifecycle { destroy = false } }
Looks like a simple typo, and not related to the for_each expression. The from argument is a reference to a resource, not a string. The syntax you want is
removed {
from = kubernetes_secret.docker_config
lifecycle {
destroy = false
}
}
Well, isn’t that annoying? lol
You are correct. That is what I get for copying anything AI spits out.
You have to use a fake moved block and then a removed block.
Move to something that could exist. It has to be a valid reference with provider resource value and key.
To = provider_resource_name.this
Then you can remove it.