diff --git a/telegram-notifier/IAMPolicy b/telegram-notifier/IAMPolicy new file mode 100644 index 0000000..fb0ae15 --- /dev/null +++ b/telegram-notifier/IAMPolicy @@ -0,0 +1,14 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "logs:CreateLogGroup", + "logs:CreateLogStream", + "logs:PutLogEvents" + ], + "Resource": "*" + } + ] +} diff --git a/telegram-notifier/LambdaFunction.py b/telegram-notifier/LambdaFunction.py new file mode 100644 index 0000000..78758bb --- /dev/null +++ b/telegram-notifier/LambdaFunction.py @@ -0,0 +1,40 @@ +from __future__ import print_function + +import boto3 +import json +import logging +import os +import urllib +from urllib2 import Request, urlopen, URLError, HTTPError + +""" +BOT_ID = Your Telegram BOT ID. Example: bot123456789:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX +CHAT_ID = Is your channel ID or your BOT chat window id +""" + +BOT_ID = "bot123456789:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" +CHAT_ID = "01234567891234" +API_ENDPOINT = "https://api.telegram.org/%s/sendMessage" % BOT_ID + +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +def lambda_handler(event, context): + message = str(event['detail']['eventDescription'][0]['latestDescription'] + "\n\n for details.") + + msg = { + "chat_id":CHAT_ID, + "text": message + } + + logger.info(str(msg)) + + req = Request(API_ENDPOINT, urllib.urlencode(msg)) + try: + response = urlopen(req) + response.read() + logger.info("Message posted to %s", CHAT_ID) + except HTTPError as e: + logger.error("Request failed: %d %s", e.code, e.reason) + except URLError as e: + logger.error("Server connection failed: %s", e.reason) diff --git a/telegram-notifier/README.md b/telegram-notifier/README.md new file mode 100644 index 0000000..3f46152 --- /dev/null +++ b/telegram-notifier/README.md @@ -0,0 +1,39 @@ +## AWS Health Telegram Notifier + +### Description + +This tool can be used to post alerts to a Telegram Channel or Bot when AWS Health events are generated by using AWS Lambda and Amazon CloudWatch Events. + +### Telegram Setup +Follow these steps to create a bot in Telegram: + +1. Navigate to https://core.telegram.org/bots#3-how-do-i-create-a-bot to create a bot. + +2. Save the Token. + +3. Create a private Channel or Chat to your bot. + +4. If you are using a channel, add your bot as channel's administrator. + +4. Run this command to get the CHAT_ID for the channel or bot. + curl -s -XPOST https://api.telegram.org/bot/getMe + +### AWS Setup + +1. Create an IAM role for the Lambda function to use. Attach the [IAM policy](IAMPolicy) to the role in the IAM console. +Documentation on how to create an IAM policy is available here: http://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html +Documentation on how to create an IAM role for Lambda is available here: http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-service.html#roles-creatingrole-service-console + +2. Create a Lambda Python function by using the [sample](LambdaFunction.py) provided and choose the IAM role created in step 1. Update the configuration section of the Lambda function with webhook URL from the Slack setup above and update the Slack channel that you want AWS Health messages posted in. +More information about Lambda is available here: http://docs.aws.amazon.com/lambda/latest/dg/getting-started.html +More information about Slack integration with Lambda is available here: https://aws.amazon.com/blogs/aws/new-slack-integration-blueprints-for-aws-lambda/ + +3. Create a CloudWatch Events rule to trigger the Lambda function created in step 2 for AWS Health events. +Documentation on how to create an AWS Health CloudWatch Events rule is available here: http://docs.aws.amazon.com/health/latest/ug/cloudwatch-events-health.html + +More information about AWS Health is available here: http://docs.aws.amazon.com/health/latest/ug/what-is-aws-health.html + +Note that this is a just an example of how to set up automation with AWS Health, Amazon CloudWatch Events, and AWS Lambda. We recommend testing the example and tailoring it to your environment before using it in your production environment. + +### License +AWS Health Tools are licensed under the Apache 2.0 License.