Cloud computing gives companies access to cutting-edge services, flexibility, and scalability. Effective cost management, however, is one of the main challenges to cloud adoption. Despite Azure’s robust offerings, businesses risk unexpectedly high costs if they don’t have active monitoring.
Image generated by Gemini
This is where cost monitoring and automated notifications come into play. By proactively tracking cloud expenses and sending cost summaries to communication platforms like Slack, teams can take immediate action, enforce budgets, and maintain financial governance.
In this article, we’ll walk through building a cost notifier system that fetches Azure subscription costs, breaks them down by service, and posts reports to Slack automatically. Interestingly, we’ll deploy this automation not in Azure Functions but in AWS Lambda, showing how cloud services can interoperate. The same logic, however, can be applied in Azure Functions if preferred.
Why Is Azure Cost Monitoring Important?
Azure pricing follows a pay-as-you-go model. While this flexibility allows organizations to scale quickly, it also means costs can accumulate without notice. Examples include:
-
Overprovisioned resources – Virtual Machines left running when not needed.
-
Data egress costs – Large amounts of data are being transferred outside Azure.
-
Unused services – Subscriptions consuming resources that no one actively monitors.
Without visibility, these small oversights can balloon into significant expenses. Cost monitoring:
-
Improves financial accountability – IT and finance teams know exactly where money is being spent.
-
Supports decision-making – Leaders can prioritize which workloads should scale and which should be optimized.
-
Prevents waste – Alerts help detect unused or underutilized resources.
-
Increases transparency – Developers, DevOps, and finance teams can all stay on the same page.
By setting up a notifier, your team will receive timely, automated updates that summarize total monthly costs and the top cost-driving services.
Solution Overview
The notifier system is built around three key components:
-
Azure Cost Management API – Provides detailed cost and usage information at the subscription level.
-
Authentication via Microsoft Entra ID – Uses a registered app and service principal for secure API access.
-
AWS Lambda Function – Runs Python code to fetch costs, process results, and send notifications to Slack.
- Amazon EventBridge – Triggers the Lambda function daily at 9:00 AM UTC+8. The EventBridge rule uses a cron expression to invoke the Lambda function without manual intervention.
Step 1: Obtaining Necessary IDs and API Keys in the Azure Portal
To query Azure’s Cost Management APIs, you must authenticate using Microsoft Entra ID (formerly Azure Active Directory). This involves setting up an application registration and granting it permissions to access subscription cost data.
Get the Tenant ID
-
Navigate to Microsoft Entra ID → Overview.
-
Note the Tenant ID (used for API authentication).
Register an App
-
Go to Microsoft Entra ID → App registrations → New registration.
-
Configure:
-
Name:
azure-cost-notifier
-
Supported account types: Single tenant (default)
-
-
Click Register.
-
Save the Application (client) ID.
Create a Client Secret
-
Open the app → Certificates & secrets → New client secret.
-
Add description, set expiry (e.g., 730 days).
-
Save the secret value securely (this will be your
CLIENT_SECRET
).
Get Subscription ID
-
In the Azure Portal, go to Subscriptions.
-
Copy the Subscription ID of the account you want to monitor.
Assign Role to App
-
Select your subscription → Access control (IAM) → Add role assignment.
-
Assign Cost Management Reader to the registered app.
-
Click Review + assign.
Now, you have all the necessary credentials:
-
Tenant ID
-
Client ID (Application ID)
-
Client Secret
-
Subscription ID
Step 2: Implementing the Code in AWS Lambda
With credentials in place, we can implement the automation in AWS Lambda. If you prefer, the same implementation can be done in Azure Functions with minimal adjustments. Both services are fully capable of running the cost notifier script; the choice depends on where you want to centralize your automation.
Create the Lambda Function
-
Go to AWS Lambda → Create function.
-
Configure:
-
Name:
azure-cost-notifier
-
Runtime: Python 3.13
-
-
Click the Create function.
Add Environment Variables
In the Lambda console, under Configuration → Environment variables, add the following variables with their corresponding values:
-
SUBSCRIPTION_ID
-
TENANT_ID
-
CLIENT_ID
-
CLIENT_SECRET
Python Code for Lambda
The script authenticates with Microsoft Entra ID, queries Azure Cost Management, processes costs, and posts a report to Slack.
Highlights of the code:
-
Authentication – Uses OAuth2 client credentials flow with
https://management.azure.com/.default
scope. -
Query – Calls Azure Cost Management API with custom date range (first day to last day of the current month).
-
Aggregation – Sums total costs and groups by service (
ServiceName
). -
Slack Message – Posts a formatted message including top cost-driving services.
Step 3: Adding Lambda Layer
The script uses the requests
library, which is not included in AWS Lambda by default. To ensure the script works as expected, you need to add the requests Lambda layer. Alternatively, you can modify the script to use Python’s built-in urllib.request
library instead of requests
, eliminating the need for an additional Lambda layer.
Step 4: Scheduling with Amazon EventBridge
Automated reports are only useful if they’re timely. To run the Lambda function daily:
-
In Lambda → Configuration → Triggers.
-
Add a new trigger:
-
Source: EventBridge (CloudWatch Events).
-
Schedule expression:
cron(0 1 * * ? *)
→ runs every day at 9 AM UTC+8.
-
-
Save the trigger.
Your notifier will now run daily and send Slack messages without manual intervention.
Benefits of This Setup
-
Cross-Cloud Flexibility: While costs are in Azure, the automation runs in AWS. This avoids vendor lock-in.
-
Proactive Cost Visibility: Teams see daily summaries, preventing end-of-month surprises.
-
Granularity: The notifier breaks costs down by service, highlighting which resources drive expenses.
-
Scalability: The same architecture can be extended to multiple subscriptions or integrated with other reporting tools.
Conclusion
Cloud cost management is no longer optional. It’s essential for organizations operating at scale. By combining Azure Cost Management APIs, Microsoft Entra ID authentication, and AWS Lambda automation, you can build a lightweight but useful system that pushes actionable cost insights directly into Slack.
This proactive monitoring helps organizations control budgets, optimize workloads, and align cloud spending with business objectives.
With this setup in place, teams can focus on delivering business value without worrying about unexpected cloud bills.
Reference:
https://learn.microsoft.com/en-us/azure/cost-management-billing/automate/automation-overview