Budget Actions#
Cost control is a critical component to maximizing the value of your investment in the cloud. To configure a Budget and Budget actions in Azure, you’ll complete the following actions by using the steps provided in each section. This document specifically outlines an example of how to setup a budget action to shutdown virtual machine when it meets the configured alert condition.
These actions included in this tutorial allow you to:
1. Create an Azure Automation Account and import relevant Runbook, which will use webhooks to stop VMs.
2. If needed, add role assignment to provide relevant access to the Automation account to perform operations on virtual machines.
3. If haven't already, create the budget with the wanted thresholds and wire it to the action group.
4. Azure Monitor Action Group will be configured to trigger the webhook when the budget threshold is met. This behaviour can be tested using 'Test action group'.
Creating Azure Automation Account#
Sign in to the Azure Management Console and search for Automation accounts on the top search bar. Click Create and choose the subscription that you intent to manage, A Resource Group to place the Automation account, Name and Region to host Automation account.
Leave all other parameters to default. You may optionally choose to configure Tags as per your compliance standards. Click Review+Create and then click on Create.
After the deployment finishes, open the new Automation account. The Overview page shows resource details such as resource group, location, subscription, and status.
Once you are in your Automation Account, Click on Runbooks and click on Browse Gallery from the top pane.
Search for Stop Azure V2 VMs from the search bar. Make sure to choose source as Github. Click on Stop Azure V2 VMs graphical runbook.
Review the graphical flow to understand how this runbook will excute to stop all VMs at subscription or resource group level and then click on Select at the bottom.
Name the runbook (for example,
Stop-VMs) and select Import.
In the Edit graphical runbook pane, select Publish, then confirm by selecting Yes.
Add VM Contributor Role Assignment#
To check access level of the Automation account that you created, go to Subscription using top search bar and click on your subscription name. Then click on Access control (IAM) >> choose check access from top pane >> click on check access
Choose Managed Identity and choose your subscription. Under ‘Managed Indentity’ choose Automation Accounts and select your automation account. This will show you manually or auto assigned Role assignments for this automation account (if any).
In case you don’t see any Role assignments, Click on Add in Access Control (IAM) blade of your subscription and choose Add role assignment. In Role >> Job function roles >> search for virtual machine contributor and click on it. Then click Next to assign this role to our automation account.
Choose Managed Identity radio button, click on select members to select your automation account. Once your clicked on select, click on Review+Assign.
Create Budget and Action Group#
Open your subscription and, under Cost Management, select Budgets. Select Add to create a new budget.
Enter a budget name and choose a Reset period (this example uses monthly). You can leave the creation and expiration dates at their defaults. Enter the total budget Amount, then select Next.
In Set alerts tab, click on Manage action group. Click Create to create a new action group.
On the Basics tab, choose the subscription and resource group, set Region to Global, and enter an action group name and a display name (the display name is limited to 12 characters). Select Next: Notifications.
Under Notification type, select Email/SMS message/Push/Voice, choose the notification method you want (this example uses email), enter the recipient address, then select OK. Name the notification, then select Next: Actions.
Under Actions, set Action type to Automation Runbook. Configure the runbook as follows:
Run runbook: Enabled
Runbook source: User
Subscription: the subscription you are managing
Automation account: the Automation account you created
Runbook: the imported Stop VMs runbook
Select Configure parameters. Subscription is the only required parameter. Leave resource group blank to apply the action at subscription scope, or enter a resource group name to limit the action to that group. Select OK as needed, name the action, then select Review + create and Create.
Once you are back on Action Groups page, give it 10-20 seconds for the new Action group to show up. Click on Refresh if needed. Once you see your Action group, carefully click on Red X at the top right corner, only Once, to come back to Create Budget pane.
After the action group is created, Azure sends a confirmation email to the notification recipient. The message is from
azure-noreply@microsoft.comand states that the recipient has been added to the action group. It includes the resource group name and action group name.
Note
Add azure-noreply@microsoft.com to your email allow list so budget and action-group messages are not delivered to spam.
On Create Budget pane, configure alert conditions as needed, I chose to not perform any action for 80% and enforce budget action when my budget hits 100% utilization, in this example. Make sure to select the action group which you have created in previous step. Click Create in the bottom, once you have filled all relevant fields as per below screenshot. In the next section, we will test this action group if it really works.
Test Azure Monitor Action Group#
Warning
Testing an action group that is configured to stop VMs will stop those VMs. Use a test resource group, and make sure the runbook webhook is scoped to that resource group if you do not want subscription-wide impact.
Search for Monitor in the top search bar. Open Monitor, select Alerts, then select Action groups in the toolbar.
In the Action groups list, select the checkbox next to the action group you created, then select Test action group.
Leave the default notification and action tests selected. For Select sample type, choose Cost budget alert. This sends a test email and fires the webhook to stop VMs in the configured subscription or resource group.
When the test finishes, the portal shows a success or failure status. Open Virtual machines to confirm that the test VMs are in a deallocated state.
If the email notification test succeeds, you receive a message confirming that a test alert was sent for the action group.