Search code examples
pythonazureazure-functionsazure-virtual-machineazure-sdk-python

Receiving the error Code: SubscriptionNotFound Message: The subscription not found when trying to get data about Azure Virtual Machine?


I am working on a script that accesses details about an Azure Virtual Machine currently. This is the code that I have so far:

"""
    Instantiate the ComputeManagementClient with the appropriate credentials.

    @return ComputeManagementClient object
"""
def get_access_to_virtual_machine():
    subscription_id = key.SUBSCRIPTION_ID
    credentials = DefaultAzureCredential(authority = AzureAuthorityHosts.AZURE_GOVERNMENT, 
                                        exclude_environment_credential = True,
                                        exclude_managed_identity_credential = True,
                                        exclude_shared_token_cache_credential = True)
    client = KeyClient(vault_url = key.VAULT_URL, credential = credentials)                     
    compute_client = ComputeManagementClient(credentials, subscription_id)
    return compute_client

"""
    Check to see if Azure Virtual Machine exists and the state of the virtual machine.
"""
def get_azure_vm(resource_group_name, virtual_machine_name):
    compute_client = get_access_to_virtual_machine()
    vm_data = compute_client.virtual_machines.get(resource_group_name, 
                                                virtual_machine_name, 
                                                expand = 'instanceView')
    return vm_data

When trying to run get_azure_vm(key.RESOURCE_GROUP, key.VIRTUAL_MACHINE_NAME) which I am certain does have the correct credentials, I get the following error output (note that I replaced the actual subscription ID with 'xxxx' for now):

Traceback (most recent call last):
  File "/Users/shilpakancharla/Documents/function_app/WeedsMediaUploadTrigger/event_process.py", line 62, in <module>
    vm_data = get_azure_vm(key.RESOURCE_GROUP, key.VIRTUAL_MACHINE_NAME)
  File "<decorator-gen-2>", line 2, in get_azure_vm
  File "/usr/local/lib/python3.9/site-packages/retry/api.py", line 73, in retry_decorator
    return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
  File "/usr/local/lib/python3.9/site-packages/retry/api.py", line 33, in __retry_internal
    return f()
  File "/Users/shilpakancharla/Documents/function_app/WeedsMediaUploadTrigger/event_process.py", line 55, in get_azure_vm
    vm_data = compute_client.virtual_machines.get(resource_group_name, 
  File "/usr/local/lib/python3.9/site-packages/azure/mgmt/compute/v2019_12_01/operations/_virtual_machines_operations.py", line 641, in get
    map_error(status_code=response.status_code, response=response, error_map=error_map)
  File "/usr/local/lib/python3.9/site-packages/azure/core/exceptions.py", line 102, in map_error
    raise error
azure.core.exceptions.ResourceNotFoundError: (SubscriptionNotFound) The subscription 'xxxx' could not be found.
Code: SubscriptionNotFound
Message: The subscription 'xxxx' could not be found.

I am using the beta preview version of azure.mgmt.compute which was installed with pip install azure-mgmt-compute=17.0.0b1. Note that I am also using an Azure Government account. Is there a way to solve this error? I have also tried using ServicePrincipalCredentials and get_azure_credentials() but ran into different errors - I was recommended to use DefaultAzureCredentials and the key vault by a coworker.


Solution

  • There is no problem with the code and it works fine on my side. And the error message shows the reason:

    azure.core.exceptions.ResourceNotFoundError: (SubscriptionNotFound) The subscription 'xxxx' could not be found. Code: SubscriptionNotFound Message: The subscription 'xxxx' could not be found.

    It seems you run the python code in your local machine. I recommend you log in with the Azure CLI first and then check if the subscription id that you used in your python code is right.