{"record":{"id":"a2b4681b4dd25841","repo":"microsoft/aspire","slug":"found-resourcegroups-length-aks-clusters-named-clustername","errorCode":null,"errorMessage":"Found {resourceGroups.Length} AKS clusters named '{clusterName}' in subscription '{subscriptionId}' (resource groups: {string.Join(\", \", resourceGroups)}). Specify which one to use by calling AsExistingInResourceGroup on the resource.","messagePattern":"Found (.+?) AKS clusters named '(.+?)' in subscription '(.+?)' \\(resource groups: (.+?)\\)\\. Specify which one to use by calling AsExistingInResourceGroup on the resource\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentResource.AksPipeline.cs","lineNumber":931,"sourceCode":"        // With '-o tsv' the query emits one resource group per matching cluster, newline separated:\n        //   my-rg\n        //   other-rg\n        // A cluster name is only unique within a resource group, not within a subscription, so the\n        // query can legitimately return several rows. Picking one would silently deploy into, and\n        // hand back credentials for, an unrelated cluster.\n        var resourceGroups = result.StandardOutput\n            .Split('\\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);\n\n        if (resourceGroups.Length == 0)\n        {\n            throw new InvalidOperationException(\n                $\"Could not resolve resource group for AKS cluster '{clusterName}'. \" +\n                \"Ensure Azure provisioning has completed.\");\n        }\n\n        if (resourceGroups.Length > 1)\n        {\n            throw new InvalidOperationException(\n                $\"Found {resourceGroups.Length} AKS clusters named '{clusterName}' in subscription \" +\n                $\"'{subscriptionId}' (resource groups: {string.Join(\", \", resourceGroups)}). \" +\n                \"Specify which one to use by calling AsExistingInResourceGroup on the resource.\");\n        }\n\n        return resourceGroups[0];\n    }\n\n    /// <summary>\n    /// Fetches the kubeconfig content for the cluster from the Azure CLI.\n    /// </summary>\n    /// <remarks>\n    /// <paramref name=\"runAzCommandAsync\"/> is injected so tests can verify that the credential\n    /// fetch is scoped to the deployment subscription without invoking the real az CLI.\n    /// </remarks>\n    internal static async Task<string> FetchKubeConfigAsync(\n        string azPath,\n        string subscriptionId,","sourceCodeStart":913,"sourceCodeEnd":949,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentResource.AksPipeline.cs#L913-L949","documentation":"The resource-group lookup query matched more than one AKS cluster with the same name in the subscription (clusters can share names across resource groups). Aspire refuses to pick one arbitrarily - kubeconfig credentials would silently target an unrelated cluster - so it throws and asks the developer to disambiguate via AsExistingInResourceGroup.","triggerScenarios":"resourceGroup property queried when `az resource list` returns 2+ resource groups for the same AKS cluster name in the resolved subscription, and the AzureKubernetesEnvironmentResource was not explicitly bound to a resource group with AsExistingInResourceGroup.","commonSituations":"Dev/test environments reusing the same cluster name in multiple resource groups (e.g. per-branch RGs); re-created clusters where the old RG still exists; shared team subscription with duplicate names from different people.","solutions":["Call AsExistingInResourceGroup(\"<resource-group>\") on the AzureKubernetesEnvironmentResource to pin the exact resource group.","Rename one of the duplicate clusters (or delete the stale one) so the name is unique in the subscription.","Verify which duplicates are legitimate with `az aks list --query \"[?name=='<clusterName>'].{name:name,rg:resourceGroup}\" -o table`.","Use distinct cluster names per environment/branch to prevent recurrence."],"exampleFix":"// before\nvar aks = builder.AddAzureKubernetesEnvironment(\"aks\");\n\n// after\nvar aks = builder.AddAzureKubernetesEnvironment(\"aks\")\n    .AsExistingInResourceGroup(\"my-cluster-rg\");","handlingStrategy":"validation","validationCode":"// disambiguate before running the pipeline\nvar aks = builder.AddAzureKubernetesEnvironment(\"aks\")\n    .AsExistingInResourceGroup(\"my-cluster-rg\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use unique AKS cluster names per environment/branch","Pin clusters to a resource group with AsExistingInResourceGroup","Periodically delete stale duplicate clusters in shared subscriptions"],"tags":["azure","aks","ambiguity","resource-group"],"backgroundTag":"ambiguous-resource-match","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-22T16:17:23.217Z"}