Automation Account - How to start/stop a virtual machine scale set instance

Automation Account - How to start/stop a virtual machine scale set instance

There are 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 using the auto-shutdown feature or automation tasks. You also have the option to configure a start/stop plan available in the marketplace for a scalable solution across multiple availability zones, subscriptions, and resource groups. However, none of these solutions can manage 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 lack Automation Tasks, and the start/stop plan does not work on them. Therefore, I configured an Azure Automation Runbook specifically for virtual machine scale set instances.

Deployment 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 successful 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 successful 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 startup 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        

--This walkthru will provision an automated snoozing schedule of your virtual machine scale sets--


















要查看或添加评论,请登录

Allen Visser的更多文章

社区洞察

其他会员也浏览了