azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

aks generation script fails with Bad Request when ran as a script, but not when ran interactively.

Open DustinEwers opened this issue 7 years ago • 11 comments

az feedback auto-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.

DustinEwers avatar Aug 10 '19 03:08 DustinEwers

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @mjconnection

ghost avatar Aug 12 '19 01:08 ghost

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @mjconnection

ghost avatar Aug 12 '19 01:08 ghost

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

HaokePeng avatar Aug 12 '19 19:08 HaokePeng

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.

ajsaulsberry avatar Aug 13 '19 19:08 ajsaulsberry

@Azure/aks-pm I am routing this to the AKS PM team for follow-up

michaeljohnson1172 avatar Aug 14 '19 23:08 michaeljohnson1172

Would you be able to try with the latest CLI version and confirm if it still happens ?

palma21 avatar Aug 15 '19 00:08 palma21

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 image

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

image

Will make sure we capture it and send a better error message saying the SP is not found/not propagated yet.

palma21 avatar Aug 15 '19 01:08 palma21

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.

DustinEwers avatar Aug 15 '19 03:08 DustinEwers

Correct, sometimes it can take some time and we have seen a bit of an average delay increase recently that might explain it

palma21 avatar Aug 15 '19 05:08 palma21

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Azure/aks-pm

ghost avatar Dec 04 '19 07:12 ghost

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

jluk avatar Dec 04 '19 23:12 jluk