Set Up Real-Time Bedrock Token Alerts in Discord

Set Up Real-Time Bedrock Token Alerts in Discord

Get instant Minecraft server alerts in Discord! This guide shows you how to set up real-time notifications for Bedrock tokens using AWS CloudWatch, SNS, and Lambda.

Published Feb 19, 2024
In the world of cloud computing, it's crucial to have real-time monitoring and alerting systems in place to ensure the smooth operation of your applications. AWS SNS (Simple Notification Service) is a powerful tool for sending notifications, and when combined with Discord, it can provide a convenient and effective way to receive alerts directly in a Discord channel. This article will guide you through integrating AWS SNS with Discord to set up real-time token alerts for Bedrock, a popular Minecraft server platform.

Use Case

Suppose you're running a Bedrock integration in any application and want to be notified immediately when certain thresholds are crossed, such as token usage. This real-time alerting can help you quickly address issues before they escalate, minimizing downtime and ensuring a stable experience cost optimization.
Why Real-Time Alerts Matter?
  • Minimize Downtime: Prompt action upon token exhaustion prevents disruptions for your players, ensuring a seamless Minecraft experience.
  • Optimize Costs: Avoid unnecessary token consumption by staying informed and taking action when thresholds are reached.
  • Peace of Mind: Gain proactive control over your server's health, eliminating the worry of unexpected token shortages.
Steps to Set Up Real-Time Alerts by Integrating AWS SNS with Discord and CloudWatch for Real-Time Generative AI Token Alerts of Bedrock Models

Prerequisite: Gather Information

Before you begin the integration process, you need to gather some necessary information:
  • Metric and Threshold: Determine the specific Bedrock metric you want to monitor and the threshold that should trigger an alarm.
  • Discord Webhook URL: Obtain the webhook URL from your Discord webhook's settings.
    → To create a webhook URL follow this my another blog How to Obtain a Discord Webhook .
  • Notification Message Format: Decide on the content and format of the notification message.

Step 1: Create an IAM Role:

First, create an IAM (Identity and Access Management) role that grants the necessary permissions for CloudWatch to interact with SNS and for the Lambda function to execute properly. This role should include policies that allow publishing messages to SNS topics and logging from Lambda functions.
In the AWS Management Console, you'll need to create an IAM role that has the necessary permissions to publish to SNS topics and trigger Lambda functions.
  • In the AWS Management Console, go to the IAM service and navigate to Roles.
  • Click Create role and select AWS service as the trusted entity type.
  • Choose Lambda as the service and click "Next: Permissions".Review and name the role (e.g., bedrock-cloudwatch-notifier). Click Create role.

    Attach the following policies to the role:
    • CloudWatchEventsFullAccess: Grants access to CloudWatch Events for triggering Lambda functions.
    • AmazonSNSFullAccess: Allows the role to publish to SNS topics.
    • cloudwatchLogsFullAccess: Enables writing logs to CloudWatch Logs.
  • Review and name the role (e.g., bedrock-cloudwatch-notifier). Click Create role.

Step 2: Create a Lambda Function:

Create a Lambda function that will serve as an intermediary between CloudWatch and Discord. This function will be triggered by SNS and will format and forward the alerts to your Discord channel via a webhook.
  • Go to the Lambda service and click Create function.
  • Choose Author from scratch and provide a name (e.g., bedrock-cloudwatch-discord-notifier).
  • Select Runtime as Python 3.10 and Execution role as the IAM role you created in step 2.
  • Click Create function
import json
from discord_webhook import DiscordWebhook

def lambda_handler(event, context):
# Extract relevant data from SNS message
message = json.loads(event['Records'][0]['Sns']['Message'])
data = {
"AlarmName": message['AlarmName'],
"AlarmConfigurationUpdatedTimestamp": message['AlarmConfigurationUpdatedTimestamp'],
"MetricName": message['Trigger']['MetricName'],
"Namespace": message['Trigger']['Namespace'],
"Statistic": message['Trigger']['Statistic'],
"Threshold": message['Trigger']['Threshold'],

# Use a template string for message formatting
formatted_message = f"""**CloudWatch Notification for Bedrock:**\n
* Alarm Name: {data.get('AlarmName')}
* Updated At: {data.get('AlarmConfigurationUpdatedTimestamp')}
* Metric Name: {data.get('MetricName')}
* Namespace: {data.get('Namespace')}
* Statistic: {data.get('Statistic')}
* Threshold: {data.get('Threshold')}

# Send message to Discord using webhook
webhook = DiscordWebhook(url="DISCORD-WEBHOOK-URL", content =formatted_message)

return {
'statusCode': 200,
'body': json.dumps('Message sent to Discord with formatted data'),
'alarm_name': data.get('AlarmName')
except Exception as e:
webhook = DiscordWebhook(url="DISCORD-WEBHOOK-URL", content ="tudungggggggggaa")

return {
'statusCode': 400,
'body': json.dumps(f'Error sending message to Discord: {e}'),
'alarm_name': None

  • Click Save.
  • Click on the Function code tab and upload your ZIP file containing the Lambda function code with the zip file with dependencies like discord webhook.

Step 3 : Create an SNS Topic

Set up an SNS topic that will publish messages to the Lambda function when the specified Bedrock metric exceeds the threshold.
Go to the SNS service and click Create topic. topic that will publish messages to the Lambda function when the specified Bedrock metric exceeds the threshold.
Provide a name (e.g., bedrock-cloudwatch-alarms).
Click Create topic.
When Topics is created then add the subscription navigating to create subscription as following
Select Protocol as AWS Lambda and Select Endpoint of lambda that we just created
Here, we can see lambda as a subscription of lambda is added.
  • After adding Lambda as a subscription a SNS is added as a trigger of the lambda as we see.

Step 4: Create a CloudWatch Alarm

Configure a CloudWatch alarm to monitor the Bedrock metric and trigger the SNS topic when the threshold is reached.
  • Go to the CloudWatch service and navigate to "Alarms".
  • Click "Create alarm".
  • Choose the Bedrock namespace (e.g., AWS/OpsWorks/Bedrock) and your specific Bedrock metric from step 1.

    Configure the alarm details:
  • Statistic: Choose the appropriate statistic (e.g., Average, Maximum, Sum).
  • Period: Set the interval for evaluating the metric (e.g., 5 minutes).
  • Threshold: Enter the trigger threshold value.
  • Evaluation Periods: Specify the number of consecutive periods over which the threshold must be exceeded.
  • Alarm Name: Provide a descriptive name (e.g., Bedrock-{server-name}-{metric-name}-High).
  • Under Alarm actions, click Add action and choose SNS topic.
  • Select the SNS topic you created in step 4.
  • Optionally, add a CloudWatch Logs log group for detailed logging.
  • Click Create alarm.

Step 5: Test and Monitor

Finally, test the system by manually triggering the CloudWatch alarm and ensure that the notification is received in the Discord channel.
  • Manually trigger the CloudWatch alarm to verify that the the notification if it works or not by using bedrock tokens to exceeds the threshold.


By following these steps, you can set up a robust monitoring system for your AWS Bedrock models that notifies you in real-time about significant events or threshold breaches. This integration not only enhances your ability to respond quickly to potential issues but also enables you to manage your generative AI applications more effectively, ensuring optimal performance and cost efficiency.