Showing posts with label ARM. Show all posts
Showing posts with label ARM. Show all posts

Monday, 10 July 2017

Convert Azure Windows virtual machine license to Hybrid Use Benefit

EDIT: This functionality is now directly in the portal. Just select the VM, click configuration and you should now see the ability to switch license type.




Microsoft has recently announced their hybrid use benefit for Windows virtual machines. They claim a 40% lower price on Windows VMs in Azure which is certainly a good thing.

If you license your on premises hosts with Windows Datacentre and also run some Azure Windows VMs then you can use the license for both - at the same time!

"Each two-processor licence or each set of 16-core licences are entitled to two instances of up to 8 cores, or one instance of up to 16 cores. The Azure Hybrid Use Benefit for Standard Edition licences can only be used once either on-premises or in Azure. Datacenter edition benefits allow for simultaneous usage both on-premises and in Azure."

The only issue with this offer is that you need to enable the hybrid use at deploy time on your VMs. This isn't a problem for ephemeral VMs, but if you have permanent VMs that you want to save some money on, then you need to re-provision them.

It is possible to delete your VMs and then recreate them using the original VHD, with the hybrid use benefit enabled. I've written a script which will do just this and published it on GitHub here.

As always, DO NOT use this in production and make sure you have backups of your machines before you use the script!

Be aware that this will only work on some VMs, I've not yet worked out the prerequisites. The script *should* detect if it hasn't worked and put your VM back with it's original license if the ARM platform throws an error. To put this in perspective, I've run the script on 8 VMs, 1 of which didn't work.

Update: Should now work with managed disk VMs

Tuesday, 17 January 2017

Azure Resource Manager (ARM) Templates Getting Started Guide

Preface
I've been doing some research and development work on ARM templates recently and thought I would put together a getting started guide while I was at it.

There are some great online references for ARM template creation:





Getting Started

The basics
ARM templates are JSON formatted files that describe Azure Resources. You can send the file off to Azure in a number of ways and the resources described will be implemented by the Service. ARM resources are deployed in parallel, so even complicated templates can be deployed quickly.

The ARM template is broken down into four sections: Parameters, Variables, Resources and Outputs. Only one section is actually required to have content - resources - the others help to make the templates more dynamic and flexible.

I use Visual Studio Code for creating and editing ARM templates as it has some excellent plugins which provide intellisense when creating resources. Using this means that you can just hit Ctrl+Space to get a hint at the possible resource properties on the fly. To set up VS Code for intellisense, the official guide is here.

You can also download templates from the Azure Portal to edit later. You can download templates from created resources, from the last page of the resource creation process, or even from entire resource groups.

Here is a really basic, valid ARM template to create a Storage Account. It's pretty rigid in that the storage account name is hard-coded, to fix that, parameters can be used to take user input.



Execution
ARM templates can be deployed in a number of ways: PowerShell, X-plat CLI and the Azure Portal are some common ways.

The following PowerShell code will create a Resource Group and deploy the template into it.



Taking input
The parameters section allows the template to take input. This input can be from the user at deployment time, from a parameters answer file or from another template. A basic parameter looks like this:


Storing Variables and Transforming input
The variables section is useful for holding regularly used values or for transforming input. For example in some of my templates, I use a resources prefix which is concatenated with resource names so that related resources can be seen at a glance.

The example includes a variable to apply a tag to a resource. In this example there is only a single resource, but in more complicated templates, the variable can be reused in all of the resources.



Producing Output
It can be useful to produce outputs from a template to get information on the resources that were created. In this example the DNS name assigned to the public IP is returned after deployment. Outputs can also be used in chained templates.


Creating multiple copies of a resource
A really powerful feature of ARM templates is the copy loop. This means you can create multiple, identical copies of specific resources in your template. For example you can create 3 copies of a VM using one code block. When you use a copy loop, you can reference the copyIndex() function in order to generate unique names for resources. In the following example, there is a simple copy loop to generate 3 public IPs. 



Using arrays in loops
Arrays can be combined with the copy loop in order to name resources with a list of predefined or runtime input names. In the example, the array is generated as a parameter and then indexed using the copyIndex() function. The count of the copy property can be set to the length of the array so that all array elements are indexed dynamically.


Running scripts in VMs using the extension resource
A nice part of Azure Resource Manager is the ability to add virtual machine extensions. One such extension is the script extension which allows the Resource Manager service to run scripts inside a virtual machine. No external access is required to the virtual machine since Azure executes the script for you. Scripts need to be hosted on an accessible web server or storage account. If there is any sensitive information in the script you should use storage keys and a key vault to access the script privately from the template. The following example just runs a publicly accessible script in the virtual machine 'myVirtualMachine'.


Linking to other templates for complex solutions
Chaining templates is super useful for creating complex solutions. For instance you can create a template to build an availability group of VMs, then call that template from another template with the copy loop and parameters to build multi-tier applications. 

I have a full implementation of this on my GitHub, the implementation will loop through several templates and create multiple availability groups and VMs. Be careful if you run this as it will spawn a lot of resources.

Here is the 'master' template which calls the other templates in the solution:



And here are all the resources it creates!




Wednesday, 2 November 2016

Terraform with Azure Resource Manager


I was intrigued by Terraform for building full environments in Azure. I searched for an example but only found a classic Azure set of Terraform files here. As such, I set about creating an example set to build a small amount of resources in Azure RM using Terraform.

Set up your Azure RM credentials

Before you can deploy any resources in Azure RM you need to set up your Azure credentials with Terraform. I followed the full RM portal guide at the Terraform site and was unable to select my custom application to add the role. Once I followed the guide at the Microsoft site using the classic portal I was able to find my custom app in Azure AD and successfully completed the Terraform guide.


Microsoft Guide Here

It's quite possible that following the classical portal guide on the Terraform site will work just fine. Once completed you will have 3 GUIDs and one key to use for authentication with Azure.

Edit: I've written a quick script to create a Terraform App Registration and return credentials for use here.


I am using Powershell as my console, so I set my environment variables as such:


Once the API keys have been loaded into environment variables, I created the following files. There is a file for each major resource component. Terraform seems to handle the dependencies automatically so no ordering is required.


The next step for this is to pull all of the resource names into a Variables.tf file so they can be set at build time. Also this configuration doesn't include a public IP for the VM so it's fairly useless until that's added. I'll update this post once I've added the IP address.






Tuesday, 11 October 2016

Create Snapshots of an Azure RM VM

Azure-VM-Snapshots

-------------------------------------
Powershell Functions for Creating Azure RM VM Snapshots

Usage:

Load the Functions:
C:> . .\AzureSnapFunctions.PS1

Create a New snapshot for all VHDs in a VM
C:> Snap-AzureRMVM -VMName MyVM -SnapshotName "Snap 1"

View the snapshots for all VHDs on a VM
C:> Get-AzureRMVMSnap -VMName MyVM

Delete all snapshots for all VHDs on a VM
C:> Delete-AzureRMVMSnap -VMName MyVM

Revert to a snapshot for all VHDs on a VM
C:> Revert-AzureRMVMSnap -VMName MyVM






Nutanix CE 2.0 on ESXi AOS Upgrade Hangs

AOS Upgrade on ESXi from 6.5.2 to 6.5.3.6 hangs. Issue I have tried to upgrade my Nutanix CE 2.0 based on ESXi to a newer AOS version for ...