Sending CloudWatch Alarm Status To Telegram – Telegram Group

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

7 Builders For No-Code Creation of Chatbots in Telegram – Telegram Group

Communication in Times of Russian Invasion: Telegram, Viber or Something Better? – Telegram Group

Should I buy fake followers for my NFT/Web3 projects Twitter/Telegram accounts? – Telegram Group

Understanding Telegram’s ecosystem of far-right channels in the US – Telegram Group

️?Celebration of 100,000 members mark in KDG Telegram group ?? – Telegram Group

Cómo crear un Bot de Telegram?. Hola ? soy YOSS, en este post te… – Telegram Group

Telegram channels amplified document falsely justifying Russian invasion – Telegram Group

Crypto High roller trader scam [Exposed] – Telegram Group

FairySwap X Crypto Lab. ? : 15th March ,2pm UTC
Venue… – Telegram Group

Sanal Topluluklar. Tanımsal olarak sanal topluluklar… – Telegram Group