Infrastructure as code for Serverless APIs

Simona Cotin
Microsoft Azure
Published in
3 min readJan 30, 2019

When building a serverless API using Azure, we typically need to provision several resources:

  1. A storage account to save our code files
  2. An App Service plan to configure the consumption plan
  3. An App Service to execute our code
  4. An Application Insights instance to monitor our app

Ideally, because all these resources belong to the same logical application, we want to manage, monitor and deploy them as a group. If the provisioning of our App Service Plan has failed, we wouldn’t want to continue with the deployment of the other resources. The same holds for all the other resources.

With Azure Resource Manager we can use JSON templates to configure the creation of all these resources and dependencies between them. Azure Resource Manager is a tool for managing and provisioning resources into the cloud.

TLDR — you can copy the template that will generate all these resources from here .

To navigate your way in any ARM template, you need to know that the typical structure is:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": []
}

The ready to deploy template file listed above is quite large so let’s break it down into the main sections:

  1. Parameters — this is where we can customize our deployment. In this template, we configure app name, storage account type and location to be dynamic. Every parameter has a name and a type, and optionally we can also configure defaultValue, allowedValues and metadata.

2. Variables — for reusable values throughout our template.

3. Resources — this section is where we declare all the resources that need to be created and their dependencies, aka this is where all the meat is!

Resources section in template

Note that this configuration will not only provision resources but also deploy the code from a given repository. To make sure you use your code as opposed to mine, you want to edit the parameters section:

"repoURL": {    "type": "string",    "defaultValue": "<your-repo-url-here>"},"branch": {    "type": "string",    "defaultValue": "<your-branch-here>"}

You could still use the template as is but that means it will deploy the master branch of my repository.

Deploy to Azure

Once we have a template defined that encapsulates our required resources we can add a “Deploy to Azure” button to our repository. In this example, we’ve created this template, and let’s add it to our GitHub repository:

In your repository’s Readme.md you need to add:

[![Deploy to Azure](https://azuredeploy.net/deploybutton.png)](https://portal.azure.com/#create/Microsoft.Template/uri/<your-encoded-azuredeploy.json-URL>)

Now anyone wanting to provision the same resources as you will only need to click a button:

Deploy to Azure

Now I call that awesome! 🎉

VS Code is a great tool to help you get started — make sure to install the Azure Resource Manager Tools extension and you will be able to generate new templates for all kinds of resources.

Checkout the quick start ARM templates, published on GitHub here. This is a collection of community contributed templates that you can use for other types of resources too, like databases, websites, etc.

Happy coding! 🐧

--

--