Sending CloudWatch Alarm Status To Telegram
In this post, we’ll see how we can send CloudWatch Alarm status to Telegram.
Pre-requisite: For this demo, we’ve created an EC2 instance and set up a CloudWatch alarm on CPUUtilization metric.
Step 1: Open Telegram, search @BotFather and send “/start” message.
Step 2: Send “/newbot” message, and follow the instructions to setup a name and a username. Note down the token you get once the bot is created.
Step 3: Now search your bot and send a message from a mobile device(don’t use Telegram desktop application).
Step 4: Next we need to fetch chat id. For this go to https://api.telegram.org/bot<yourtoken>/getUpdates (replace <yourtoken> with the token we got in Step 2).
Note down the value for id field from the response you get. If you see an empty response, try sending another message.
Step 5: Now go to AWS console and create a Python 3.9 lambda function with the given code. Replace <yourtoken> and. <chatid> with appropriate values.
import json
import urllib3def lambda_handler(event, context):
instance_id=event['detail']['configuration']['metrics'][0]['metricStat']['metric']['dimensions']['InstanceId']
bot_token = '<yourtoken>'
bot_chatID = '<chatid>'
message=instance_id + " breached CPU utilization threshold."
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + message
http = urllib3.PoolManager()
response = http.request("GET", send_text)
return (response.status)
Step 6: Next create an Eventbridge rule with following pattern.Replace <cloudwatch_alarm_arn> with appropriate value. You can get the ARN by selecting your CloudWatch alarm, going to Details section and clicking on View EventBridge rule.
{
"source": [
"aws.cloudwatch"
],
"detail-type": [
"CloudWatch Alarm State Change"
],
"resources": [
"<cloudwatch_alarm_arn>"
],
"detail": {
"state": {
"value": [
"ALARM"
]
}
}
}
Set Target as your lambda function which we created in Step 5.
Step 7: Now connect to your EC2 instance and generate some load by executing following command:
for i in 1 2 3 4; do while : ; do : ; done & done
Once our alarm threshold is breached, you should get a message on Telegram.
Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.
If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇
Ten articles before and after
12:24 AM | Time Conversion and Information | – Telegram 繁体版 下载
What Time is 11:31? | – Telegram 繁体版 下载
14:48 Time | Conversion and Information | – Telegram 繁体版 下载
13:09 Time | Conversion and Information | – Telegram 繁体版 下载
16:56 Time | Conversion and Information | – Telegram 繁体版 下载
使用隧道中转 socks5 代理的方式来上 Telegram 存在安全风险吗? | – Telegram 简体版
[BEST] Tricks To Rank #1 Your Telegram Channel (2022) – Telegram 简体版
(My Notes) Telegram Bot
Telegram Announcement: The Upcoming PYDEX Launch – Telegram 官网下载