ASP.NET, CMS, Managed Services

Azure 1-Click sitecore VM deploy using “Azure PowerShell”

Overview:

Microsoft Azure is a leading Cloud Service provider providing a wide range of IaaS, PaaS services. We can create a VM in Azure within minutes, however, there are many focus areas to further automate repetitive tasks or activities by combining all of the tasks into PowerShell and execute a single PowerShell script with very minimal inputs (names, environments etc.) and create a set of VMs as per our requirements.  In this post I will be covering automating the creation of Azure VMs using Azure PowerShell.

Few advantages of this approach are:

  1. Script can be executed by anyone who doesn’t have any Azure knowledge and still be able to spin up complex infrastructure in Azure Cloud and start working on them.
  2. Create multiple VM images with all the software required and plug in all of them to contribute to one single eco system (Dev, QA and Prod) with in a single operation. Alternatively, introduce required scripts at the time of VM creation to install software on the go based on the requirement.
  3. Flexibility to automate multiple operations on Azure with a single script.
  4. Repeatable process of spinning VMs / Services with the desired setup

Why Cloud:

  1. Less time to Provision
  2. Pay as you go
  3. Highly and instantaneously scalable with auto scaling options
  4. Deploy with ease

What is Azure PowerShell?

  1. PowerShell is a windows scripting language. It is an interactive command line shell for windows.
  2. it is designed to automate system tasks and supports object level programming.
  3. Azure powerShell is a set of modules and commandlets which gets installed on top of windows powerShell to perform Azure Tasks from the command line.

Process:

Pre-Requisites:

  1. User – Proficiency in Windows Poweshell + knowledge on Azure Powershell commandlets.
  2. Azure Subscription along with an azure admin user account
  3. Azure Powershell commandlets installed on the machine from where the operation is performed.
  4. Storage Account already created in Azure Subscription for saving generalized VM Images.
  5. Azure VM Images (generalized and saved into storage account specified #5) for the scenarios. Ex: 1VM with database and Sitecore Installed on single machine for Development setup, 1VM installed with Sitecore without Database installation. We may reuse these images to spin up Dev, QA and Prod Setups for Sitecore by executing post deployment scripts.

The approach we followed involves creating an Azure Powershell Script which will talk to our Azure Subscription and then create new VMs using a pre-defined software requirements created by us which will have all the required setup pre installed. We will be using Azure Commandlets and then combining all the commands in to one single powershell which can accept Arguments like VM Count, BaseName(common name), Size Of VM to be created etc.

Note: In order to create a single script we need to combine all the operations we do using powershell like a piece of puzzle in an orderly fashion. In this post I will explain the order in which the commandlets are executed and you can customize your own script based on the instructions provided.

Typically to get this to work we need to understand the below 2 steps and then be able to customize the script by introducing Arguments and ‘FOR’ loop with in powershell to create multiple VMs with different scenarios:

Step1 – CREATING VM IMAGES / TEMPLATES :

  1. Create VMs in Azure and install the required software and setup configuration specific parameters on all the VMs created.
  2. Generalize the VMs we created. Generalization is the process followed to make the VM ready to export / duplicate to another VM using Image, as it removes all the GUIDs from the Operating system.
  3. Save (capture) the generalized VM and save it to a storage account attached to the Azure Subscription, note the name of the storage account . Ensure the URLs for the different VMs created are noted down somewhere as we will be using these in script. NOTE: You will loose the original VM once you generalize it. So, ensure you don’t generalize a VM which is in use.

Reference for Creating a VM image from existing Azure Resource Manager Virtual Machine: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/capture-image

Step2 – Create A new VM from Generalized VM Image:

  1. Once the VHDs are uploaded, take a look at the operations performed in the below Microsoft article below. The article below explains how to create a VM from existing generalized VM we created in Step1.

Reference : https://docs.microsoft.com/en-us/azure/virtual-machines/windows/upload-generalized-managed

Step3 – Create your own PowerShell script:

This step is highly customizable and would change based on the requirement. Here in this article we will discuss about the one such use case and how its executed based out of the commandlets used and understood in Step1 and Step2.

  1. Save the VM Image URLs with their respective friendly name into a CSV file. For Example to spin up a VM with SQL Server Installed, we will read the CSV file for the URL and take the argument for the friendly name while executing the powershell which will create the respective VM. i.e, we will provide the argument as SQL and execute the powershell which will create a SQL VM.
  2. Same logic can be applied by taking input argument for Environment as DEV or QA while executing the powershell. Ex: We can write the logic in powershell to take QA as input argument and introduce IF and Else conditions within the powershell script to spin up 2 VMs with 1 as a SQL VM and 1 as a Sitecore VM.
  3. We may introduce a logic with in powershell to generate a random password for the VM and save it on to a variable and while creating the VM we will use this password and save the password in to a text file for accessing.
  4. We may extend step3 to save all the VM details created by the powershell script in to a text file along with IP addresses, username, password and DNS name etc. by introducing the logic within powershell.
  5. For post deployment steps introduce custom scripts in to powershell script while the VM is being created, reference: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/extensions-customscript or http://www.techdiction.com/2016/02/12/create-a-custom-script-extension-for-an-azure-resource-manager-vm-using-powershell/ . Ex: Enable WinRM on the VM once it is created so that the we can introduce powershell commandlets to modify the connectionstrings file (of the Sitecore VM) to point to the newly created database server.
  6. Enable WinRM on the VM once the VM creation step is created to modify the connection strings on the Sitecore Server to point to the database server which is created on the fly.

Some Screenshots to exhibit how the execution looks like:

  1. Powershell script being called – user inputs arguments as below:

# request number:  a random number to identify the operation

# Orgname: Orgname for identification purpose

# env: dev / qa / prod  (Dev will created one Sitecore VM with DB and Sitecore on one VM ; QA will create 2                               individual VMs one VM with database and one VM with Sitecore installed.

# C : It’s the count, the number of VMs sets you want to spin for each environment.

# Cname: It’s a common name to prefix the created VMs for their naming convention. Ex: In this example it will                     create a VM with name tekkansitecore0

  1. So the above arguments will spin up 1 VM with Sitecore and Database on a single VM.
  2. Based on the cname the rest of the resources will be created with same prefix. Ex: Public IP name will be “tekkansitecorepip0” , below screenshot shows the progress happening through powershell:

 

 

 

  1. While the powershell is being executed, you could rightaway check the Azure Portal and you will notice that the script started doing its Job.

  1. Once the script completes execution, the VMs will go to “Running” State as below:

  1. Finally, we have introduced a logic in powershell which will export the VM information along with the passwords to an excel file and this excel file will have full info about the VMs created. Instead of Excel file, we could also use database updation logic with in powershell to securely save the VM information into database instead of excel file. It will look something like below:

Leave a Reply

*