How to connect the Azure DevOps Release pipeline and deploy to Azure using a Service Connection and Azure App Registration
Tags: azure, devops, deployment, permissions
Published: 2020-05-08 14:49:05

Every once in a while I get the opportunity to kick-start a new and exciting project. :blush: This allows me to refresh my knowledge and gives me a real taste of the latest technology trends and techniques. This includes the setup of the automated delivery pipeline using one of my favorite development platforms: Azure DevOps. :heart: In this post I will cover some of the steps to automate the release from Azure DevOps to your Azure resources.

##Create a new release pipeline in Azure DevOps
The first thing that we need to is to create a new release pipeline. You can either start from scratch or select from a list of predefined deployment templates. I prefer to use the Azure App Service deployment template and will use it in the rest of this post. :point_down:

##Connect the Azure subscription
After selecting the deployment template you will be asked to select the targeted Azure subscription containing the Azure App Service resoures. Via the drop down you get a list with all the available Azure subscriptions that you can access. When you select the desired Azure subscription you get asked to authorize to configure an Azure service connection:

![Authorize to configure an Azure service connection](https://dev-to-uploads.s3.amazonaws.com/i/shph3dlb05g78yk3dyg9.PNG)

This is where the fun starts. :sweat_smile: Whenever I hit the Authorize button I get the following error:

![Service connection creation operation failed](https://dev-to-uploads.s3.amazonaws.com/i/bf97ds1sw7q5g1gw98o4.PNG)

The error message states that my account has insufficient privileges to create the Azure service principals needed to access and deploy to the Azure App Service. :man_shrugging:

Fortunately this can be fixed by manually creating the Azure service connection using an Azure App Registration. :heart_eyes:

##Create the Azure App Registration
In Azure portal within the Azure Active Directory goto the App registrations tab and create a new registration. Provide a user-facing display name (e.g. Azure DevOps Connection) and Register the app.

*Note: You might not have enough permissions to access Azure Active Directory. In that case ask your friendly neighborhood system administrator for help.* :innocent:

Create a new client secret a.k.a. application password that we can use later in the setup of the Azure service connection in Azure DevOps.
![Add a client secret](https://dev-to-uploads.s3.amazonaws.com/i/5rfo5d0l5xxwzycqou3k.PNG)

*Tip: Store your application secrets and access keys in Azure Key Vault.* :fire:

The next step is to assign the correct permissions so that the app registration can access the required resources.

##Provide access to the Azure App Registration
Navigate to the resource group containing the Azure App Services that will be used for the deployment. In the Access control (IAM) panel add a new role assignment:
![Add role assignment](https://dev-to-uploads.s3.amazonaws.com/i/mqdgzteh4wcm3dxwdw5i.PNG)

We are now ready to manually create the Azure service connection. :grin:

##Configure a new Azure service connection
Switch back to the newly created release pipeline in Azure DevOps and click on the **Manage** link next to the Azure subscription:

![Manage service connections](https://dev-to-uploads.s3.amazonaws.com/i/534e9w2xrjqlzx88v5sa.PNG)

Create a new service connection based on the Azure Resource Manager connection type and select the Service principal (manual) authentication method:
![Select the authentication method](https://dev-to-uploads.s3.amazonaws.com/i/qwf0ge8ti9jai1vzimgj.PNG)

*Note: the automatic and recommended authentication method will result in the same insufficient privileges error message as received earlier when hitting the Authorize button.* :eyes:

Next provide the following data from your Azure Cloud subscription:
- Subscription Id
- Subscription Name 
- Service Principal Id (App Registration Application ID)
- Service principal key (App Registration Client Secret created earlier on)
- Tenant ID

*Tip: Use the search function in Azure portal to quickly navigate to Subscriptions to grab the subscription ID and name or navigate to the Tenant properties for the tenant ID.* :100:

Finally verify and save the service connection and you are good to go! Happy deploying! :blush: