Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscription could not be found in Azure Management API

I'm trying to create a Kubernetes cluster using Azure Management API.

  var credentials = SdkContext.AzureCredentialsFactory
    .FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));


  var azure = Azure
    .Configure()
    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
    .Authenticate(credentials)
    .WithDefaultSubscription();

var kubernetesCluster = azure.KubernetesClusters.Define("aks").WithRegion(Region.EuropeWest)
        .WithNewResourceGroup("aksResourceGroup").WithLatestVersion().WithRootUsername("aksUsername")
        .WithSshKey(sshPublicKey).WithServicePrincipalClientId("clientId")
        .WithServicePrincipalSecret("secret").DefineAgentPool("ap").WithVirtualMachineCount(1)
        .WithVirtualMachineSize(ContainerServiceVirtualMachineSizeTypes.StandardA0).Attach()
        .WithDnsPrefix("dns-aks").Create();

In the last line, a CloudException is thrown with the message: Subscription [] could not be found.

Even though an exception is thrown, the resource group is created but it is empty.

I have logged-in using Azure CLI with that service principal and I have run

az account list

with the following response:

[
  {
    "cloudName": "AzureCloud",
    "id": "SUBSCRIPTION ID FROM EXCEPTION ABOVE",
    "isDefault": true,
    "name": "Pay-As-You-Go",
    "state": "Enabled",
    "tenantId": "xxx",
    "user": {
      "name": "xxxx",
      "type": "servicePrincipal"
    }
  }
]

The App registration exists In Azure Active Directory > App registrations > All apps. I even gave permissions to all possible APIs.

Is there anything I did wrong in order to receive that exception message?

like image 657
Florin-Constantin Ciubotariu Avatar asked Apr 02 '18 16:04

Florin-Constantin Ciubotariu


People also ask

How do I add a subscription key to API?

Select the Products menu/link from Azure portal. Select the product from list. Select the APIs from selected product options. Click on Add button and select your API from list and click on Select.

What is subscription key in API?

To access APIs, you'll need a subscription and a subscription key. A subscription is a named container for a pair of subscription keys. Regularly regenerating keys is a common security precaution.

Where do I find my Azure subscription key?

You can obtain access keys in the portal or through PowerShell, Azure CLI, or REST API. Sign in to the Azure portal. List the search services for your subscription. Select the service and on the Overview page, click Settings >Keys to view admin and query keys.


1 Answers

According to the error log, it seems you don't set default subscription for your service principal. You could use az account set --subscription <name or id> to set it.

If it still does not work, I suggest you could use the following code.

  var azure = Azure
    .Configure()
    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
    .Authenticate(credentials)
    .withSubscription("subscription id")

Note: You should give your service principal Owner role on your subscription level. See this link. But it seems you had done it, but I suggest you could check again.

like image 60
Shui shengbao Avatar answered Oct 27 '22 22:10

Shui shengbao