Terraform - 重构模块 : Error: Provider configuration not present

标签 terraform terraform-modules

我正在重构一些 Terraform 模块并得到:

Error: Provider configuration not present

To work with
module.my_module.some_resource.resource_name its
original provider configuration at
module.my_module.provider.some_provider.provider_name is required, but it
has been removed. This occurs when a provider configuration is removed while
objects created by that provider still exist in the state. Re-add the provider
configuration to destroy
module.my_module.some_resource.resource_name, after
which you can remove the provider configuration again.

看来我需要从 tfstate 文件中删除该资源,然后使用新的 tf 配置重新添加它。

当我重构一些整体代码时,有数百条这样的错误:提供程序配置不存在消息。

有删除和重新添加的快捷方式吗?

最佳答案

正如错误消息所解释的那样,Terraform 检测到仍有资源对象处于其提供程序配置不可用的状态,因此它没有足够的信息来销毁这些资源。

在这种特殊情况下,发生这种情况似乎是因为您的子模块之一中有一个 provider 配置 block 。虽然这是为了与旧版本的 Terraform 兼容而允许的,it's recommended to only have provider blocks in your root module以便它们始终比提供者管理的任何资源实例的生命周期更长。

如果您的目的是销毁module.my_module中的资源实例,那么您必须在删除module“my_module” block 之前执行此操作从根模块。这是一种不寻常的情况,我们可以使用 -target 来帮助 Terraform 理解我们希望它做什么:

terraform destroy -target=module.my_module

一旦所有这些对象都被销毁,您应该能够删除模块“my_module” block ,而不会看到“提供程序配置不存在”错误,因为在依赖于该提供者配置的状态。

如果您的目标是将资源 block 移动到另一个模块中,则另一个可能的解决方案是使用 terraform state mv 指示 Terraform 跟踪新地址下的现有对象:

terraform state mv 'module.my_module.some_resource.resource_name' 'module.other_module.some_resource.resource_name'

同样,最好在删除旧模块之前执行此操作,以便旧的提供程序配置仍然存在,直到没有任何内容可供管理。将现有对象移动到状态中的新模块并在配置中为其放置一个 resource block 后,Terraform 应该了解您使用不同的提供程序配置来管理此资源的意图现在,您可以安全地删除旧的 module block ,以及其中的 provider block 。

关于Terraform - 重构模块 : Error: Provider configuration not present,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58393294/

相关文章:

kubernetes - 如何获取 `provider "kubernetes "` authenticate with ` google_container_cluster`

azure - 如何使用 Terraform 和 Azure 强制删除/释放网络服务组?

terraform - 有条件地在 Terraform 中创建单个模块

azure - Terraform 模块 rbac 分配

terraform - 条件模块创建 terraform

amazon-web-services - 具有多个监听器的 Terraform 负载均衡器

amazon-web-services - 使用 Terraform 在新创建的虚拟机上创建 Docker 容器

amazon-web-services - AWS 中的 Cloudformation/Serverless 与 Terraform

terraform-provider-azure - Terraform 输出变量用作输入变量

azure - Terraform 忽略具有 terraform 条件值的资源内的嵌套 block