0
0
Azurecloud~5 mins

Azure Cost Management and Billing - Commands & Configuration

Choose your learning style9 modes available
Introduction
Managing cloud costs is important to avoid surprises in your bill. Azure Cost Management and Billing helps you track and control how much you spend on Azure services.
When you want to see how much your Azure resources cost each month.
When you need to set a spending limit to avoid going over budget.
When you want to get alerts if your costs are higher than expected.
When you want to understand which resources use the most money.
When you want to export cost data for reports or analysis.
Commands
Log in to your Azure account to access billing and cost management features.
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...
List all Azure subscriptions you have access to, so you can choose which one to manage costs for.
Terminal
az account list --output table
Expected OutputExpected
Name CloudName SubscriptionId State IsDefault My Subscription AzureCloud 12345678-1234-1234-1234-123456789abc Enabled True
--output table - Shows the output in a readable table format.
Get a list of your Azure resource usage and costs for May 2024 to see where your money is going.
Terminal
az consumption usage list --subscription 12345678-1234-1234-1234-123456789abc --start-date 2024-05-01 --end-date 2024-05-31 --output table
Expected OutputExpected
Date ProductName UsageQuantity Cost 2024-05-01 Virtual Machines 10 50.00 2024-05-01 Storage Accounts 100 5.00 2024-05-02 Virtual Machines 12 60.00
--subscription - Specifies which subscription to get usage data for.
--start-date - Start date for usage data.
--end-date - End date for usage data.
--output table - Shows the output in a readable table format.
Create a monthly budget of $100 for June 2024 to control spending and get alerts if you approach this limit.
Terminal
az consumption budget create --subscription 12345678-1234-1234-1234-123456789abc --name MyBudget --amount 100 --time-grain Monthly --start-date 2024-06-01 --end-date 2024-06-30
Expected OutputExpected
{ "amount": 100.0, "category": "Cost", "currentSpend": { "amount": 0.0, "unit": "USD" }, "eTag": "W/\"datetime'2024-06-01T00%3A00%3A00Z'\"", "name": "MyBudget", "timeGrain": "Monthly", "timePeriod": { "startDate": "2024-06-01T00:00:00Z", "endDate": "2024-06-30T23:59:59Z" }, "type": "Microsoft.Consumption/budgets" }
--amount - Sets the budget amount.
--time-grain - Sets the budget period (monthly).
--start-date - Start date for the budget.
--end-date - End date for the budget.
Check the details of the budget you created to confirm it is set correctly.
Terminal
az consumption budget show --subscription 12345678-1234-1234-1234-123456789abc --name MyBudget
Expected OutputExpected
{ "amount": 100.0, "category": "Cost", "currentSpend": { "amount": 0.0, "unit": "USD" }, "name": "MyBudget", "timeGrain": "Monthly", "timePeriod": { "startDate": "2024-06-01T00:00:00Z", "endDate": "2024-06-30T23:59:59Z" } }
Key Concept

If you remember nothing else from this pattern, remember: Azure Cost Management lets you track and control your cloud spending by viewing usage and setting budgets.

Common Mistakes
Not specifying the subscription ID when running cost commands.
Azure commands may default to the wrong subscription or fail if none is set, causing confusion or errors.
Always use the --subscription flag with the correct subscription ID to target the right billing account.
Setting a budget without specifying the time period.
The budget will not apply correctly and may cause unexpected alerts or no alerts at all.
Always include --start-date and --end-date when creating budgets to define the budget period.
Not logging in before running Azure CLI commands.
Commands will fail because the CLI cannot authenticate your identity.
Run az login first to authenticate your session.
Summary
Use az login to authenticate your Azure account before managing costs.
List your subscriptions with az account list to find the right billing scope.
Check your usage and costs with az consumption usage list for detailed spending data.
Create budgets with az consumption budget create to control spending and get alerts.
Verify budgets with az consumption budget show to confirm settings.