Skip to content

Create SMS Alert Channel using a Custom Webhook and AWS SNS

Video Lecture

Create SMS Alert Channel using a Custom Webhook and AWS SNS

Description

In this video, I show you how you can create an alert channel using a custom webhook that sends SMS. The SMS gateway I will use is AWS SNS.

You will need an AWS account. Log in and create an IAM user, and add it to a group with the AmazonSNSFullAccess policy. Take note of the Access Key and Secret given to you by AWS.

Go to the SNS Service console, select the Region you desire. I use us-east-1 in the video.

Press the link Start with an Overview and the press the link Text Messaging (SMS)

See video for more details

On your Grafana server.

Install python pip

sudo apt install python-pip

Install Flask and Boto3

pip install flask boto3

start new screen session

screen

add a new file called sendsms.py

sudo nano sendsms.py

and add the python script below

#Copyright 2020 Sean Bradley https://sbcode.net/grafana/ MIT License
from flask import Flask, request
import boto3

APP = Flask(__name__)

CLIENT = boto3.client(
    "sns",
    aws_access_key_id="your_access_key_id",
    aws_secret_access_key="you_secret_access_key",
    region_name="us-east-1"
)


@APP.route('/sendsms', methods=['POST'])
def sendsms():
    number = "+" + request.args.get('number')
    print(number)
    message = request.json["message"]
    print(message)

    response = CLIENT.publish(
        PhoneNumber=number,
        Message=message,
        MessageAttributes={
            'AWS.SNS.SMS.SenderID':
            {
                'DataType': 'String',
                'StringValue': 'Grafana'
            }
        }
    )
    return response


APP.run()

Start the python script using

python sendsms.py

You should see a response similar to

root@grafana:~# python sendsms.py
 * Serving Flask app "sendsms" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Press CTRL A then CTRL D to exit the screen session without closing it. You can come back to a screen session at anytime by typing

screen -r

Go into the Grafana UI

Select Alerting-->Notification Channels and add a New Channel

Key Value
Name SMS
Type webhook
Url http://127.0.0.1:5000/sendsms?number=**your telephone number**
Http Method POST

Warning

In the above table, the telephone number added to the URL should be in E.164 format but without the + symbol.

eg. Country code +44, with mobile number 07700 900123 should be written as 447700900123.

So the URL would now be http://127.0.0.1:5000/sendsms?number=447700900123

Now press the Send Test button. You should get an SMS on your mobile telephone.

AWS SNS SMS Webhook GitHub Repository

AWS SNS SMS Pricing

Request AWS SMS Limit Increase

Comments