Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Telegram Notification Lambda function #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions telegram-notifier/IAMPolicy
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
40 changes: 40 additions & 0 deletions telegram-notifier/LambdaFunction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import print_function

import boto3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boto3, json and os modules are being imported without being needed

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
premsurya marked this conversation as resolved.
Show resolved Hide resolved

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
message = str(event['detail']['eventDescription'][0]['latestDescription'] + "\n\n<https://phd.aws.amazon.com/phd/home?region=us-east-1#/event-log?eventID=" + event['detail']['eventArn'] + " | Click here> for details.")

msg = {
"chat_id":CHAT_ID,
"text": message
}

logger.info(str(msg))

req = Request(API_ENDPOINT, urllib.urlencode(msg))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using the requests module here will probably be more concise, and probably more familiar to python coders.

as an added plus there's no need to bundle the lib in your deployment package as it's included in botocore:

import botocore.vendored.requests as requests

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)
39 changes: 39 additions & 0 deletions telegram-notifier/README.md
Original file line number Diff line number Diff line change
@@ -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<token>/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.