Description
When using multiple provider aliases with the same resource type, there appears to be a concurrency issue where the wrong provider configuration is being applied to resources. This results in resources being created in the wrong region or with incorrect settings.
Steps to Reproduce
- Define multiple providers of the same type with different aliases in the Terraform configuration:
provider “mgc” {
alias = “northeast”
region = “br-ne1”
api_key = “api-key-1”
}
provider “mgc” {
alias = “southeast”
region = “br-se1”
api_key = “api-key-2”
}
- Create multiple resources of the same type, each using a different provider alias:
resource “mgc_kubernetes_cluster” “cluster_northeast” {
provider = mgc.northeast
name = “cluster-northeast”
version = “1.21”
}
resource “mgc_kubernetes_cluster” “cluster_southeast” {
provider = mgc.southeast
name = “cluster-southeast”
version = “1.21”
}
3. Run terraform plan
Expected Behavior
Each resource should be created using the configuration of its specified provider alias. The cluster_northeast
should be created in the br-ne1
region, and the cluster_southeast
should be created in the br-se1
region.
Actual Behavior
All resources are being created with the configuration of a single provider, regardless of the specified alias. This results in clusters being created in the wrong region or with incorrect settings.
Additional Context
We’ve attempted to resolve this by implementing ResourceWithConfigure
and using a thread-safe map to store provider data. However, the issue persists. It appears that the Configure
method of the resource is always receiving the same provider data, regardless of the alias specified in the Terraform configuration.
Possible Related Code
Here’s a simplified version of our resource implementation:
type KubernetesClusterResource struct {
providerData *providerData
}
func (r *KubernetesClusterResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
providerKey, ok := req.ProviderData.(string)
if !ok {
resp.Diagnostics.AddError(“Unexpected Provider Configure Type”, “Expected string”)
return
}
data, exists := providerDataStore.data[providerKey] <<<< Here it is always the same value, like a type of draw taking place between the providers.
if !exists {
resp.Diagnostics.AddError("Provider Data Not Found", fmt.Sprintf("Unable to find provider data for key: %s", providerKey))
return
}
r.providerData = data
}
Environment
- Terraform version: Terraform v1.9.4
- terraform-plugin-framework version: 1.11.0
- Go version: >= 1.22.6
- Operating System: Linux