Azure’s YAML provisioning templates

Azure’s ARM templates have been a rite of passage for all Azure engineers.  No one who has been working with the cloud at any depth will be without late-night stories of frustration.  The Azure Resource Manager is a service that accepts specially constructed JSON templates and uses those templates to provision each object represented within.

Every element in Azure is an object of a particular type with values set on its properties.  How those objects are represented is completely up to us.  JSON based ARM templates is only one method of natively dealing with Azure’s provisioning service.

This post will demonstrate that a couple of extremely simple functions can allow YAML templates to be used with Azure.  By not using ARM templates for sequencing the deployment of Azure objects we have to use a scripting language.  I’m using PowerShell but could have as easily used Python. 

Two prerequisites are needed on a system to work through this example.

The open module “powershell-yaml” needs to be installed once on a system.  For ease of use, the routines I’ll discuss in this post have been made available in a module called 'AZRest' that can be downloaded here. https://github.com/LaurieRhodes/azure-yaml

Example – Creating an Object

We have four steps required to create objects in Azure.

Authenticate.

Authentication in this example will be using a function titled “Get-Header” which creates Azure or Office365 headers for REST.  Previous blogs have discussed how the authentication headers are created.  Instead of using a PowerShell function, you might prefer to use the Microsoft Authentication Library (MSAL) as a dll.

Get API List.

 In a previous post I discussed programmatically retrieving the latest Azure API versions for object types and showed the code behind ‘Get-AzureAPIVersions’.  This is needed to know the right version of API used when querying objects based on their ID.  Object ID are written in templates.

 Convert YAML to PowerShell Objects.

Uses the ConvertFrom-YAML command of ‘PowerShell-YAML’ to create a PowerShell object from saved YAML.

Push PowerShell object to Azure

All Azure / PowerShell objects have an Id property.  By deconstructing the ID string we can find the right API version to make a REST call for creating that object.

Example – Creating a Virtual Machine

The four steps above are demonstrated with the code below.  As many files as are liked may be linked as part of a deployment.

Note that this is not a “framework”.  It’s simply manipulating & managing the same azure objects in PowerShell and YAML.  You still need to know how to configure different objects.  Microsoft keeps REST based API documentation full updated at: https://docs.microsoft.com/en-us/rest/api/azure/.

Because PowerShell objects, YAML objects and Azure objects are mirrors of each other, I can set properties of an object directly with PowerShell as part of my deployment process.  For example, I may wish to change a password with a Virtual machine before it is deployed:

Generating Templates

Two lines of code will write an existing Azure object to YAML.

Any YAML templates made from deployed objects need to be reviewed and cleaned up but there is a simpler way to create deployment templates and that is by using the JSON examples provided by Microsoft for REST API provisioning.

With this example I have decided to take the request body for generating the Azure Firewall from Microsoft’s documentation and copied the JSON.

This time I can “Convert From JSON” to a PowerShell object which can be converted to a YAML template.

All that is required for the produced template to be used is for an ID reference to be added to the template and properties customised as require.

Best of all, comments can be made in templates to give hints on what should be changed.