aks generation script fails with Bad Request when ran as a script, but not when ran interactively.
az feedbackauto-generates most of the information requested below, as of CLI version 2.0.62
Describe the bug I have a script to build a Kubernetes environment. This script runs fine if I execute each individual command. However, if I run it as a PowerShell script the last command throws the following error:
az : ERROR: Operation failed with status: 'Bad Request'. Details: Internal server error
At C:\Users\Dustin\Downloads\generate-azure-environment.ps1:31 char:1
+ az aks create --resource-group "$kubernetesResourceGroup" `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: Operatio...al server error:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
I'm not sure why I running these commands as a script fails vs. running them individually. I've played around with adding Start-Sleep commands, but that seems like a sketchy hack.
To Reproduce Run this script:
$kubernetesResourceGroup="kube-test-1" # needs to be unique to your subscription
$acrName='qawsedtestacr' # must conform to the following pattern: '^[a-zA-Z0-9]*$
$aksClusterName='test-cluster-dje'
$location = 'centralus'
$numberOfNodes = 1
Write-Host "Creating resource group $resourceGroupName"
az group create -l $location -n $kubernetesResourceGroup
Write-Host "Creating the Azure Container Registry"
az acr create --resource-group $kubernetesResourceGroup --name $acrName --sku Standard --location $location
Write-Host "Creating a Service Principal"
$sp=az ad sp create-for-rbac --skip-assignment | ConvertFrom-Json
$appId=$sp.appId
$appPassword=$sp.password
$acrID=az acr show --resource-group $kubernetesResourceGroup --name $acrName --query "id" --output tsv
Write-Host "Assigning the service principal to ACR pull"
az role assignment create --assignee $appId --scope $acrID --role acrpull
Write-Host "Building a Kubernetes Cluster"
az aks create --resource-group "$kubernetesResourceGroup" `
--name "$aksClusterName" `
--node-count "$numberOfNodes" `
--service-principal "$appId" `
--client-secret "$appPassword" `
--generate-ssh-keys `
--location "$location"
Expected behavior I want my script to behave the same way regardless of whether I run it as a script or run the commands individually.
Environment summary Windows 10, Azure CLI 2.0.70
Additional context Add any other context about the problem here.
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @mjconnection
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @mjconnection
We have the same error.
When running in a script, it shows
Finished service principal creation[##################################] 100.0000%Operation failed with status: 'Bad Request'. Details: Internal server error
Seeing the same error here with the same script, except presence or absence of Start-Sleep pauses has no effect; the Bad Request error occurs either way.
@Azure/aks-pm I am routing this to the AKS PM team for follow-up
Would you be able to try with the latest CLI version and confirm if it still happens ?
From what I could find your errors are due to errors on the AAD Service Principal creation and propagation. Which is why for some of you running this manually one by one it works.
Eg running @DustinEwers script as-is

Running the script with a Start-Sleep -s 240 after the SP is created makes it work.

Will make sure we capture it and send a better error message saying the SP is not found/not propagated yet.
Gotcha, so what's happening is that the SP isn't being propagated for several seconds. The command comes back, but we're not actually ready to use that SP yet. That explains why the sleeps helped (though not consistently because I was using a 3 second sleep) in the first place.
Correct, sometimes it can take some time and we have seen a bit of an average delay increase recently that might explain it
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Azure/aks-pm
I am closing this issue as it's a known problem with AAD replication as mentioned in this issue. https://github.com/Azure/AKS/issues/1206