Get Azure Batch job cost

Tyson Jones 101 Reputation points
2021-04-09T00:25:32.33+00:00

How can I programmatically retrieve the cost of a completed Azure Batch job? I'm interested in how much the job cost to run based on total run time within a given pool. Thanks.

Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
2,287 questions
Azure Batch
Azure Batch
An Azure service that provides cloud-scale job scheduling and compute management.
320 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tyson Jones 101 Reputation points
    2021-04-11T23:51:49.217+00:00

    I ended up querying the cost management API using a resource ID filter to retrieve the information I need. This works for us because we use auto pool specifications for our batch jobs and we name the pools using a convention (so we know the pool ID based on the job ID). Here's what I'm doing if anybody needs to do the same.

    POST https://management.azure.com/subscriptions/{subscription_id}/providers/Microsoft.CostManagement/query?api-version=2019-11-01

    {
        "type": "ActualCost",
        "dataSet": {
            "granularity": "None",
            "aggregation": {
                "totalCost": {
                    "name": "Cost",
                    "function": "Sum"
                }
            },
            "grouping": [
                {
                    "type": "Dimension",
                    "name": "ResourceId"
                }
            ],
            "filter": {
                "Dimensions": {
                    "Name": "ResourceId",
                    "Operator": "In",
                    "Values": [
                        "/subscriptions/{subscription_id}/resourcegroups/{resource_group}/providers/microsoft.batch/batchaccounts/{batch_account}/pools/{pool_id}"
                    ]
                }
            }
        }
    }
    

1 additional answer

Sort by: Most helpful
  1. shiva patpi 13,161 Reputation points Microsoft Employee
    2021-04-10T05:48:30.03+00:00

    Hello @Tyson Jones ,
    Thanks for your query ! Not sure what is the reason for programmatic approach for retrieving the cost , because Azure Portal it self provides couple of resources through which we can find the cost of the batch job in a given pool.
    Basically cost of Batch Job depends on multiple parameters like VM Size , Storage Transactions , etc. and that cost can be optimized by running Low-Priority VMs.

    Reference :-

    You can try using the API Get-AzureRmConsumptionUsageDetail by passing the reference
    Example: Get-AzureRmConsumptionUsageDetail -BillingPeriodName 201710 -InstanceName <nameofinstance> -Top 10

    Couple of REST APIs:

    Regards,
    Shiva.