Is that possible to use TWO or more providers in the same tf file?

:sun_with_face:
Hi all,
I am trying to use two (or more) providers in the same terraform tf file. I want to apply on both (or more) my changes. Is that possible to do it?

These are the providers :

provider "rancher2"{  //the first one
 api_url = "https://first.rancher.cloud/v3" //api-endpoint
 access_key = "tokxen-xxxxxx" 
 secret_key = "xxxqrzertwlm8f9pqsddffg8c84npl9kbgh9xbvwfh7klc7d5m9ktsk"
 alias = "a"
}


provider "rancher2"{  //the second
 api_url = "https://second.rancher.cloud/v3" //api-endpoint
 access_key = "tokxen-xxxxx" 
 secret_key = "vs5qliuhgygydhthrthrjkjkaz53s5bssxj8fthfphc82vwnd4jwrn9"
 alias = "b"
}

And here is my resource :

resource "rancher2_role_template" "bt" {
  
  name = "bt"
  context = "cluster"
  default_role = true
  description = "tf templating for CRD acceptance by API gps"
  rules {
    api_groups = ["common.k8s.elastic.co"]
    resources = ["*"]
    verbs = ["create","delete","get","list","patch","update","watch"]
  }

The difficulty is to attack these two providers to refresh my resource at the same time.

It sounds like you want a single resource to be managed by two providers. That’s not possible. Each resource belongs to a single provider. If you want the same resource to be duplicated by two providers, you’ll have to define it twice in your configuration, and you can use a for_each loop to apply the identical configuration to both providers.

we tried using the for_each loop to instantiate the resource refereing to different provider this way :
indent preformatted text by 4 spaces

provider "rancher2"{  //a
    api_url = "https://aaaaaaaaaa/v3" //api-endpoint
    access_key = "aaaaaaa" 
    secret_key = "aaaaaaaaaaaaaaaaaaa"
    alias = "a"
    }

provider "rancher2"{  //b
    api_url = "https://bbbbb/v3" //api-endpoint
    access_key = "bbbbbbbbbb" 
    secret_key = "bbbbbbbbbbb"
    alias = "b"
    }
resource "rancher2_role_template" "bu-crd-right" {

for_each = {
    provider_a = "a"
    provider_b = "b"
}
  name = "bu-crd-right ${each.key}"
  provider = rancher2.${each.value}
  ...
  rules {
    xxxx
  } 
}

my guess is that it’s linked to this issue : https://github.com/hashicorp/terraform/issues/3656
this seems not possible yet. Can you confirm ?

It certainly looks like the same issue.