question

TysonJones-1548 avatar image
0 Votes"
TysonJones-1548 asked TysonJones-1548 commented

Get Azure Batch job cost

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-managementazure-batch
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TysonJones-1548 avatar image
0 Votes"
TysonJones-1548 answered TysonJones-1548 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@shivapatpi-MSFT Can you foresee any issues with this approach?

0 Votes 0 ·
shivapatpi-MSFT avatar image
0 Votes"
shivapatpi-MSFT answered TysonJones-1548 commented

Hello @TysonJones-1548 ,
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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Shiva, thanks for your reply. We have our own custom software where users can run jobs and we're using Azure Batch as the engine for this. One of our requirements is to report how much each job cost us to run. I'm not too concerned with data transfer costs but we would definitely like to know how much it cost to run the VM/s for the duration of the job.

0 Votes 0 ·