Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

Terraform - refactoring modules: Error: Provider configuration not present

I'm refactoring some Terraform modules and am getting:

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.

It seems like I need to remove that resource from the tfstate file then re-add it with the new tf config.

As I'm refactoring some monolithic code there are hundreds of these Error: Provider configuration not present messages.

Any shortcut to removing and re-adding?

question from:https://stackoverflow.com/questions/58393294/terraform-refactoring-modules-error-provider-configuration-not-present

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As the error message explains, Terraform has detected that there are resource objects still present in the state whose provider configurations are not available, and so it doesn't have enough information to destroy those resources.

In this particular case, that seems to be occurring because there is a provider configuration block in one of your child modules. While that is permitted for compatibility with older versions of Terraform, it's recommended to only have provider blocks in your root module so that they can always outlive any resource instances that the provider is managing.

If your intent is to destroy the resource instances in module.my_module then you must do that before removing the module "my_module" block from the root module. This is one unusual situation where we can use -target to help Terraform understand what we want it to do:

terraform destroy -target=module.my_module

Once all of those objects are destroyed, you should then be able to remove the module "my_module" block without seeing the "Provider configuration not present" error, because there will be no resource instances in the state relying on that provider configuration.

If your goal is to move resource blocks into another module, the other possible resolution here is to use terraform state mv to instruct Terraform to track the existing object under a new address:

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

Again, it's better to do this before removing the old module, so that the old provider configuration remains present until there's nothing left for it to manage. After you've moved the existing object into a new module in the state and have a resource block in place for it in the configuration, Terraform should understand your intent to manage this resource with a different provider configuration from now on and you can safely remove the old module block, and thus the provider block inside it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...