Introduction:
Azure Virtual Machines provide flexibility and scalability for managing workloads in businesses. However, many times the requirement on the exact control over when these virtual machines are running becomes a must for cost management and resource efficiency. One can automate the start and stop processes of Azure Virtual Machine to reduce manual intervention, optimize efficiency, and ensure resource utilization.
In this article, we will discuss how to use Azure Automation-a strong tool that simplifies operational tasks-to automatically start and stop Azure Virtual Machines.
Why Automate the Start and Stop of VMs?
- Cost Optimization: Stopping VMs during non-business hours or when not in use can significantly reduce Azure costs.
- Operational Efficiency: Automation eliminates the need for manual intervention, reducing the risk of errors and saving time.
- Resource Optimization: Ensures resources are available when needed and conserved during downtime.
How do we accomplish this?
Tools and Prerequisites
To automate the start and stop of Azure VMs, you’ll need the following:
- Azure Automation Account: A service that allows you to run PowerShell scripts and workflows.
- Azure PowerShell Modules: Ensure the Azure PowerShell module is installed and up to date.
- Service Principal: A service principal with appropriate permissions to manage VMs.
- Resource Groups and VM Names: The list of VMs you want to automate.
Steps to Automate VM Start and Stop
Step 1: Create an Azure Automation Account
- Log in to the Azure Portal.
- Navigate to Automation Accounts and click Create.
- Provide the necessary details, such as name, subscription, and resource group, and click Create.
Step 2: Add a Runbook for Automation
- In the Automation Account, navigate to Runbooks and click Create a Runbook.
- Provide a name, select PowerShell as the Runbook type, and click Create.
- Open the newly created Runbook and paste the following script:
# Variables
$subscriptionId = “<Your-Subscription-ID>”
$resourceGroup = “<Your-Resource-Group-Name>”
$vmName = “<Your-VM-Name>”
# Login to Azure
Connect-AzAccount -Identity
# Select Subscription
Set-AzContext –SubscriptionId $subscriptionId
# Start the VM
Start-AzVM –ResourceGroupName $resourceGroup -Name $vmName
(OR)
# Stop the VM
Stop-AzVM –ResourceGroupName $resourceGroup -Name $vmName -Force
- Replace the placeholders with your subscription ID, resource group, and VM name.
- Save and publish the Runbook.
Step 3: Configure Schedules for Automation
- Go to the Runbook and navigate to the Schedules tab.
- Click Add a Schedule and create a new schedule (e.g., start at 8:00 AM and stop at 8:00 PM).
- Link the schedule to the Runbook and specify parameters, if needed.
Step 4: Assign Permissions to the Runbook
- Navigate to Azure Active Directory > App Registrations and locate the service principal linked to the Automation Account.
- Assign the required roles (e.g., Virtual Machine Contributor) to the service principal at the subscription or resource group level.
Step 5: Test the Automation
- Manually trigger the Runbook to ensure it successfully starts and stops the VM.
- Check the activity logs in Azure Monitor to verify execution.
Benefits of Automation
- Time Savings: Automating repetitive tasks like starting and stopping VMs frees up valuable time for other activities.
- Error Reduction: Scripts and schedules ensure consistency and accuracy in operations.
- Cost Control: Ensures VMs are only running when required, reducing unnecessary expenses.
Conclusion
Automating the start and stop of Azure Virtual Machines is a practical solution for businesses looking to optimize resource usage and minimize costs. With Azure Automation and PowerShell, you can create reliable, repeatable workflows that simplify VM management. Implementing such automation not only saves time but also ensures that your Azure resources are used efficiently, aligning with your organizational goals.