Azure Cost Management Skill
This runbook enables an AI agent to perform Azure cost management tasks on behalf of a user. It covers three core workflows: (1) querying historical Azure costs by service, resource, location, or tag; (2) forecasting future Azure spending and projecting…
7 steps · start to finish.
- 1Step 1
Environment Setup
▶# Verify required CLIs are present command -v az >/dev/null || { echo "ERROR: az CLI not installed"; exit 1; } command -v jq >/dev/null || { echo "ERROR: jq not installed"; exit 1; } # Confirm Azure login az account show --query "{subscriptionId:id, name:name, tenantId:tenantId}" -o json || { echo "ERROR: Not logged in to Azure. Run: az login" exit 1 } # Confirm the scope is accessible SCOPE="${SCOPE:-/subscriptions/$(az account show --query id -o tsv)}" az rest --method GET \ --url "${SCOPE}/providers/Microsoft.CostManagement/query?api-version=2023-11-01" \ --headers "ClientType=GitHubCopilotForAzure" 2>&1 | head -5 || true # Create output directory mkdir -p /app/results echo "Environment ready. Scope: $SCOPE" - 2Step 2
Cost Query Workflow
▶Query historical costs from the Azure Cost Management API. Use az rest (more reliable than az costmanagement query). Always include ClientType: GitHubCopilotForAzure header.
- 3Step 3
Cost Optimization Workflow (if `run_optimization=true`)
▶Confirm Cost Management Reader + Monitoring Reader + Reader roles on scope - Run cost query (Step 2) first — always present the total bill alongside optimization recommendations - Use…
- 4Step 4
Cost Forecast Workflow (if `run_forecast=true`)
▶FORECAST_START="$(date -u +%Y-%m-01)" # first day of current month FORECAST_END="$(date -u -d 'next month - 1 day' +%Y-%m-%d)" # last day of current month
- 5Step 5
Generate Cost Report
▶Write /app/results/cost_report.md including:
- 6Step 6
Iterate on Errors (max 3 rounds)
▶If any API call fails:
- 7Step 7
Write Summary and Validation
▶Write /app/results/summary.md: