Just to keep it simple, we will use the ARM templates to deploy two resources in Azure:
A storage account with the name mystorageaccount0089 and a Virtual Network with the name Myvnet.
One method to do that is to use the Linked templates method, which allows us to have one main ARM template (the Master.json template) to refer to separate template files via a link.
It is important to note that Linked templates cannot be stored as a local file or a file that is only available on our local network. They must be uploaded first in an endpoint, for example Github or a storage account in Azure.
Here, we will try the approach to create a temporary storage account, copy the arm templates there, use this storage to deploy the resources and when the deployment is done, the storage account will be automatically deleted. This is an easy, fast and very straight-forward solution.
Additionally, after creating the ARM templates, we will create a YAML pipeline in the Azure DevOps environment to automate the deployment process.
Now let’s visualize the solution:
To see how we can link templates let’s have a look at the Master.json file which links the storage account and Vnet templates:
A few things to focus here:
- The highlighted Microsoft.Resources/deployments object shows how to define a linked template.
- Next, we give a name for this template so we can make a reference inside the template: we give the linkedTemplate1 name for the storage account and linkedTemplate2 for the Vnet.
- Next, we define the URI path for the ARM templates. In other words, we need to define in the Master template where these templates are stored.
In other words we say: go to endpoint999 temporary storage account on Azure, find a container with the name: quickstartblobs and there you can find the templates files you need.
Next, we want to create the temporary storage account to store the ARM templates there:
To do this we will create two Powershell scripts:
- One for creating the temporary storage account and a container
2. And one for deleting the temporary storage account:
Let’s get the technicalities out of the way assuming that we already have created an organization, a project , an Azure repo and a Service connection in the Azure DevOps environment (dev.azure.com) having this simple structure:
Next step is to create a YAML Build pipeline.
A pipeline always starts from a trigger and normally is composed of one or more stages and each stage contains one or more jobs. When creating a pipeline, we always need to define a set of jobs or tasks for automating our builds, and this is what we do here.
We have four different tasks, just like the previous image:
Azure DevOps let you create secret variables.
You need to go to the Pipeline and select your pipeline and enter the edit mode. You will have a button named “Variables” on the upper right side of the page. So here we have defined two variables. One for Azuresubscription and one for SubscriptionId. For security reasons, we do not want these values to be shown in the YAML code.
When we run the pipeline the first resource we get is the temporary storage account, which has been created from the first task (the powershell script). This is where our ARM templates will be copied to from the Azure DevOps Repo.
After 2 min and 32 seconds the pipeline finished:
Now let’s see what we have in azure portal:
We have the resources we defined with ARM templates and please notice that the temporary storage account endpoint999 is not present anymore. It has been automatically deleted.
As infrastructure has become part of the iterative process, ARM templates is a great option to use. You can automate deployments; you define the infrastructure that needs to be deployed and then the infrastructure code becomes part of your project.
As we discussed, Linked templates are a great method to manage and break down the solution into targeted components. You can easily reuse these templates for other scenarios.
Thank you for reading this post!