Tag Azure Database for Costing

Kman 41 Reputation points
2020-07-14T07:12:07.72+00:00

Hi There,

I am planning to have several databases in one instance of Azure SQL Database and I need to recharge the cost of each database. I have a database per tenant model. Is there a way to tag each database within Azure SQL Database to capture the cost per database?

Thank You.

Azure SQL Database
Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
2,285 questions
{count} votes

1 answer

Sort by: Most helpful
  1. KalyanChanumolu-MSFT 8,316 Reputation points
    2020-07-14T10:23:54.547+00:00

    @KaisMalique-9406 Thank you for reaching out.

    Yes, you will be able to retreive the costs for each Azure SQL Database. You can also choose to tag them individually for convinience.

    From your Azure Portal, please navigate to Cost Management.
    Click on the Cost Analysis blade and filter by the Tag and/or Resource Type for any particular billing period

    You will be able to see the resource cost per database on the server and also export them to Excel by clicking on the Download button.
    In this case, I have 2 Azure SQL Databases, 1 Datawarehouse and 1 Elastic pool all running on the same Server and I can retreive the costs separately.

    12134-image.png

    If you want to get these details programatically, you can use the Azure CostManagement Query - Usage API and fetch these details.

    URI: https://management.azure.com/subscriptions/YOUR-SUBSCRIPTION-ID/providers/Microsoft.CostManagement/query?api-version=2019-11-01

    RequestBody for Post call:
    Modify the filters and timeperiod as per your needs

     {"type":"ActualCost","dataSet":{"granularity":"None","aggregation":{"totalCost":{"name":"PreTaxCost","function":"Sum"}},"grouping":[{"type":"Dimension","name":"ResourceId"}]},"timeframe":"Custom","timePeriod":{"from":"2020-7-1T00:00:00+00:00","to":"2020-7-31T00:00:00+00:00"}}
    

    Results:

     {
       "id": "subscriptions/*******-****-****-****-******/providers/Microsoft.CostManagement/query/*******-****-****-****-******",
       "name": "*******-****-****-****-******",
       "type": "Microsoft.CostManagement/query",
       "location": null,
       "sku": null,
       "eTag": null,
       "properties": {
         "nextLink": "https://management.azure.com/subscriptions/*******-****-****-****-******/providers/Microsoft.CostManagement/query?api-version=2019-10-01&$top=1000&$skiptoken=AQAAAA%3D%3D",
         "columns": [
           {
             "name": "PreTaxCost",
             "type": "Number"
           },
           {
             "name": "ResourceId",
             "type": "String"
           },
           {
             "name": "Currency",
             "type": "String"
           }
         ],
         "rows": [
           [
             28.100787,
             "/subscriptions/*******-****-****-****-******/resourcegroups/myteam-resources/providers/microsoft.sql/servers/sqlserverleon/databases/db2",
             "USD"
           ],
           [
             37.771289,
             "/subscriptions/*******-****-****-****-******/resourcegroups/myteam-resources/providers/microsoft.sql/servers/sqlserverleon/databases/leonpool",
             "USD"
           ],
           [
             1.19945,
             "/subscriptions/*******-****-****-****-******/resourcegroups/myteam-resources/providers/microsoft.sql/servers/sqlserverleon/databases/mydatabase",
             "USD"
           ],
           [
             3.79819,
             "/subscriptions/*******-****-****-****-******/resourcegroups/myteam-resources/providers/microsoft.sql/servers/sqlserverleon/databases/mydatabase1",
             "USD"
           ]
         ]
       }
     }
    

    I hope this helps. Please let us know if you have further questions


    If an Answer is helpful, please “Accept Answer” or Up-Vote for the same which might be beneficial to other community members reading this thread.

    2 people found this answer helpful.