Using an ARM template to deploy your SSL certificate stored in KeyVault on an Web App

Screenshot 2019-08-17 at 22.56.08Everyone knows it’s completely normal nowadays to have your website loaded over https. Troy Hunt explains why. There are quite some examples out there on how to use Let’s Encrypt certificates on your Azure web app, see this one by Henry Been for example. For most of us that’s a perfect and free solutions. It doesn’t work for all of us however. Sometimes you require additional verification while requesting a new certificate or you simple do not own the domain and therefor cannot or are not allowed to use Let’s Encrypt. Here’s how to add a custom SSL certificate to an Azure Web App stored in KeyVault.

Create the KeyVault with the required settings

Here’s the ARM template to create a KeyVault with the required settings to be able to grab a certificate from it during a Web App deployment. There are two things we care about in this context. First, you need to enable ‘enabledForTemplateDeployment’. Second, we need to grand the Microsoft.Web resource provider access to the KeyVault to get the certificate. That’s the second resource in this file. That objectId is always the same for everyone.

 

Store the certificate in KeyVault

I use an Azure DevOps pipeline to run all the steps described in the remainder of this blog. The first three steps are about uploading the certificate into KeyVault. First, go to the secure files under Pipelines, Library and upload your certificate. Then, add a download secure file task to your deployment pipeline and select your certificate. We now need to extract a base64 string from the file and upload that to KeyVault as a secret. Add an PowerShell task and execute the following script.

This task saves the base64 value in an Azure DevOps variable that we will use in the next step. To upload the certificate to KeyVault I use an ARM Template as shown here. I use the Azure resource group deployment task to deploy the template with the following parameter override:

Deploy the certificate to your Web App

The last step we need take is to deploy the Web App with it’s hostname binding and certificate. I use the following ARM template to do that with the following parameter overrides in the Azure resource group deployment task:

That’s it! If you now navigate to your app’s domain there will be a nice, green lock.

You may also like...