Paginas

24 June 2017

Automation Azure - Script to Change Virtual Machines Size ARM & ASM


In this post I'll show you how to resize automatically azure virtual machines.
Microsoft Azure offers some cheap low spec virtual machines. But as we all know, the prices can go up pretty quickly as the specs go up. But we need large VMs to support today’s modern day workloads. The downside is that most of these workloads are only present during business hours but you end up running and paying for these large VMs even during non-business hours and on weekends.
 
The solution here is to have an azure automation script that resizes the VMs to the lowest possible size during non-business hours and have another automation script to resize the VMs to their original size just before start of business.
 
Because Azure has two portals (for now..), I'll show you the scripts from both (since threre are many differences).
  • Old Portal - Azure ASM (Azure Service Manager)
  • New Portal - Azure ARM (Azure Resource Manager)
Let's see how to do it!
 

What is automation?

Microsoft Azure Automation provides a way for users to automate the manual, long-running, error-prone, and frequently repeated tasks that are commonly performed in a cloud and enterprise environment. It saves time and increases the reliability of regular administrative tasks and even schedules them to be automatically performed at regular intervals. You can automate processes using runbooks or automate configuration management using Desired State Configuration. This article provides brief overview of Azure Automation and answers some common questions. You can refer to other articles in this library for more detailed information on the different topics.
Automating processes with runbooks
A runbook is a set of tasks that perform some automated process in Azure Automation. It may be a simple process such as starting a virtual machine and creating a log entry, or you may have a complex runbook that combines other smaller runbooks to perform a complex process across multiple resources or even multiple clouds and on premise environments. 
For example, you might have an existing manual process for truncating a SQL database if it’s approaching maximum size that includes multiple steps such as connecting to the server, connecting to the database, get the current size of database, check if threshold has exceeded and then truncate it and notify user. Instead of manually performing each of these steps, you could create a runbook that would perform all of these tasks as a single process. You would start the runbook, provide the required information such as the SQL server name, database name, and recipient e-mail and then sit back while the process completes.

 
What can runbooks automate?
 
Runbooks in Azure Automation are based on Windows PowerShell or Windows PowerShell Workflow, so they do anything that PowerShell can do. If an application or service has an API, then a runbook can work with it. If you have a PowerShell module for the application, then you can load that module into Azure Automation and include those cmdlets in your runbook. Azure Automation runbooks run in the Azure cloud and can access any cloud resources or external resources that can be accessed from the cloud. Using Hybrid Runbook Worker, runbooks can run in your local data center to manage local resources.

 
Creating Runbooks with Azure Automation
 
You can create your own runbooks from scratch or modify runbooks from the Runbook Gallery for your own requirements. There are four different runbook types that you can choose from based on your requirements and PowerShell experience. If you prefer to work directly with the PowerShell code, then you can use a PowerShell runbook or PowerShell Workflow runbook that you edit offline or with the textual editor in the Azure portal. If you prefer to edit a runbook without being exposed to the underlying code, then you can create a Graphical runbook using the graphical editor in the Azure portal.

You can read more in here: https://docs.microsoft.com/en-us/azure/automation/automation-intro


The Scripts
There are four scripts (two for ASM and two for ARM) divided in SizeUp and SizeDown. They will work only in business days (five days a week) and they will resize automatically six virtual machines. You can add many has you want. So, lets start.


DownSize Azure ARM
PowerShell Workflow Runbook

workflow DownSizeARM
{
    $day = (Get-Date).DayOfWeek
    if ($day -eq 'Saturday' -or $day -eq 'Sunday'){
    exit
    }
$cred = Get-AutomationPSCredential -Name <AUTOMATION_NAME>
Add-AzureRmAccount -Credential $Cred
Select-AzureRmSubscription -SubscriptionName <NAME_SUBSCRIPTION>
Function DOWN-SIZE{
    [CmdletBinding()]
    param(
        [parameter(Mandatory=$true)]
        [string]$ResourceGroupName,
        [parameter(Mandatory=$true)]
        [string]$Name,
        [parameter(Mandatory=$true)]
        [string]$VMSize
    )
PROCESS{
    $vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $Name
    $vm.HardwareProfile.vmSize = $VMSize
    Update-AzureRmVM -ResourceGroupName $ResourceGroupName -VM $vm
}
}
DOWN-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Basic_A0"
DOWN-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D1"
DOWN-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D1"
DOWN-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Basic_A0"
DOWN-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D1"
DOWN-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D1"
}


