Tuesday, 13 August 2024

How to use Cost Management + Billing to help track Azure Lab Services costs.

 How to use Cost Management + Billing to help track Azure Lab Services costs.

Cost management is one of the top concerns with education, especially with cloud resources.  No one wants to be surprised by a large bill at the end of the class session.  There are two key methods to getting a better handle on costs.  The first method is budgeting, which includes being able to set a target for the maximum cost of a lab, department, or school.  Part of the budget is to have alerts that warn the consumer before there is a problem.  The second area is analysis, once the lab has costs allocated to it the consumer will be able to review the costs to verify   that the usage was appropriate and plan for the next classes budget.

With the release of the Azure Lab Services April 2022 Update (preview), there are several additions that, used in conjunction with Azure Cost Management + Billing, can help you have better view of costs.  We’ll look at the analysis first to see the different options that are available to create a budget on.  For this example, I’ll analyze the costs for a single lab then add a budget with alerts.

Analyzing costs

To analyze cost, first open the Azure portal and select the “Cost Management + Billing” and go into “Cost Management” and then “Cost Analysis”

thumbnail image 1 of blog post titled 
	
	
	 
	
	
	
				
		
			
				
						
							How to use Cost Management + Billing to help track Azure Lab Services costs.

In this view you can see the overall costs, the forecasted cost, a budget, and the budget overage.  For more details you can check the Cost Management + Billing documentation.  For now, we will look at the ways to change the views for the AccumulatedCosts.  The first change is the date range, by default the view is set to the current month.  But classes and the corresponding labs can be weeks or months long and have start/stop dates that aren’t at the beginning or end of the month. We want to make sure we are seeing all the costs in that timeframe. So, the view can be changed using the “Custom Date Range” to include the entire time the class is running. 

Cost for multiple labs (by lab plan)

Now for this example we’ve set up the Azure lab services where each division or group has their own lab plan and every lab is used by a class in that group.   We’ll use the new tags to filter down the costs to what we want to see.  To do this select the “+filter” pill and select the “Tag” option.  A “pill” is the elongated oval shaped button, like a pill, at the upper section of the view. This will add another pill to select the tag name which is “ms-labplanid” for the lab plan.  The last pill is the lab plan id value, the id is fairly long and can be truncated in the pulldown.  If you hover over a specific option a flyout will show the entire resource id. Once you check the plan id(s) you want, the chart will change to show you all the cost for every lab in the lab plan.  This view of the costs can be saved to review at a later date without rebuilding the filter.

thumbnail image 2 of blog post titled 
	
	
	 
	
	
	
				
		
			
				
						
							How to use Cost Management + Billing to help track Azure Lab Services costs.

Cost per lab

Now that’s nice but let’s dig a little deeper to see the details for a specific lab.  We’ll do the same action of adding another tag filter, but this tag name is “ms-labname” and the value is the lab name you want the costs for.  Select the filter pill, select “tag”, select “ms-labname”, then choose the lab name you want.  The visualization changes to show the costs for just that lab. 

thumbnail image 3 of blog post titled 
	
	
	 
	
	
	
				
		
			
				
						
							How to use Cost Management + Billing to help track Azure Lab Services costs.

Cost for vms

Now the last automatic tag only pertains to labs that have a template vm.  This will allow you to filter to only show the cost of the student vms, not the template vm.  Following the same pattern with the filter to choose the “Tag”, then the “ms-istemplate” name and select value to be false.  Selecting true would only show the template vm cost.

thumbnail image 4 of blog post titled 
	
	
	 
	
	
	
				
		
			
				
						
							How to use Cost Management + Billing to help track Azure Lab Services costs.

Given the  spike of student vm usage you could infer that this was the first day of the class.  There is more detailed documentation for common cost analysis uses.

Custom Tags

