How can I change the provider alias used for a data source?

I have a data source defined as:

data "snowflake_role" "this" {
  provider = snowflake.acc
  name     = "role_name"
}

I would like to change the provider alias. I changed it to:

data "snowflake_role" "this" {
  provider = snowflake.sec
  name     = "role_name"
}

but terraform validate failed with

│ Error: Provider configuration not present

│ To work with data.snowflake_role.this its original
│ provider configuration at
│ provider[“registry.terraform.io/snowflake-labs/snowflake”].sec 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
│ data.snowflake_role.this, after which you can remove
│ the provider configuration again.

This is kind of annoying. Is there a way to change the provider alias without destroying the resource?

How you declare your provider alias block and terraform block?
terraform block like this example bellow…

terraform {
  required_providers {
    mycloud = {
      source  = "mycorp/mycloud"
      version = "~> 1.0"
      configuration_aliases = [ mycloud.alternate ]
    }
  }
}

DOH! Your reply caused me to go and look at the provider declarations and I realised the sec alias hadn’t been created. I was making the stupid assumption that it had. :man_facepalming:

Once I created the alias everything worked fine. Thx @claytonsilva

1 Like

It’s ok @jamiekt, i share this checks because i remember of my mistakes :wink:

1 Like