Azure Bicep and Azure DevOps to deploy a function app

Cheranga Hatangala
Cheranga
Published in
9 min readJun 17, 2021

--

I love “Infrastructure as Code because it provides you the luxury of making continuous improvements to your infrastructure by treating it as code. In reality, as developers we try to design and code to the best of our ability or come and refactor later. With IaC now we can do that for our infrastructure as well.

There are many tools which you can use to do that. This article is based on Azure and the default choice so far has been ARM templates . There are some other third party providers in the game as well such as Terraform, pulumi,etc...

This article is based on Microsoft’s new kid on the block for IaC.

🔥🔥🔥 Azure Bicep!

Azure Bicep

Bicep is a domain specific language which declares your Azure resources to be deployed. It provides uncomplicated syntaxes and have been designed to reuse the code very easily through modules.

ARM templates Vs Bicep

Simply put an ARM template is a json structured file which declares the resources and configurations.

We all have worked with ARM templates and one of its main “challenges” are depending on the resource you deploy you will need to know exactly what to configure and, this can be quite frustrating. Also since its JSON an ARM template can quickly turn into a one large file. Well as a solution that you can “modularise” your ARM templates using linked templates but to do that you will need to do some additional grunt work such as storing it in a storage and accessing it securely.

With Bicep, the syntax is very concise and clear. My favourite feature of it, support for “Modules” are there to organise your templates in a much re usable manner.

See the below Bicep template which will declare a consumption based app service.

In contrast to the previous, see the equivalent ARM template below,

The Bicep template is very straight forward, where as the ARM template syntax you need to be very explicit about what you require.

Bicep playground

A good starting point will be the Bicep playground. In there you can experiment with Bicep. The most important feature I like in there is that the ability to transform your existing ARM templates into Bicep . You can simply click the “Decompile” button and point to the ARM template. But be mindful that all your ARM templates might not be easily converted into Bicep templates. You just need to fix the errors which it will show you so generously.

Required tools

  • VSCode
  • Bicep extension for VSCode

Absolutely love the Bicep extension! It has code snippets, syntax highlighting and, even intellisense!

You will find these features highly useful when building Bicep templates.

Create an Azure function app using Bicep templates

To create an Azure function app you will need the below resources.

  • Resource group
  • Storage account
  • App service plan
  • App insights (optional but highly recommended)
  • Key Vault (optional)
  • Function App and its settings

Now let’s create the above resources using Bicep!

Storage account

Let’s start with the storage account.

The storage account is so straight forward. You get the storage account kind and the storage account tier as parameters and use them to declare the storage account you require. Finally in the output section it outputs the connection string to the storage account.

[ I didn’t know a way to get this connection string in a “declarative” fashion. But if you are aware of this please let me know in the comments. ]

Application service plan

In here we’ll create a consumption based app service plan.

Application insights

This is required so that you can review how good (or bad) your application is performing in Azure. Although this component is optional it’s highly recommended.

Function app (without the settings)

First let’s create the skeleton function app without the settings. The key vault (next step) will need to know the function app’s principalid and the tenantid to provide access.

The function app which we’ll be creating will use deployment slots (azure function apps supports production and staging slots only).

So the below Bicep template creates function app with the two slots. Since the AKV will need the principalid and the tenantid the template will output them.

Key vault

Key vault is used to store sensitive data in a secure manner. In Azure, you can also specify who has access to the AKV. So in our case we would like to create an AKV but also we would like the function app to be able to access it securely.

We’ll be using MI (managed identity) to provide this link.

The below template is accepting the function app’s principalid and the tenantid to setup the access policies.

Finally it outputs the URI (the latest version) of the secret to be used.

Function app settings

Finally let’s set up the configurations required for the function app. Since we have two slots (Production and staging) we’ll need two configuration sections to be setup.

We have the components, lets use them

Now we have all the building blocks to create the function app. Bicep has this cool feature where you can create modules. Now let’s use Bicep modules to organise our resources to be deployed.

This is the storage account module

