Automated Deployment of Azure SQL Database/ Azure SQL Data warehouse through Azure Devops via Service Principal – Part 1

Related: 1|2

Problem Statement :

Is it possible to publish DACPAC on Azure SQL Database or Azure SQL Data Warehouse via Service Principal rather than SQL Account for better Security Standards.

Prerequisites :

  1. Service Principal

How to Create a Service Principal

2. Azure SQL Server with Active Directory enabled

Solution :

  1. Provide the Service Principal “Reader” role assignment via Access Control (IAM) on the Azure SQL server.

2. Once the Reader role is assigned , Create a Azure Service Connection of type “Azure Resource Manager using Service Principal” in the Azure Devops Project Settings.

How to Set Up a Service Connection

In case if the Service Principal is initially not given Reader role access on SQL Server, one would get the below error while creation of the Service Connection :

Failed to query service connection API: ‘https://management.azure.com/subscriptions/<<SubscriptionId>&gt;?api-version=2016-06-01’. Status Code: ‘Forbidden’, Response from server: ‘{“error”:{“code”:”AuthorizationFailed”,”message”:”The client ‘<>’ with object id ‘<>’ does not have authorization to perform action ‘Microsoft.Resources/subscriptions/read’ over scope ‘/subscriptions/<>’ or the scope is invalid. If access was recently granted, please refresh your credentials.”}}’

3. Login into the Azure SQL Server via an AD admin account and execute the below set of queries in the Master database.

CREATE USER [<>] FROM EXTERNAL PROVIDER ;

exec sp_addrolemember ‘dbmanager’, ‘<>’ ;

exec sp_addrolemember ‘loginmanager’, ‘<>’ ;

Note : The above queries can be executed only when logged in via an AD admin account and not via any SQL account logged in session.

4. Create a Release Pipeline in Azure Pipelines and add the Azure SQL Database deployment task with the below configurations:

YAML :

steps:

  • task: SqlAzureDacpacDeployment@1
    displayName: ‘Deploy database’
    inputs:
    azureSubscription: ‘$(ServiceConnection)’
    AuthenticationType: servicePrincipal
    ServerName: ‘$(Server)’
    DatabaseName: ‘$(DatabaseName)’
    DacpacFile: ‘$(DacpacPath)’

where $(ServiceConnection) is the variable that represents the name of the Service Connection created as a part of step 2.

There are multiple Authentication types supported for Azure SQL Database deployment but Service Principal is a much secure way as compared to some other authentication types.

Published by Nandan Hegde

Microsoft Data MVP |Microsoft Data platform Architect | Blogger | MSFT Community Champion I am a MSFT Data Platform MVP and Business Intelligence and Data Warehouse professional working within the Microsoft data platform eco-system which includes Azure Synapse Analytics ,Azure Data Factory ,Azure SQL Database and Power BI. To help people keep up with this ever-changing landscape, I frequently posts on LinkedIn, Twitter and to his blog on https://datasharkx.wordpress.com. LinkedIn Profile : www.linkedin.com/in/nandan-hegde-4a195a66 GitHUB Profile : https://github.com/NandanHegde15 Twitter Profile : @nandan_hegde15 MSFT MVP Profile : https://mvp.microsoft.com/en-US/MVP/profile/8977819f-95fb-ed11-8f6d-000d3a560942

8 thoughts on “Automated Deployment of Azure SQL Database/ Azure SQL Data warehouse through Azure Devops via Service Principal – Part 1

  1. @Nandan, I tried implementing this solution.Followed the steps except the YAML part. Where I have to add the yaml code ?

    Like

  2. Could you provide an explanation of what the 2 queries in step 3 does?
    CREATE USER [] FROM EXTERNAL PROVIDER ;
    exec sp_addrolemember ‘dbmanager’, ‘’ ;
    exec sp_addrolemember ‘loginmanager’, ‘’ ;

    ———————–
    Also what is the difference between this and the solution in part 2?
    Can we run the queries in part 2 instead when using an ARM Service Connection?
    CREATE USER [] FROM EXTERNAL PROVIDER ;
    exec sp_addrolemember ‘dbmanager’, ‘’ ;
    exec sp_addrolemember ‘loginmanager’, ‘’ ;

    Like

Leave a comment

Design a site like this with WordPress.com
Get started