How to rename a provider alias in terraform

Hi All,

We have created some AWS infra with terraform. Originally, it was planned to use a single region but now there are some resources that require to be in multiple regions.

So, we would like to rename the provider alias to contain the region name (please, let’s not go into the details about why not use a separate directory for the new region… Let’s focus on the problem):
from infra-prod to infra-prod-eu-central-1

If we just rename the alias and then update the providers meta-argument of our modules, terraform apply runs and says nothing changed (which seems to be a bug knowing that the old provider alias is not available anymore in the root module and it is stored together with the resources in the state file… see below).

At the same time, when we want to do any change on the resource and run terraform again, it says Error: Provider configuration not present

Seems terraform stores the provider (including the alias as well) in the state file for each resource:

# terraform state pull|grep provider|head -n3
      "provider": "provider[\"registry.terraform.io/hashicorp/aws\"].infra-prod",
      "provider": "provider[\"registry.terraform.io/hashicorp/aws\"].infra-test",
      "provider": "provider[\"registry.terraform.io/hashicorp/aws\"].infra-prod",

So we tried to use terraform state replace-provider but seems this can only change the provider path (registry.terraform.io/hashicorp/aws) itself but not the alias.

The only solution that we could find is to pull the state, replace the provider names manually and push it back. This is not something that we would like to do, as part of the infra is in production.

Anyone has any other solution/idea?

Thanks ahead

So, after further debug, this only happens when modules given a providers meta argument. In such cases terraform stores the name of the alias together with the resource in the state.
In order to overcome this, after one renames the alias of the provider, one needs to run a terraform apply -refresh-only to update the alias in the state file.