I have a legacy module that declares an aws provider inline. This is now considered poor practice. I am trying to refactor my Terraform so that the provider is declared at the root level, instead, and passed into the module.
What I have now:
root:
provider "aws" {
region = "us-west-2"
}
module "foo" {
source = "foo-source"
}
foo-source/provider.tf:
provider "aws" {
alias = "foo"
region = "us-east-1"
}
foo-source/foo.tf:
resource "aws_instance" "my_instance" {
provider = aws.foo
# ...
}
What I want to have:
root:
provider "aws" {
region = "us-west-2"
}
provider "aws" {
alias = "foo"
region = "us-east-1"
}
module "foo" {
source = "foo-source"
providers = {
aws = aws
aws.foo = aws.foo
}
foo-source/foo.tf:
resource "aws_instance" "my_instance" {
provider = aws.foo
# ...
}
When I try to do this, I get:
Error: Provider configuration not present
To work with module.foo.aws_instance.my_instance its
original provider configuration at
module.foo.provider.aws.foo 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.foo.aws_instance.my_instance, after which you can
remove the provider configuration again.
Is there a way I can complete this refactor successfully without having to destroy or recreate the many resources created by the old provider? Or without having to terraform state rm
and terraform import
all of them?