Terraform error: Provider configuration not present

2 min read

Due to the need to use the count keyword for making a resource optional I wanted to upgrade from terraform 0.12 to 0.13 but while doing so I found the following error:

Error: Provider configuration not present

To work with module.jenkins.aws_iam_policy.jenkins_policy
its original provider configuration at provider["registry.terraform.io/-/aws"]
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.jenkins.aws_iam_policy.jenkins_policy, after which
you can remove the provider configuration again.

Checking the documentation on upgrading to terraform 0.13 you can found the following statement:

When upgrading between major releases, we always recommend ensuring that you can run terraform plan and see no proposed changes on the previous version first, because otherwise pending changes can add additional unknowns into the upgrade process.

For this upgrade in particular, completing the upgrade will require running terraform apply with Terraform 0.13 after upgrading in order to apply some upgrades to the Terraform state, and we recommend doing that with no other changes pending.

Thus, the upgrade needs to be done on separate steps, first you need to upgrade the terraform state itself by performing an apply with the newer version (we can use tfenv to switch easily between terraform versions)

tfenf use 0.13.6
terraform plan
terraform apply

Once the terraform apply finishes, preferably without any changes on the infrastructure, we will have successfully upgraded the terraform state from the 0.12 to the 0.13 versions (this still applies for upgrading from 0.13 to 0.14):

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Releasing state lock. This may take a few moments...

We can now add the required changes (like the count usage on some resources)


Posted on 07/04/2021