Infrastructure as code for Serverless APIs
- A storage account to save our code files
- An App Service plan to configure the consumption plan
- An App Service to execute our code
- 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:
The ready to deploy template file listed above is quite large so let’s break it down into the main sections:
- 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!
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:
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:
Now I call that awesome! 🎉
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! 🐧