BeGreen
BeGreen

Reputation: 951

Connect to Azure ML Workspace with Python SDK1 in Compute Instance

I have a script that allow me to connect to Azure ML Workspace. But it only works in local (interactive authentification) or on Cluster Instances while doing experiences.

But I can not manage to make it work in Compute Instances.

from azureml.core import Workspace
from azureml.core.run import Run, _OfflineRun

run = Run.get_context()

if isinstance(run, _OfflineRun):
    workspace = Workspace(
        "subscription_id",
        "resource_group",
        "workspace_name",
    )
else:
    workspace = run.experiment.workspace

I tried to use https://learn.microsoft.com/en-us/python/api/azureml-core/azureml.core.authentication.msiauthentication?view=azure-ml-py but it did not work.

from azureml.core.authentication import MsiAuthentication
from azureml.core import Workspace

msi_auth = MsiAuthentication()
workspace = Workspace(
            "subscription_id",
            "resource_group",
            "workspace_name",
            auth=msi_auth,
        )

File ~/localfiles/.venv/lib/python3.8/site-packages/azureml/_vendor/azure_cli_core/auth/adal_authentication.py:65, in MSIAuthenticationWrapper.set_token(self) 63 from azureml._vendor.azure_cli_core.azclierror import AzureConnectionError, AzureResponseError 64 try: ---> 65 super(MSIAuthenticationWrapper, self).set_token() 66 except requests.exceptions.ConnectionError as err: 67 logger.debug('throw requests.exceptions.ConnectionError when doing MSIAuthentication: \n%s', 68 traceback.format_exc()) File ~/localfiles/.venv/lib/python3.8/site-packages/msrestazure/azure_active_directory.py:596, in MSIAuthentication.set_token(self) 594 def set_token(self): 595 if _is_app_service(): --> 596 self.scheme, _, self.token = get_msi_token_webapp(self.resource, self.msi_conf) 597 elif "MSI_ENDPOINT" in os.environ: 598 self.scheme, _, self.token = get_msi_token(self.resource, self.port, self.msi_conf) File ~/localfiles/.venv/lib/python3.8/site-packages/msrestazure/azure_active_directory.py:548, in get_msi_token_webapp(resource, msi_conf) 546 raise RuntimeError(err_msg) 547 _LOGGER.debug('MSI: token retrieved') --> 548 token_entry = result.json() 549 return token_entry['token_type'], token_entry['access_token'], token_entry File ~/localfiles/.venv/lib/python3.8/site-packages/requests/models.py:975, in Response.json(self, **kwargs) 971 return complexjson.loads(self.text, **kwargs) 972 except JSONDecodeError as e: 973 # Catch JSON-related errors and raise as requests.JSONDecodeError 974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError --> 975 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I do not want to use SDK2, it will break all my existing code.

I don't understand why the identidy is not automatically managed when starting the Compute Instances like Cluster Instances.

Does any one has a solution for this?

Upvotes: 0

Views: 929

Answers (1)

Sairam Tadepalli
Sairam Tadepalli

Reputation: 1683

Use the below code blocks to connect to the existing workspace using the python SDK using the compute instances.

  • Create a compute instance

enter image description here

enter image description here

enter image description here

from azureml.core import Workspace
ws = Workspace.from_config()
ws.get_details()

Output:

enter image description here

Connecting to workspace using Config file:

import os
from azureml.core import Workspace

ws = Workspace.from_config()

enter image description here

Upvotes: 0

Related Questions