Azure Automation Made Easy
Sean Sparkman (@seansparkman)
june 27th, 2017
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.
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.
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.
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.
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.
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.
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
listKeys. Variable and
parameter substitution syntax is done by placing
limited. There are a small set of
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
"value": "[concat(parameters('prefix'), uniqueString(resourceGroup().id))]",
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.
"value": "[listKeys(parameters('storageAccountId'), '2016-01-01')]",
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.
Tags: technology azure