azure-arm-consumption: get consumption of a resource group

Viewed 803

In my Node.js project I am trying to use azure-arm-consumption package to get the current consumption/billing of a resource group. I mean, how much money was spent on this resource group until now.

Under Interfaces, from AggregatedCost to UsageDetails, all of these interfaces contain methods, but I just don't manage to find the method for reading how much money a specific resource group has consumed.

My code:

const MsRest = require('ms-rest-azure');
const credentials = MsRest.loginWithServicePrincipalSecret(keys.appId, keys.pass, keys.tenantId);
const { ConsumptionManagementClient } = require('azure-arm-consumption');
const client = new ConsumptionManagementClient (credentials, subscriptionId);
const cost = client.forecasts.list(subscriptionId);

It retrieves the consumption of my subscription divided by date. Now the problem is that I don't want it to be divided by date, but by resource group. Is there any method in this API that can do that?

2 Answers

Accoring the documentation, there is no available method/endpoint for getting data by resource group. You have to download all data and process them by yourself.

Here is script that will download data using REST (using ConsumptionManagementClient will be almost the same) and sum usage by resource group. Take it as starting point.

const axios = require('axios');

function processUsage(data) {
  let usage = [];

  for (index in data.value) {
    const properties = data.value[index].properties;
    const cost = properties.pretaxCost;

    let resourceGroup = properties.instanceId.replace(`/subscriptions/${properties.subscriptionGuid}/resourceGroups/`, "");
    resourceGroup = resourceGroup.substring(0, resourceGroup.indexOf("/"));

    let foundUsage = usage.filter(x => x.rg === resourceGroup);
    if (foundUsage.length > 0) {
      foundUsage[0].cost += cost
    } else {
      usage.push({
        rg: resourceGroup,
        cost: cost
      });
    }
  }

  usage.map(x => console.log(`${x.rg} | ${x.cost}`));

  // Example result display list of resource groups and costs in EUR (in my case)
  // myresourcegroup | 1.2234748382055878
  // DevTestLab | 0.0004314997440000002
}

function getUsage(subscriptionId, accessToken) {
  const url = `https://management.azure.com/subscriptions/${subscriptionId}/providers/Microsoft.Consumption/usageDetails?api-version=2018-10-01`

  const options = {
    headers: {
      Authorization: `Bearer ${accessToken}`
    }
  }
  axios.get(url, options).then(response => {
    processUsage(response.data);
  }).catch(error => {
    console.log(error);
  });
}

getUsage(
  "subscription id",
  "access token"
);

Reference:
https://docs.microsoft.com/en-us/rest/api/consumption/usagedetails https://docs.microsoft.com/en-us/javascript/api/azure-arm-consumption/usagedetails#list-object-
https://docs.microsoft.com/en-us/javascript/api/azure-arm-consumption/usagedetail
https://azure.microsoft.com/cs-cz/blog/azure-consumption-usage-details-api/

Related