๐จ The Problem
Every cloud computing enthusiast shares the exact same fear: accidentally leaving a resource running and waking up to a massive, unexpected credit card bill.
When collaborating with teammates on new MVPs or spinning up hackathon backends, standard email budget alerts just aren't enough. They easily get buried in spam or ignored in a cluttered inbox. I see beginners hesitate to learn AWS purely out of financial fear. If a cloud bill is spiking, you need to know immediately, right where you already hang out.
This post demonstrates how to build a "Zero-Bill" alert system that monitors your AWS account and instantly fires a notification directly to a Discord server the moment your projected spend crosses $1.00.
๐๏ธ Architecture Overview
Here is how the data flows:
1.AWS Budgets: Monitors your account spend in real-time.
2.Amazon SNS (Simple Notification Service): Acts as the pub/sub messenger between Budgets and Lambda.
3.AWS Lambda: A lightweight Python function that formats the alert and sends it out.
4.Discord Webhook: The endpoint that receives the message and posts it to your server.
๐ ๏ธ Prerequisites and IAM
Before building, ensure you have:
โข An active AWS Account.
โข A Discord Server where you have permission to create Webhooks.
โข The IAM Policy: Your SNS topic must explicitly allow AWS Budgets to publish to it. When editing your default SNS access policy, you must append this exact statement to the Statement array:
JSON
{
"Sid": "AllowBudgetsToPublish",
"Effect": "Allow",
"Principal": {
"Service": "budgets.amazonaws.com"
},
"Action": "SNS:Publish",
"Resource": "arn:aws:sns:YOUR_REGION:YOUR_ACCOUNT_ID:Zero-Bill-Alerts"
}
(Remember to swap in your actual Region and Account ID!)
Step 1: Create the Discord Webhook
First, we need a destination for the alerts.
- Open your Discord Server and navigate to Settings > Integrations > Webhooks.
- Click New Webhook, name it AWS Billing Bot, and select your private monitoring channel.
- Click Copy Webhook URL and save this securely. โ ๏ธ Security Warning: Never commit this URL to a public GitHub repository! If leaked, anyone can spam your Discord server.
Step 2: Set up the SNS Topic
Amazon SNS acts as the bridge.
- Go to the AWS SNS Console and create a Standard topic named Zero-Bill-Alerts.
- Edit the Topic's Access Policy. Ensure you append the Budgets permission JSON (from the prerequisites) to the existing default policy list, rather than overwriting it entirely.
Step 3: Write the Lambda Function
We need a tiny Python script to catch the SNS message and forward it to Discord.
- Go to the AWS Lambda Console and create a new Python 3.12 (or 3.13) function.
- Under Configuration -> Environment variables, add a key called DISCORD_WEBHOOK_URL and paste your copied URL as the value.
- Paste the following code into the lambda_function.py file:
Python
import json
import urllib3
import os
def lambda_handler(event, context):
webhook_url = os.environ['DISCORD_WEBHOOK_URL']
# Extract the message from the SNS event
sns_message = event['Records'][0]['Sns']['Message']
# Format the message for Discord
discord_payload = {
"username": "AWS Billing Bot",
"avatar_url": "https://a0.awsstatic.com/libra-css/images/logos/aws_logo_smile_1200x630.png",
"content": f"๐จ **AWS BUDGET ALERT** ๐จ\n```
{% endraw %}
\n{sns_message}\n
{% raw %}
```"
}
# Send the request
http = urllib3.PoolManager()
response = http.request(
'POST',
webhook_url,
body=json.dumps(discord_payload),
headers={'Content-Type': 'application/json'}
)
return {
'statusCode': response.status,
'body': 'Message sent to Discord'
}
- Click Deploy. Then, click Add Trigger, select SNS, and choose the Zero-Bill-Alerts topic.
Step 4: Create the AWS Budget
Now, we wire it all together by creating the actual financial tripwire.
- Navigate to the AWS Billing Dashboard and select Budgets.
- Create a Cost budget and set the budgeted amount to $1.00.
- In the alert configuration, set it to trigger when Forecasted costs reach 100% of the budget.
- Under the notification settings, enter the ARN (Amazon Resource Name) of your Zero-Bill-Alerts SNS topic.
- Test it by, using the push notification, provide a message and scroll down to click the push notification button, go back to your discord server and verify whether the application is working.
๐งฑ The "Gotchas" (Lessons Learned)
While building this, I hit a few roadblocks that aren't explicitly covered in the standard AWS documentation.
โขThe SNS InvalidParameter Error: When attaching the IAM policy to the SNS topic, you might get this error: InvalidParameter: Policy Error: null. This happens if your JSON syntax is missing its wrappers or if you overwrite the default SNS policy completely. You must add the Budget permissions to the existing default Statement array, separating them with a comma.
โขThe urllib3 vs. requests Trap: Many tutorials tell you to use the requests library in Python. However, requests is not built into the standard AWS Lambda Python runtime. If you use it, your code will crash unless you manually upload a custom Lambda Layer. By using urllib3, which is included natively, the script runs instantly with zero extra configuration.
๐งน Resource Cleanup (FinOps)
This architecture is entirely serverless and easily fits within the AWS Free Tier. However, to maintain good cloud hygiene and ensure you don't leave orphaned resources behind, I highly recommend deleting the Budget, Lambda function, and SNS topic once you verify the architecture works.




Top comments (0)