Jump to content

Qtip: Connect Windows Azure VM to Azure SQL DB using Managed Identity

Featured Replies

Posted

In this guide I am going to show steps to connect Windows Azure VM to Azure SQL DB using Managed Identity covering create user in Azure SQL DB , connect using SSMS and connect using powershell

 

 

 

Requirements:

 

Windows 10 or 11 Azure Virtual Machine with system managed identity enabled and admin privileges to run powershell scripts

Azure SQL DB server with entra admin access and database for testing

SQL Server Management Studio (SSMS) latest version

 

 

 

Get required information from VM and managed identity:

 

mediumvv2px400.png.c69502c119c226c8642dd47c0ab3a9c1.png

 

Use Object (principal) ID to get Application ID

 

Go to Entra ID and search Object (principal) ID

 

mediumvv2px400.png.f52e2a5c4514f5ddeb086c38203650f9.png

 

Select result to get Application ID

 

mediumvv2px400.png.04b33e713b893b4eeae9c8f664c3dd9b.png

 

 

 

Provide access to Azure SQL DB:

 

Connect to server/database using Entra user with admin privileges and create user in this case is the name of the computer

 

mediumvv2px400.png.5614e6c0fd492197e45c6c3c1de63674.png

 

-- DROP USER [managediddemo] --remove user if exists

CREATE USER [managediddemo] FROM EXTERNAL PROVIDER;

ALTER ROLE db_datareader ADD MEMBER [managediddemo];

 

 

 

Connect from Azure VM:

 

Connect using SQL Server Management Studio SSMS ...

 

Open SSMS and provide server name , select authentication Microsoft Entra Managed Identity and user assigned Identity will be Application ID from VM

 

mediumvv2px400.png.b9ba7a00f58daaebae756f70b87e1c02.png

 

In connection properties provide database name otherwise you will receive an error if user is not administrator and finally connect

 

mediumvv2px400.png.5284e9d1efc5417d114e7e872a7b99c3.png

 

mediumvv2px400.png.1f9eaca79f3f43e9ec52a5bb17c392b0.png

 

Now is connected

 

 

 

Connect using powershell...

 

 

 

To be able to connect using powershell you need to Install modules required for Azure

 

Open powershell as administrator and run commands below

 

Set-ExecutionPolicy unrestricted

Install-Module -Name PowerShellGet -Force

Install-Module -Name Az -AllowClobber -Scope CurrentUser -force

Install-module SQLServer -force

 

mediumvv2px400.png.7a1a6378ae61890dd23ef40f27928482.png

 

Once modules are installed you can close powershell and open again as administrator

 

 

 

Get token

 

Connect-AzAccount -Identity

$access_token = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token

write-host $access_token

 

mediumvv2px400.png.5b4d7be634f12c9f1e4a706bc6e1b81e.png

 

*In some scenarios token string can be provided directly to avoid round trip each time

 

 

 

Test with invoke-sqlcmd

 

Connect-AzAccount -Identity

$access_token = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token

Invoke-Sqlcmd -ServerInstance <servername>.database.windows.net -Database <dbname> -AccessToken $access_token -query 'select top 10 name from sys.tables'

 

-query is the query to run in this case only gets a list of tables in database

 

mediumvv2px400.png.f1b35e2aa6302d3df094c4dba1a0d486.png

 

 

 

Test using Microsoft.Data.SQLClient

 

import-module Az.Accounts

import-module Microsoft.PowerShell.Security

import-module Microsoft.WSMan.Management

import-module SqlServer

$access_token = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token

$connectionstring="Server=tcp:<servername>.database.windows.net,1433; Database=<dbname>; Encrypt=True;"

$connection= New-Object Microsoft.Data.SqlClient.SqlConnection

# you can get connection string from azure portal in database overview

$connection.ConnectionString = $connectionstring

$connection.AccessToken=$access_token

$connection.Open()

$command= $connection.CreateCommand()

$command.CommandText = "select top 10 name from sys.tables"

$dataSet = New-Object system.Data.DataSet

$adapter = New-Object microsoft.Data.SqlClient.SqlDataAdapter $command

$adapter.Fill($dataSet) | Out-Null

$connectionid=$connection.clientconnectionid

write-output $connectionid

$dataSet.Tables

 

mediumvv2px400.png.eb2f9169147a005d084c704de6207a10.png

 

Now your Windows Azure VM is able to connect using different methods

 

 

 

More Information

 

Provision Azure AD admin (SQL Database)

Configure Microsoft Entra authentication - Azure SQL Database & SQL Managed Instance & Azure Synapse Analytics

 

 

 

What are managed identities for Azure resources?

 

Managed identities for Azure resources - Managed identities for Azure resources

 

 

 

Configure managed identities on Azure virtual machines (VMs)

 

Configure managed identities on Azure virtual machines (VMs) - Managed identities for Azure resources

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...