If you want something with more details than the automatic tags, you can define your own custom tags at either the resource group, the lab plan or the lab level.  Any tags on the lab plan will be included in any labs created with it.  Custom tags can be added to the specific labs from the Azure Portal or programmatically.  The same filtering steps that we did with the automatic tags can be done using the custom tags.  There are some constraints on custom tags, like tags aren’t applied to historical data, which are documented in the “Understanding Cost Management Data”.

 

While the “AccumulatedCosts” view is really good for seeing cost growth and forecasting, the “CostByResource” view gives you a view into the costs per vm.  To get there in the view section change “AccumulatedCosts” to “CostByResource”, this will reset the date range and remove all the filters. Change the date range back to the same dates as the class/lab to get all the data.  You can either add a filter using the tags for the lab name or enter the lab name in the quick filter at the top of the resource list.

Cost by vm

thumbnail image 5 of blog post titled 
	
	
	 
	
	
	
				
		
			
				
						
							How to use Cost Management + Billing to help track Azure Lab Services costs.

In the view above there are costs for two vms within the lab that we are analyzing.  From the tags vm 0 is the template vm for the lab, the second is a student vm (1).  So now to find the student that is burning up money! 

The actual vm name isn’t displayed in either the Azure portal (portal.azure.com) or the Labs portal (labs.azure.com), so we’ll have to use to get the detailed vm information.  To get this setup you’ll need to:

Once PowerShell is open here are the commands to install the Az modules and get the lab vms.

 

 

 

Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
Install-Module Az.LabServices
Connect-AzAccount -Subscription <your subscription id>
$vm = Get-AzLabServicesVm -LabName <labName> -ResourceGroupName <groupName> -Name <name ie 1>
Get-AzLabServicesUser -ResourceId $vm.ClaimedByUserId | Format-List -Property Email

 

 

 

This will give you the email address of the student that is using the vm named 1 in this example.  At this point you have detailed information on what the cost is for a specific vm, and the student that the vm is assigned to.  Now that we have lab costs down to the individual vm, let’s take a look at budgeting.

Lab Budgeting

So, let’s set up a budget for a lab using the same tags that will send an email alert when costs reach 50%, 75%, and 90% of the budget and when the cost exceeds the budget.

The first item is to create a budget so in the Azure Portal open the “Cost Management + Billing” and go into “Cost Management” and then “Budget”. 

thumbnail image 6 of blog post titled 
	
	
	 
	
	
	
				
		
			
				
						
							How to use Cost Management + Billing to help track Azure Lab Services costs.

We’ll select “ + Add” to create a new budget.  In the “Create budget” screen we’ll use the filters to select the lab specific tag.  So, select the “Add filter” pill, select “Tag”, then “ms-labname”, and for the value the lab name.  Set the name to identify the budget and set the “Reset Period” to monthly.  The Creation Date and Expiration date should be the same as the lab.  Then set the overall lab budget. 

thumbnail image 7 of blog post titled 
	
	
	 
	
	
	
				
		
			
				
						
							How to use Cost Management + Billing to help track Azure Lab Services costs.

Calculating Budget amount

Budgets are reset on Monthly, Quarterly, or Annually basis.  The issue is that the lab costs that are displayed in the lab website (https://labs.azure.com) are for the entire time of the class, which could be for multiple months.  The simple solution is to set the budget to be the displayed lab cost divided by the number of months.

Set Alerts

Select “Next” to move to the “Set alerts” page where you can set conditions to send out emails when lab costs reach key percentages.  I would recommend that you set up a few alerts to give you early warnings.  Below I’ve set up alerts at 50%, 75%, and 90% based on actual usage. Then add in the emails to the appropriate people. 

When the lab costs reach the specific percentage of actual cost you’ll receive an email from “azure-noreply@microsoft.com” that will list out the details of the budget, the alert, the cost, and other details.

thumbnail image 8 of blog post titled 
	
	
	 
	
	
	
				
		
			
				
						
							How to use Cost Management + Billing to help track Azure Lab Services costs.

Now you have a budget specific to a lab that will alert people when costs reach specific cost levels.  This is a sample to get you up and running with cost management focused on Azure Lab Services.

No comments:

Post a Comment