Share via

Use user-managed identity in Python script on local machine to access Azure SQL Server (serverless) with pyodbc

cyber_thunk 1 Reputation point
2021-12-28T21:33:19.487+00:00

I've created a Python script on my local machine & I'm attempting to authenticate into an Azure SQL Server (serverless (i.e., not managed instance)). Rather than storing creds in code, I want to utilize user-managed identity (UMI) to authenticate into my SQL Server.

I created the UMI in Azure portal, assigned it to my SQL Server & gave it read, write & admin authorization in SQL server.

Local machine:
Windows 11 pro
ODBC Driver 17.8 64x
Python 3.9

Using the following import statements:

from azure.identity import ManagedIdentityCredential
import pyodbc

Here is some of my code:
umi_client_id = 'my user managed identity client id'
db_umi_cred = ManagedIdentityCredential(client_id=umi_client_id)
db_connect = pyodbc.connect(f"DRIVER={odbc_driver}; SERVER={sql_server}; DATABASE={sql_db}; UID={db_umi_cred}; Authentication={pyodbc_auth}")

I get an error: ...TCP Provider: Timeout error [258]...

I've been trying this for a few days with no success. I don't want to use ODBC Data Source Administrator to store anything. I also shouldn't have to use key vault for this (right?). I won't be publishing this python script to cloud; just trying to test this locally. Conceptually, this seems pretty straightforward.

Any help would be greatly appreciated.

Azure SQL Database
Microsoft Security | Microsoft Entra | Microsoft Entra ID

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.