Azure ARM (Azure Resource Manager) templates describe complex computing environments as plain, human readable and editable code. In education, ARM templates can be used to instantly deploy a preconfigured learning environment for the students, which is discarded after the course completed. This on-demand solution minimizes both the cost and effort for Education IT, and the environment can be spun up and removed in minutes. This blog is a short intro on using ARM templates, and shows how you can do this as well in your own environment.
Educational organization X has a week long course “Using WordPress” for adult students. This course is run on demand basis with identical content multiple times a year. The course typically has 20-30 students each. Each student needs a ready to run, empty WordPress (WP in short) instance with and URL to access it, an admin user id and a password for the WP. After the course has finished all data is erased for the next run.
The solution is to create an Azure ARM template which describes the single wordpress virtual machine instance (including the networks & firewalls for the environment) and a loop that you could give parameter how many of these machines you’d like to have this time. In addition to the ARM template we need a simple Powershell script to ask the parameters from user, login to azure and then issue the command to “Create the environment described in this ARM template file using these parameters I just typed in“.
Every time the course is held, IT dept runs on the previous day the script to create required amount of WP instances, and delivers the resulting WP URLs, userid’s and passwords to the course instructor. When the course is finished, IT deletes the Azure Resource Group containing the environment and virtual machines with a few button clicks from the Azure portal.
We also made a helper script for collecting the created WordPress URLs, user id’s and passwords in a nice list to give to the course instructor, and a small Azure Automation which turned off the machines in the evening, and started them up in the morning. This was just to minimize the cost – virtual machines are billed by minutes they are running.
Related use cases
The script is used to create Bitnami WordPress instances based on Azure marketplace template, but the same script can be used to create any other marketplace resource. Examples include (but are not limited to) ready-to-use, fully configured LAMP stack, MySQL, Moodle, Apache, Jenkins or PostgreSQL just to name a few. In this case you just change the virtual machine definition in the ARM template, which is the azuredeploy.json file in the following example.
Next, we’ll look at how the trick is done.
- You already have an Azure Subscription, and admin account credentials to login to Azure portal.
- If you haven’t already done, install Powershell, and import AzureRM module into it on your local workstation.
- Find your Azure Subscription ID. You’ll need this when powershell script asks which subscription you want to use.
- Go to portal.azure.com, select Subscription from left menu. Click on a subscription, and the resulting page “Overview” shows your Subscription ID (long string).
Get the files
Download all of the following files using the button below, and place them all in the same directory on your local Powershell machine.
- azuredeploy.json – ARM template describing the environment to be deployed.
- azuredeploy.parameters.json – This file is used to pass the parameters from powershell to ARM template. Powershell overwrites this file on each run, but the file needs to exist.
- CreateWordpressMachines.ps1 – Powershell file to input the parameters and then run the deployment.
Optional files for your convinience:
- GetWpPasswords.ps1 – Helper powershell script to get WP URLs and userid/passwords from your newly created WP instances.
- azure-automation directory – if you want to build automation to shut down and start up the servers on schedule, this is my helper Runbook for Azure automation. Not explained any further on this article.
Run the script to create the WP VM’s
Start your Windows Powershell prompt, or use Powershell ISE (as always – preferrably in Admin mode), and just run the CreateWordpressMachines.ps1 file with Powershell in the directory you downloaded the files.
Image: Powershell running the script
The script will ask you for:
- Subscription ID to be used when creating the wordpresses.
- Resource Group name to be created where to put the machines.
- Number of machines to create (min 3, max 250)
- Admin username & password for linux virtual machines used to run the WP instances. Normal password rules apply (letters, numbers, special character requirements)
- Base name for deployment. (lowercase, 6 – 16 chars) This name will appear on all resources created. Virtual machines will be named basename1, basename2 etc. Only lowercase letters!
After this the script will ask you to log in with user name and password that has rights to administrate resources in the selected subscription. After providing the login information the script starts creating the resources. You can watch the resources appearing in the portal.azure.com. The script will usually run 8-15 minutes, and when finished, you’ll get “All finished” notification. Your WordPress / Ubuntu machines are now up and running, and generating billable minutes.
IF you want to automatically pick up the WP URLs, userid / passwords, run the helper script GetWpPasswords.ps1 NOW right after the deployment has finished. The WP user/pass info is stored on each virtual machine boot log, which gets permanently overwritten when you stop and start the virtual machine next time. After salvaging the user/pass from the boot log, you are free to stop and start the machines as you like.
Stopping and starting the virtual machines
Stop the virtual machines when not in use from portal.azure.com by selecting the virtual machine and hit the Stop-button on top of virtual machine overview screen. Start vm’s with the Start button. Azure stops billing for virtual machines when the stop command is issued from portal. You also have an option to create Azure Automation Runbook(s) that automatically stop and start the machines on given schedule, but this is outside this blog article scope. Please refer to links in the end of this article for more information.
Deleting the virtual machines after use
Go to portal.azure.com and delete the resource group you created. Select “Resource groups” from left menu, open the resource group containing the virtual machines and select “Delete” from resource group overview top bar. You’ll have a confirmation screen, and after that everything in the resource group is deleted, and billing stops. For the next course run, start from beginning and run the create script again.
A week long WP course cost with 30 students? Roughly 50 EUR. Here’s how the cost is calculated: Go to Azure pricing calculator, it uses Azure list prices to calculate estimates for your environment. In our case the cost is calculated as follows:
- Number of Basic A1 ubuntu virtual machines * hours running +
- Estimate of 5 GiB of disk storage for each virtual machine
With 29.5.2017 list prices, 30 virtual machines running for five working days course, and virtual machines are stopped at 19:00 and started again at 07:00 every morning. Each vm takes 5 GiBs of disk storage (a bit of an overestimate, but hey, just to be sure).
- Hours running = 30 vm’s * 5 days * 12 h = 1800 hours
- Blob storage for VM disks: 30 vm’s * 5 GiB each = 150 GiB
Virtual machines: 40,98 EUR + storage 6,33 EUR =
Total 47,32 EUR
You can also export an Excel from pricing calculator, included here for your convinience for our example scenario.
Image: Azure pricing calculator at https://azure.microsoft.com/en-us/pricing/
For the curious…
Go ahead, and open the ps1 and json files included! Powershell files (ps1) contain the imperative commands that make things happen, and the template.json file is the actual ARM template which describes the environment created.
The ARM template is a json-formatted representation of resources in the system. If you open it, you’ll see that the template first defines for each virtual machine a data and diagnostic disk, and a network interface. Then it creates a firewall (network security group), a virtual network and a subnet, and attaches the network cards created into the subnet. Finally, when everything else is done, the script start creating the VM’s from selected image, and attaches the network interface and the disks into the VM.
ARM templating can be a bit tricky first, but as said before, you can use the ARM template to create any other virtual machine resource, if you change the part that defines which image the template uses to create the virtual machine. Now it creates Bitnami WordPresses, but you can change it to create any other resource as well. Be brave and experiment!
Here’s a little something to get you started:
- Understanding ARM template syntax (the nuts and bolts)
- Quickstart ARM templates (never start from scratch!)
- Export existing Resource Group as ARM template (and use this as your ARM template starting point)
- Azure Powershell overview (start here!)
- Technet Script Center, Azure Virtual machine powershell scripts (never start from scratch either with ps scripts.)
A little disclaimer in the end: This post is written on 26.5.2017. The scripts, modules, conventions and links tend to change over time, sometimes very quickly. I tested the scripts today, and they work – now. However I can offer no guarantees that they’ll work tomorrow. Please notify me if and when something breaks.
Leave A Comment