Start/Stop VMs for Cloud-hosted Environments
Cloud-hosted Environments
Either Demo VMs you spin up or one-boxes you are working on may need automatic start/stop periodically. In most cases, this is needed on a daily basis. These environments are called as cloud-hosted environments and cost model for these machines as pay-as-you-go means you are charged based on time usage.
You begin the day. Many development tasks are awaiting. You open up a new RDP window and try to login but failed. Virtual machine is stopped. If you are working for a partner, you must find the exact VM information through LCS or Azure, assuming you have access to LCS project. If you don’t have access, you must get in touch with someone that has access and wait for him/her to start the VM for you. Let’s assume you are working for a customer as an in-house resource, this is still a mandatory task every morning that someone should do.
Configure Auto-shutdown for Workshops Lab VM
You can configure the auto-shutdown feature to shut down your VM each evening. This will help conserve your Azure credits.
Steps
- Open a web browser such as Google Chrome, Microsoft Edge or Internet Explorer from the Start menu. To avoid signing in automatically with your default company/personal credentials, use the InPrivate/Incognito mode when opening a new browser session.
- Navigate to Lifecycle Services at https://lcs.dynamics.com and sign in using the OnMicrosoft.com Account.
- Click on the project linked to your VM.
- On the main project page under the Environments heading, check to make sure your environment status shows as Deployed. If you see a status of Queued or Deploying, the VM deployment is not yet complete and auto-shutdown should not be configured.
- Open a new browser session in the InPrivate/Incognito mode using a browser such as Google Chrome, Microsoft Edge or Internet Explorer.
- Navigate to the Microsoft Azure Portal at https://portal.azure.com and sign in using your com Account.
- Click on Virtual machines then click on your VM name. Under the Operations heading, click Auto-shutdown. In the auto-shutdown properties, set Enabled to On and the scheduled shutdown time to 7:00pm for example. Then click
Schedule the Start and Stop of Your Cloud-hosted Environments
The Start/Stop VMs during off-hours solution starts and stops your Azure Virtual Machines on a schedule or by utilization. Save money by making sure VMs are off when not being used.
The following are limitations with the current feature at this moment:
- It manages VMs in any region, but can only be used in the same subscription as your Azure Automation account.
- It is available in Azure and Azure Government for any region that supports a Log Analytics workspace, an Azure Automation account, and alerts. Azure Government regions currently don't support email functionality.
You can implement that by using an Azure Automation account and an automatic startup/shutdown solution. In this topic, we will see how to accomplish that.
Requirements
The Start/Stop VMs during off-hours solution relies on three Azure services:
- Automation: starts and stops your virtual machines on a schedule
- Log Analytics: visualizes the successful start and stop of your machines
- Monitor: alerts and email notification for VM state changes
You will be charged based on the pricing of the services above. Note that the logs for all runbooks jobs in the selected Automation account will be sent to Log Analytics as part of this solution.
If you use this solution, it will only use the automation job minutes and log ingestion. That is, this solution does not add additional OMS nodes to your environment.
How to implement the solution
- Sign in to the Azure portal.
- From Azure Marketplace, search for Start/Stop VMs during off-hours template and click on it.
- In Detail screen, press Create button
- Now it is time to create needed Azure services. First, you must create a new OMS workspace or use existing one under your subscription.
- Let’s assume you create a new one. Press “Create a new OMS workspace, or c…” and then on the next screen, select Create New Workspace
- On next screen, proceed with Create New option, give a meaningful name to the Log Analytics Workspace, create new Resource Group if needed, select a Location, Subscription and Pricing Tier
- After you click OK, wait for the deployment to finish.
- Now it is time to create an Automation account:
- Select Create an Automation account
- Next click on Create and Automation account.
- Fill in the Name and click OK.
- Wait for the deployment process
- Finally, set Configuration parameters
- On the next screen, fill fields with necessary information and then click OK
Target ResourceGroup names (string)
Enter Resource Group names that contains VMs to be managed by this solution. Separate names by using comma (,). Values are case-sensitive. By default, all resource groups in the subscription will be included. Be careful, all VMs in this resource group will be scheduled to shut down.
VM Exclude List (string)
Enter VM names to be excluded from management operation. Separate names by using comma (,). Values are case-sensitive. Specify the VMs you don’t want in this scheduling.
Schedule
Enter a time for starting and stopping VMs by resource group. This is a recurring daily schedule. Set a datetime (current time zone) that's at least 30 minutes from now. Schedule will be reflected as UTC in the automation account.
Email functionality
Azure Monitor features are used to send email notifications. If you choose Yes, specify an E-mail to create an alert (in Azure Monitor) and send a notification when the VMs are stopped and started. You can disable later.
- Now all setup is done. Click Create
- Wait for deployment. It might take a while.
- After deployment is completed, you will see screen as below:
When Runbook Triggered
When runbook runs, you’ll receive notification mails like below:
Here is the log for job executions under runbook:
Thank you for reserving you valuable time and read this blog. I hope you find this blog post helpful and please feel free to share in your social media accounts.
Acknowledgments: Kudos to my Premier Field Engineer colleagues Amy Flowers and José Antonio Estevan for their contributions and peer review.
*This post is locked for comments