Trying to connect Azure SQL Database from Azure Machine Learning Service using MSI Authentication (Without using username and password connect azure database) RRS feed

  • Question

  • I am trying to connect the **Azure SQL Database** from **Azure Machine Learning Service** with **MSI Authentication (Without a username and password).**

    I am trying to Machine learning model on azure Machine learning service that purpose I need data that' why I want to connect Azure SQL Database from Azure Machine Learning Service using MSI Authentication.

    But I got below error:-
     "error": {"message": "Activity Failed:\n{\n    \"error\": {\n        \"code\": \"UserError\",\n        \"message\": \"User program failed with KeyError: 'MSI_ENDPOINT'\",\n

    Please check the below code that I have used for the database connection.
    import logging
    import struct
    import pyodbc
    import os
    import requests

    class AzureDbConnect:
        def __init__(self):
            print("Inside msi database")
            msi_endpoint = os.environ["MSI_ENDPOINT"]
            msi_secret = os.environ["MSI_SECRET"]

            resource_uri = ''

            print("Inside token")

            token_auth_uri = f"{msi_endpoint}?resource={resource_uri}&api-version=2017-09-01"
            head_msi = {'Secret': msi_secret}
            resp = requests.get(token_auth_uri, headers=head_msi)
            access_token = resp.json()['access_token']
            print("Token is :- ")

            accesstoken = bytes(access_token, 'utf-8')
            exptoken = b""
            for i in accesstoken:
                exptoken += bytes({i})
                exptoken += bytes(1)
            tokenstruct = struct.pack("=i", len(exptoken)) + exptoken

            conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
                                  "Server=tcp:<Server Name>"
                                  "1433;Database=<Database Name>",
                                  attrs_before={1256: bytearray(tokenstruct)})


            self.sql_db = conn.cursor()

    class_obj = AzureDbConnect()


    **Is there any way to connect Azure, SQL Database from Azure Machine Learning Service With MSI Authentication?**

    • Edited by Akshay Godase Friday, November 1, 2019 9:34 AM Updated
    Friday, November 1, 2019 9:33 AM

All replies