Customize cost data visualizations with Azure Workbooks and Azure Cost Management

This post has been republished via RSS; it originally appeared at: ITOps Talk Blog articles.

Azure Workbooks have become a standard data visualization tool inside Azure. After starting in Azure Monitor, they now are also in Azure Sentinel, and Azure Security Center. Workbooks are incredibly dynamic, allowing you to create parameters that can be used throughout the workbook to both control what is seen by a user as well as inside queries. There are many things you can query from logs and metrics to Azure Resource Graph to Azure Resource Manager(ARM). This post will show you how to query Cost Management by using the ARM data source. Then we will make our query to ARM dynamic, because that is what sets Workbooks apart from a regular old dashboard.

 

Why might you want to query Cost Management from Workbooks? Well first because we can do parameters we can set a top level Subscription parameter and quickly get costs for all subscriptions. But we can also use tags as I describe in the dynamic monitoring post linked to above, we can show costs for infrastructure based on tag, or resource type right along side with operational monitoring data.

 

Query Azure Cost Management

Like pretty much everything now, it all comes back to an API. Naturally, Cost Management has an API that we can query. To query Cost Management in Workbooks we'll need the URL example from the docs, and a subscription id.

 

In this screen grab I've already setup a Subscription parameter, we'll want to do a new query, then select Azure Resource Manager as our data source. The HTTP Method is POST, and finally the path is /subscriptions/{Subscription:id}/providers/Microsoft.CostManagement/query?

 

Where {Subscription:id} is from our subscription parameter.

 

workbooks-costmanagement-query.png

 You'll also need to declare the api version you are using, in this case 2019-11-01 is what we're using.

Now that we have this setup, we need a JSON body to POST to the API to tell Cost Management what information we want back. There are a number of examples on the API docs linked to above, I also recently wrote about JSON body examples here, with some more straight forward examples than what is given on the docs.

 

I'll be using a simple body that brings me back my month to date cost, by subscription.

workbooks-costmanagement-jsonbody.png

 

JSON Path

The result you'll get back is JSON, so if we want this in a more readable format we'll need to use JSON Path. Under Results Settings, change Result Format to JSON Path. Then put $.properties in JSON Path Table.

workbooks-costmanagement-jsonpath.png

  Now this will bring back our data in a slightly prettier format than JSON.

 

Make it Dynamic!

Now that we've successfully queried Cost Management and formatted the results. Its time to put the power of Workbooks to use. We'll add two parameters, one for our Environment tag, IE Prod, Non Prod, QA etc. And one for Aggregation type, where we specify how we want the cost to be aggregated. 

 

workbooks-costmanagement-tag.png

This Environment parameter uses Azure Resource Graph to query all resources that have been tagged with Demo_Environment, where we have Dev, QA and Prod as options.

 

 

workbooks-costmanagement-aggregation.png

 

Aggregation is a simple JSON drop down where we manually specify the options we want to be available in the drop down.

  

Once we have our parameters setup, we can then add them into the JSON body. For Demo Environment we have added a Filter for Tags. We have placed Aggregation at the bottom under Grouping.

{ "type": "Usage", "timeframe": "MonthToDate", "dataset": { "granularity": "None", "filter": { "tags" : { "name" : "Demo_Environment", "operator" : "In", "values" : [ {Environment} ] } }, "aggregation": { "totalCost": { "name": "PreTaxCost", "function": "Sum" } }, "grouping": [ { "type": "Dimension", "name": "{Aggregation}" } ] } }
This allows us to aggregate based on Resource Group, Subscription or even Resource Type, while selecting any individual tag or all 3 tags at the same time. This will then be translated by the workbook and sent to the Cost Management API.
 
Cost by subscription for Prod, QA and Dev
workbooks-costmanagement-costbysub.png
 
 Cost by resource type for Prod, QA and Dev
workbooks-costmanagement-costbytype.png
 
Cost by Resource Group for Prod
workbooks-costmanagement-rgprod.png
 
You can add more filters for other tags or even resource types in the JSON Body. Almost everything in the body can be parameterized. For instance, you can make a TimeFrame parameter to bring back cost for a specific cycle, like Week to date, the Last Billing cycle etc.
workbooks-costmanagement-timeframe.png
 
workbooks-costmanagement-lastbillingmonth.png
 
The only thing we can't make truly dynamic is querying subscription, because were sending a request to the API it can only be for one subscription at a time.
 
This workbook is on my github, so you don't have to recreate the wheel.
 
 

Summary

With the ability to query a number of different sources, Workbooks allow you to truly customize your data visualization across your Azure environment.  The ability to show Cost Management along side your resources and slice and dice it anyway you want or need is what Azure Workbooks are all about!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.