Ends in
00
days
00
hrs
00
mins
00
secs
ENROLL NOW

🔥 $2.99 AWS AIF-C01 Study Guide eBook plus 15% OFF our AI Reviewers

Prevent Unforeseen Azure Costs: Setting Up Automated Slack Alerts for Your Subscription

Home » Others » Prevent Unforeseen Azure Costs: Setting Up Automated Slack Alerts for Your Subscription

Prevent Unforeseen Azure Costs: Setting Up Automated Slack Alerts for Your Subscription

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.

Azure Cost NotifierImage 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:

  1. Improves financial accountability – IT and finance teams know exactly where money is being spent.

  2. Supports decision-making – Leaders can prioritize which workloads should scale and which should be optimized.

  3. Prevents waste – Alerts help detect unused or underutilized resources.

  4. 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:

  1. Azure Cost Management API – Provides detailed cost and usage information at the subscription level.

  2. Authentication via Microsoft Entra ID – Uses a registered app and service principal for secure API access.

  3. AWS Lambda Function – Runs Python code to fetch costs, process results, and send notifications to Slack.

  4. 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.

Tutorials dojo strip

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 & secretsNew 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 LambdaCreate 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

Free AWS Courses

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:

  1. In Lambda → Configuration → Triggers.

  2. Add a new trigger:

    • Source: EventBridge (CloudWatch Events).

    • Schedule expression: cron(0 1 * * ? *) → runs every day at 9 AM UTC+8.

  3. 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

🤖 Bonus Deal: Get 15% OFF for All AI Reviewers

Tutorials Dojo portal

Learn AWS with our PlayCloud Hands-On Labs

🧑‍💻 CodeQuest – AI-Powered Programming Labs

FREE AI and AWS Digital Courses

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

FREE AWS, Azure, GCP Practice Test Samplers

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

Join Data Engineering Pilipinas – Connect, Learn, and Grow!

Data-Engineering-PH

Ready to take the first step towards your dream career?

Dash2Career

K8SUG

Follow Us On Linkedin

Recent Posts

Written by: Nestor Mayagma Jr.

Nestor is a cloud engineer and content creator at Tutorials Dojo. He's been an active AWS Community Builder since 2022, with a growing interest in multi-cloud technologies across AWS, Azure, and Google Cloud. In his leisure time, he indulges in playing FPS games.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?