How to get an automated alert when your Azure cost takes off

I wrote this quick-and-dirty script to let me know if I happen to forget to turn off a P15 instance, or if I configure a service with a super-expensive performance tier without realizing. Maxing out your free Azure credits may be depressing enough, but emptying your credit card could really put you in the hurt locker.

So, here’s a Powershell script that warns me before any of this happens. It uses the Azure Consumption API to check how much money we’ve racked up on a subscription so far, and if any single instance exceeds, say, 50% of that total cost, it sends a notification to a Slack channel.

How to download

I’ve put the script on GitHub, so feel free to download it there. By all means, send me a pull request if you have any brilliant additions. You’ll find that I’m not a Powershell pro, so there may be glaring bugs or omissions.

What you’ll need

You’ll need to set up an Azure Automation account to run your script in a Runbook. I messed with this way back, but I honestly wouldn’t remember how to do it from scratch, so if you don’t already have an Automation account, ask Google.

To make the Azure Consumption module work, you’ll need to install it from the Powershell Gallery to your Automation account, if you haven’t done so already. Just click the ”Deploy to Azure Automation” button.

You’ll also need to set up a custom Slack integration. The custom integration is basically a webhook that you can send messages to, and those messages will appear in a channel of your choice. Look for “Incoming webhooks” in your Slack configuration.

You’ll need to copy-and-paste the URL of your custom webhook into the script.


At this point, the only parameter is the optional $ThresholdPercentage parameter, which controls when the script fires off an alert. If you set $ThresholdPercentage=40, and your total consumption this month is $100, the script will fire an alert for each service instance that exceeds a consumption of $40.

Fine print

This is a hack. It’s on GitHub, so feel free to contribute with improvements, but there’s no warranty, no guarantees. You need to test everything you want to run.

2 thoughts on “How to get an automated alert when your Azure cost takes off

  1. Pingback: Warning on Azure Consumption – Curated SQL

  2. Easier approach:
    Go to the resource group you want to monitor in the portal
    Set up a budget you want to spend max on a monthly basis
    Set up a cost alert any percentage of your budget spent and take actions
    I would at least sent a mail, but in Test environments maybe stopping the services is more reasonable.
    Personally, I think using an extra PowerShell script is over the top if you just need to monitor. I would suggest to use this if you need to export the data.

Leave a Reply to Tarek Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.