There are so many auto shutdown scripts available to manage single instance Azure virtual machines, but how do we manage the snoozing / user defined scheduling of instances in virtual machine scale sets in non-production environments as part of FinOps.
Stand-alone virtual machines can be snoozed via the auto-shutdown feature or via the automation tasks. You also have the option to configure the start/stop plan in the marketplace for a scaled solution across multiple availability zones, multiple subcriptions and resource groups.
None of these solutions are however capable of managing virtual machine scale set instances. So how do we configure user defined schedules for virtual machine instances inside a virtual machine scale set? These instances have no Automation Tasks and the start/stop plan does not work on these instances. So I configured an Azure Automation Runbook specifically for virtual machine scale set instances.
Depolyment Plan
Step 1 – Create an Azure Automation Account
Step 2 – Provision the SAMI
Step 3 – Assign the SAMI Role
Step 4 – Import the Necessary PowerShell Modules
Step 5 – Create 2 Runbooks
Step 6 – Information Gathering
Step 7 – Add the PowerShell START script
Step 8 – Creating your STOP powershell script
Deployment Steps
Step 1 - Create an Azure Automation Account
Go to your Azure Portal > Automation Accounts > Create >
Basic tab:
Populate the subscription, resource group, unique automation account name and region fields > Next
Advanced tab:
Enable SAMI > Next,
Networking tab:
Select your level of network access,
Tags tab:
Populate the fields as required,
Review and Create,
Step 2 - Provision the SAMI
Go to your Automation Account > Identity > System Assigned tab > make sure the SAMI is ON,
Step 3 - Assign the SAMI Role
Click on Azure Role Assignments button > Add role assignment > select your scope and role based on PoLP
Step 4 - Import the Necessary PowerShell Modules
You may need to install the Connect-AzAccount module for your powershell script.
Click on Modules > Add a Module > select Browse from gallery > click here to browse from gallery > enter Connect-AzAccount >
Click on the Az.Accounts module
On the content page > populate the search bar with your required command to ensure that the command is included in the module.
Go to Automation account > Modules > search and install the required modules required by your PowerShell script >
Select > choose a runtime version that is not in preview > Import.
You can then do a search on the module name to verify its importation.
Step 5 - Create 2 Runbooks
You will be creating two runbooks. One runbook for the Start powershell script and another runbook for the STOP runbook.
Click on Runbooks > Create a runbook >
Name > provide a unique name,
Runbook type > Powershell,
Runtime version > (Ive selected a version not in preview),
Create
Step 6 - Information Gathering
1.1 Use powershell to view / collect all the high-level information about your target scale set:
#variables
$ResourceGroupName = ‘<rgname>’
$VMScaleSetName = ‘<scalesetname>’
#script
Get-AzVmss -ResourceGroupName $ResourceGroupName `
-VMScaleSetName $VMScaleSetName
1.2 To get a view of all the instances in your vm scale set:
#variables
$ResourceGroupName = ‘<rgname>’
$VMScaleSetName = ‘<scalesetname>’
#script
Get-AzVmssVM `
-InstanceView `
-ResourceGroupName $ResourceGroupName `
-VMScaleSetName $VMScaleSetName
Step 7 - Add the PowerShell START script
Expand your Runbook and find your new START Runbook >
Copy and paste your prepared PowerShell script into the window,
Add the following powershell script to your runbook:
Connect-AzAccount -Identity
Set-AzContext -Subscription ‘Your subscription name’
#variables
$ResourceGroupName = ‘<rgname>’
$VMScaleSetName = ‘<scalesetname>’
#script
Start-AzVmss `
-ResourceGroupName $ResourceGroupName `
-VMScaleSetName $VMScaleSetName `
-InstanceId “1”
#get your instance number from the instance overview page.
Paste in your powershell script,
Save,
Test Pane,
Start,
Upon a successfull run > Publish
Create a schedule for your startup runbook,
Go to Runbook > Schedule > Add a schedule > link a schedule to your runbook >
Step 8 - Creating your STOP powershell script
Expand your Runbook and find your new STOP Runbook >
Add the following powershell script to your runbook:
Connect-AzAccount -Identity
Set-AzContext -Subscription ‘<yoursubscriptionname>’
#variables
$ResourceGroupName = ‘<rgname>’
$VMScaleSetName = ‘<scalesetname>’
#script
Stop-AzVmss `
-ResourceGroupName $ResourceGroupName `
-VMScaleSetName $VMScaleSetName `
-InstanceId “1” `
-Force
Save,
Test Pane,
Start,
Upon a successfull run > Publish
Create a schedule for your startup runbook,
Go to Runbook > Schedule > Add a schedule > link a schedule to your runbook >
This will successfully allow you to schedule the start up and shutdown of your virtual machine scale set instances using user defined scheduling.
Additional helpful management scripts
Additional useful powershell commands for managing your virtual machine scale set instances:
## Get your current scale set details:
#variables
$ResourceGroupName = ‘<rgname>’
$VMScaleSetName = ‘<scalesetname>’
#script
$vmss = Get-AzVmss `
-ResourceGroupName $ResourceGroupName `
-VMScaleSetName $VMScaleSetName
##How to manually scale your scale set instances
$vmss.sku.capacity = 2
Update-AzVmss `
-ResourceGroupName $ResourceGroupName `
-Name $VMScaleSetName `
-VirtualMachineScaleSet $vmss
-Force
Admiring the time and energy you put into your website and in depth information you present. It’s great to come across a blog every once in a while that isn’t the same outdated rehashed information. Fantastic read! I’ve saved your site and I’m adding your RSS feeds to my Google account.
Really enjoyed this blog post, how can I make is so that I get an email sent to me when you publish a fresh post?