UpSize Azure ARM
PowerShell Workflow Runbook

workflow UpSizeARM
{
    $day = (Get-Date).DayOfWeek
    if ($day -eq 'Saturday' -or $day -eq 'Sunday'){
    exit
    }
$cred = Get-AutomationPSCredential -Name <AUTOMATION_NAME>
Add-AzureRmAccount -Credential $Cred
Select-AzureRmSubscription -SubscriptionName <NAME_SUBSCRIPTION>
Function UP-SIZE{
    [CmdletBinding()]
    param(
        [parameter(Mandatory=$true)]
        [string]$ResourceGroupName,
        [parameter(Mandatory=$true)]
        [string]$Name,
        [parameter(Mandatory=$true)]
        [string]$VMSize
    )
PROCESS{
    $vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $Name
    $vm.HardwareProfile.vmSize = $VMSize
    Update-AzureRmVM -ResourceGroupName $ResourceGroupName -VM $vm
}
}
UP-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D3"
UP-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_A1"
UP-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D2"
UP-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_A1"
UP-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D2"
UP-SIZE -ResourceGroupName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D2"
}


DownSize Azure ASM
PowerShell Workflow Runbook

workflow DownSizeASM
{
$day = (Get-Date).DayOfWeek
if ($day -eq 'Saturday' -or $day -eq 'Sunday'){
exit
}
$cred = Get-AutomationPSCredential -Name <AUTOMATION_NAME>
Add-AzureAccount -Credential $cred
Select-AzureSubscription -SubscriptionName <NAME_SUBSCRIPTION>
Function DOWN-SIZE{
[CmdletBinding()]
param(
[parameter(Mandatory=$true)]
[string]$ServiceName,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$Name=$ServiceName,
[parameter(Mandatory=$true)]
[string]$VMSize
)
PROCESS{
Get-AzureVM -ServiceName $ServiceName -Name $Name | Set-AzureVMSize $VMSize |
Update-AzureVM
}
}
DOWN-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Basic_A0"
DOWN-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D1"
DOWN-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D1"
DOWN-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Basic_A0"
DOWN-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D1"
DOWN-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D1"
}


UpSize Azure ASM
PowerShell Workflow Runbook
 
workflow UpSizeASM
{
$day = (Get-Date).DayOfWeek
if ($day -eq 'Saturday' -or $day -eq 'Sunday'){
exit
}    
$cred = Get-AutomationPSCredential -Name <AUTOMATION_NAME>
Add-AzureAccount -Credential $credSelect-AzureSubscription -SubscriptionName <NAME_SUBSCRIPTION>
Function UP-SIZE{
[CmdletBinding()]
param(
[parameter(Mandatory=$true)]
[string]$ServiceName,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$Name=$ServiceName,
[parameter(Mandatory=$true)]
[string]$VMSize
)
PROCESS{
Get-AzureVM -ServiceName $ServiceName -Name $Name | Set-AzureVMSize $VMSize |
Update-AzureVM
}
}
UP-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Small"
UP-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D3"
UP-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D2"
UP-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Small"
UP-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D2"
UP-SIZE -ServiceName <NAME_RG> -Name <NAME_VM> -VMSize "Standard_D2"
}


In the classic deployment model (ASM), some VM size names are slightly different in CLI and PowerShell:
  • Standard_A0 is ExtraSmall
  • Standard_A1 is Small
  • Standard_A2 is Medium
  • Standard_A3 is Large
  • Standard_A4 is ExtraLarge

Now you can create schedules and associate with the Runbooks you just created. For instance you can schedule a SizeUp everyday at 8:30AM and SizeDown everyday at 9:00PM.

You can monitor all the Runbooks to track errors and see the logs/outputs:

 
See you next time!

1 comment: