Managed Identity Authentication from Synapse notebook using pyodbc library to Azure SQL DB

bkotz 15 Reputation points
2023-10-06T20:36:10.6733333+00:00

I have an Azure Synapse workspace and Azure SQL DB set up. What I'm trying to do is use python's pyodbc library to connect from an Azure Synapse notebook to a table in my Azure SQL DB using the managed identity of the Synapse workspace to authenticate. I've followed many instructions and I've tried a few different changes here and there, but I keep getting this same error:

Error: ('FA004', "[FA004] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Failed to authenticate the user '' in Active Directory (Authentication option is 'ActiveDirectoryMSI').\nError code 0xA190; state 41360\n (0) (SQLDriverConnect)")

I'm unsure why it is trying to use the user field when authentication type is set to MSI.

  • I added the synapse workspace name associated to the synapse managed identity to my SQL DB as a user and I have added it roles.
  • I created a security group with my alias and synapse msi to administrator for SQL server under Settings > Microsoft Entra ID then click set admin
  • On SQL server I switched to system assigned managed identity under Security > Identity pane

I don't know if it is possible to use the synapse MSI directly, but assumed that since it is an MSI it would work. Below is some of the code I am using on the pyodbc side:

SERVER = 'tcp:<my_server_name>.database.windows.net'
DATABASE = '<my_db_name>'
DRIVER = '{ODBC Driver 18 for SQL Server}'

connectionString = f"Driver={DRIVER};Server={SERVER};Database={DATABASE};Authentication=ActiveDirectoryMsi;Connection Timeout=30;"

conn = pyodbc.connect(connectionString) #This is where it fails with above error.

How do I use the Synapse workspace managed identity to authenticate to Azure SQL DB? I haven't been able to find directions specifically for this scenario. I appreciate all your time and help in advance!

Azure SQL Database
Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
{count} votes

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.