0
0
Azurecloud~5 mins

Azure Cost Analysis tool - Commands & Configuration

Choose your learning style9 modes available
Introduction
Managing cloud spending can be confusing. Azure Cost Analysis helps you see where your money goes in Azure. It shows detailed reports of your cloud costs so you can control your budget.
When you want to check how much your Azure resources cost this month.
When you need to find which service or project is using the most money.
When you want to set a budget and track if you are close to it.
When you want to see cost trends over time to plan better.
When you want to export cost data for reports or further analysis.
Commands
Log in to your Azure account to access your subscriptions and resources.
Terminal
az login
Expected OutputExpected
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code ABCD1234 to authenticate. You have logged in. Now let us find all the subscriptions to which you have access...
Run a cost query for the current month to see daily usage costs for your subscription.
Terminal
az costmanagement query --type Usage --timeframe MonthToDate --dataset-configuration '{"granularity":"Daily"}' --scope /subscriptions/00000000-0000-0000-0000-000000000000
Expected OutputExpected
{"columns":[{"name":"Date","type":"DateTime"},{"name":"Cost","type":"Number"}],"rows":[["2024-06-01",12.34],["2024-06-02",15.67],["2024-06-03",10.89]]}
--type - Specifies the type of cost data to query, here usage costs.
--timeframe - Defines the time range for the cost data.
--scope - Sets the subscription or resource group to analyze.
Create a cost export to save monthly cost data automatically to a storage account for later review.
Terminal
az costmanagement export create --name MonthlyCostReport --type Usage --timeframe MonthToDate --storage-account mycoststorage --container costreports --scope /subscriptions/00000000-0000-0000-0000-000000000000
Expected OutputExpected
{"id":"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/exports/MonthlyCostReport","name":"MonthlyCostReport","type":"Microsoft.CostManagement/exports","properties":{"exportType":"Usage","timeframe":"MonthToDate","storageAccountId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mycoststorage","container":"costreports"}}
--name - Names the export job.
--storage-account - Specifies where to save the exported cost data.
--container - Defines the storage container for the export files.
Check the details and status of the cost export you created.
Terminal
az costmanagement export show --name MonthlyCostReport --scope /subscriptions/00000000-0000-0000-0000-000000000000
Expected OutputExpected
{"name":"MonthlyCostReport","properties":{"exportType":"Usage","timeframe":"MonthToDate","status":"Succeeded","storageAccountId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mycoststorage","container":"costreports"}}
Key Concept

If you remember nothing else from this pattern, remember: Azure Cost Analysis lets you see and control your cloud spending by showing detailed cost reports and exporting data for review.

Common Mistakes
Not logging in with 'az login' before running cost commands.
Commands fail because Azure CLI needs authentication to access your data.
Always run 'az login' first and complete the sign-in process.
Using incorrect subscription ID in the --scope flag.
The command cannot find your subscription and returns an error or empty data.
Use 'az account show' to get your current subscription ID and use it exactly.
Not creating a storage account or container before setting up cost export.
Export creation fails because the destination does not exist.
Create a storage account and container in Azure portal or CLI before running export commands.
Summary
Use 'az login' to authenticate your Azure CLI session.
Run 'az costmanagement query' to get detailed cost data for your subscription.
Create cost exports with 'az costmanagement export create' to save reports automatically.
Check export status with 'az costmanagement export show' to ensure your data is saved.