Azure App Services make it quite easy for you to add one or more authentication providers to your application. But how do you add Azure AD as a provider using Infrastructure as Code?
In this article I will show you the steps of deploying and securing an Azure App Service with AAD authentication using an Azure pipeline.
Note
I will be referencing some parts of my previous article Manage Azure AD applications from an Azure DevOps Pipeline.
Setting up the web app deployment
Before we can add authentication to a web application, first the infrastructure must be deployed.
For the deployment I will use a simple ARM template that consists of a Linux app service,
a site and a couple of configuration settings.
In this case I will set it up for use with the latest PHP stack.
The ARM template has a single parameter called siteName
to use for the name of the site and app service.
As this article is not about deploying a web app using an ARM template, I will not embed the contents here, but you can find the whole deployment file in my GitHub repo.
Web content
I have kept the contents of this web page purposely simple.
The index.php
file contains some simple scripting to show the web app is working correctly.
Configuring the pipeline
I will skip over the steps of connecting an Azure subscription and creating an empty pipeline. These steps are already described in the chapter “Configuring an Azure pipeline” in my previous article.
With the empty pipeline as a base, we need three tasks to deploy this web application.
I will add the siteName
as a variable to the pipeline, so it can be reused in the tasks that will be added.
The first step is the Azure Resource Group Deployment task to deploy the ARM template into a resource group.
As it is not possible to deploy a single php file to a web app, zip it with the Archive Files task, then use the Azure App Service Deploy task to deploy the archive to the created web site.
After executing the pipeline, there is now an anonymous web site running in Azure.
Adding Azure AD authentication
To add Azure AD as an authentication provider, an Azure AD app needs to be configured. For all details, I am pointing to my previous article again.
In summary, an Azure CLI task is added. In this pipeline as the second step, after the ARM template is deployed.
It contains the following contents as inline script:
Note
newlines between parameters are only added for readability
Now, to add authentication, only the az webapp auth update
command has to be added.
This makes the app service use the created Azure AD app as a means to authenticate visitors to the web site.
After executing the pipeline with the updated configuration, nobody is able to view the page anonymously anymore and visitors need to supply a valid Azure AD account to see the page again.
Looking in the Azure Portal, it is possible to confirm that the pipeline has configured all settings as requested.
Source code
I have shared all the related source code in the AzDO.Pipelines.AppService.AADAuth GitHub repository.