Azure Automation Made Easy

« Return to Our Notebook

Azure Automation Made Easy

Microsoft Azure Cloud
Azure supports multiple platforms

So, what is Azure? Azure is Microsoft’s cloud solution. It’s a collection of services used to build, deploy, and manage applications. We do a lot of work with Azure here at Infinity. The great thing about Azure is that it’s not limited to Microsoft platforms but also fully supports PHP, Node, Linux, and many other Open Source technologies.

Microsoft provides a tool called Azure Resource Manager (ARM), which allows you to easily provision your applications with templates and deploy multiple services and their dependencies. ARM templates are reusable, so whether you are creating a staging environment or deploying a new instance of a product, you can use them to simplify the deployment process for any project in Azure.

ARM Templates

You do not have to learn a new language or muck about with XML to use ARM templates. They are written using JSON, which is a convenient and widely supported option for writing configuration files. Unfortunately, JSON by itself does not support variable expressions. In order to support variables and template re-use, Microsoft provides a form of expression that is loosely based on Javascript that can be used inside templates.

"resources": [
    {
        "apiVersion": "2014-06-01",
        "type": "Microsoft.Web/sites",
        "name": "[parameters('siteName')]",
        "location": "[resourceGroup().location]",
        ...
    }
]

Inside your templates, you can create dependencies, output data, set tags, and so on. Anything you can do in the portal is possible with ARM templates, but it can also be automated to avoid human error. This allows you to confidently create consistent environments inside Azure.

In the past, developers and IT professionals needed to use PowerShell commands to manage Azure. The Azure CLI added support for Bash. ARM provides a RESTful API, which allows you to run your templates from the provided SDKs, freeing you from any dependency on PowerShell or Bash. In addition to running these languages in your environment, you can also use .NET, Java, Node, PHP, Python, or Ruby SDKs to execute your templates and create your Azure environment.

Generating ARM Templates

In the Azure cloud, resources are grouped together in Resource Groups. Typically, Resource Groups are tied to a specific region, but they can span multiple regions. Azure creates ARM templates that are available through the portal under the “Automation script” blade or vertical tab inside of your Resource Group. This blade provides a parameterized ARM template for the user and scripts to run the template with CLI, PowerShell, .NET, or Ruby.

Example showing the automation script blade

Template Library

Generated templates are great, because you can save the template to your library in the Azure portal. On the “Automation script” blade, select the “Add to Library” button to save the template. The portal will take you to a screen with name and description fields. Fill them in and click “Save”. If this is the first time you’ve saved something, it may take a few moments for Azure to create your library. There’s a chance you will need to click “Save” again. Now your Resource Group template is saved in the Azure portal.

Example highlighting the add to library button in the azure portal on the automation script blade

Once it’s saved there, you can use it to create more resources in Azure. Click on the “More Services” button to open the services blade. Scroll down to the bottom to find the “Template” button. The portal will provide you with a list of the templates you have saved. Select the template you wish to use. It will now show you the template itself as one file.

Example highlighting the Templates button from the More Services blade in the Azure portal

Example showing the Templates blade

You can choose to edit or deploy the template. When you click “Deploy”, Azure will give you the option to fill in values for all your parameters and select or create a Resource Group. Click “Purchase” once you have entered all the options you want. Remember you cannot reuse the same name of a resource, even if it’s in a different Resource Group — you must use a unique name.

Example showing the screen to deploy an user created template inside of the Azure portal

From your template library blade, you can also create your own templates or copy and paste your templates. Currently, there is no option to upload template files.

Parameters and Variables

Like all programming, you need variables to make code dynamic. Otherwise your program will do the same thing every time you execute it. In the case of ARM templates, variables are generated inside the template. Parameters are values passed into the template. Parameters can have default values provided.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "app_name": {
            "value": "example-app-name"
        },
        "namespace_name": {
            "value": null
        },
        "notification_hub_name": {
            "value": null
        },
        "database_server_name": {
            "value": null
        }
    }
    ...
}

Variables and parameters can be used inside the templates to provide values for properties. This becomes more powerful when using the template’s functions like concat or listKeys. Variable and parameter substitution syntax is done by placing [ and ] brackets inside a JSON value. The syntax is similar to Javascript but is very limited. There are a small set of functions you can use. Strings are denoted with single quote marks, because JSON values require the double quote marks and multiple double quote marks would cause a conflict. You must use the concat function to combine strings.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "prefix": {
            "type": "string",
            "defaultValue": "prefix"
        }
    },
    "variables": {
        "appName": "[concat(parameters('prefix'),'-app')]",
        "appServiceName": "[concat(variables('app'),'-service')]"
    },
    "resources": [
        ...
    ],
    "outputs": {
        "concatOutput": {
            "value": "[concat(parameters('prefix'), uniqueString(resourceGroup().id))]",
            "type": "string"
        }
    }
}

For example, using listKeys, I can set an app setting for my app service with a security key for the storage account that was created. Now, I can use the storage account key in my web application without having to store it in my web.config file or manually configure the setting. This is a more secure solution as the key doesn’t have to be passed around or stored in a source repository.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.Jon#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageAccountId": {
            "type": "string"
        }
    },
    "resources": [
        ...
    ],
    "outputs": {
        "storageKeysOutput": {
            "value": "[listKeys(parameters('storageAccountId'), '2016-01-01')]",
            "type": "object"
        }
    }
}

Resources

For more examples of ARM templates, be sure to visit the Azure Quickstart Template repository. Microsoft also provides great documentation on the template format.

Keep an eye out for our next post on Azure automation and how we leverage it to create the backend for TPC 2017 DC mobile application — it will be posted soon. The Perl Conference is over for the year, but if you'd like to take a look at the app, you can still find the mobile application in the Google Play Store and the Apple App Store.

We solve problems with technology. What can we solve for you?

Reach Out

t: 800.646.0188