As you can see the syntax is very easy and straight forward. You define the module using the module keyword and the location for the Bicep template. Then you just simply pass the parameters required for the template. The Bicep extension of VSCode really helps you out here. As shown below the full intellisense is provided to you. It even give you a bunch of options including conditional access and my personal favourite required-properties .

But when deploying the Azure resources you will need to know the dependencies between them when deploying. In our function app these are the dependencies when the Azure resources need to be deployed.

  • Function app has dependencies on the storage account and the application service plan.
  • The key vault has a dependency on the function app because it needs to provide access to the function app.
  • The function app settings module has dependencies on function app, app insights and the key vault module.
  • The storage account, application service plan and the app insights modules does not have any dependencies.

The dependencies among the Bicep modules are specified as DependsOn. The function app module dependencies can be defined as shown below.

Azure DevOps

Let’s create a YAML based multi-stage pipeline to build and deploy our function app.

The CI (build) part

This is the CI (continuous integration) part where you build and test your software before creating any artifacts to be deployed.

The main purpose of a build pipeline is to check whether your code can be build successfully and to be able to create artifacts.

These are the steps associated with the build pipeline above,

  • Build and restore your .NET projects.
  • Run the tests.
  • Create the function app artifact and package it as a zip.
  • Create the Bicep templates as an artifact so that they can be referenced easily in the deployment pipeline.

The CD (deploy) part

This is the CD (continuous deployment) part where you deploy the artificats which were created as a result of the build.

The steps involved in the deployment pipeline are as follows,

  • Create the resource group.

We’ll use AZURE CLI to create the resource group.

Creating the resource group using Azure CLI

The resource group is created only if it doesn’t exist.

  • Provision the resources

We have the main Bicep template which orchestrates all the required resources to deployed in Azure. You can use the same Azure CLI command to deploy resources, az deployment group create here.

Passing parameters to the main bicep template to provision resources

Notice the --template-file argument. Since we created all the Bicep templates as an artifact with the name deploy (see the build pipeline above) we can easily use it to locate the Bicep template. Then all the parameters which are required by the main.bicep template is passed.

  • Deploy to the staging slot (stop + deploy latest code + start)

Since we are deploying an HTTP triggered function app with slots, we need to deploy our code to the staging slot first. Let’s stop it first, secondly we’ll deploy the latest code there and then start the staging slot.

  • Deploy to the production slot (swap with staging + stop staging slot)
swap staging slot with production

Since the staging slot is up and running, we can perform the swap operation with the production slot as specified above. Once it’s done no need to keep the staging slot alive. So as the final step we can stop the staging slot.

The CI/CD (build and deployment) pipeline

Now we have a build pipeline and a deployment pipeline. In reality the applications we develop will be deployed in multiple environments. So let’s create the final piece where we can build and deploy the function app into multiple environments.

  • Trigger points

We’ll trigger the pipeline when the code is checked in to the master branch or to any branch under feature . Also for PR requests made comparing the code to the master branch (you can make these conditional for each environment as well).

As you can see above there are multiple stages in the pipeline,

BUILD -> DEV -> SITare the stages in this pipeline, but you can create other environments as stages in the pipeline as you wish. Notice how the variable files are placed. There’s one common.yaml file to store all the parameters and each [env].yaml file to override the parameter values which will be specific to the environment.

Setting up the pipeline in Azure DevOps

Create a new pipeline in Azure DevOps, but since you already have the files required please select the file which has instructions to build and deploy your solution before you finish creating the pipeline in Azure DevOps.

Select the existing pipeline file in your solution.

After running the pipeline, you will be able to see the resources successfully deployed in Azure.

The Azure DevOps pipeline

The created resource groups in Azure

The created resource groups

The deployment histories in resource groups.

Finally, the resources deployed in the resource group.

The resources deployed in the resource group

Conclusion

Azure Bicep is awesome! The feature which I like most is the module support. VSCode and the Bicep extension is really helpful when you are building templates.

The code for this can be found in GitHub

Thanks!

References

--

--