-
Notifications
You must be signed in to change notification settings - Fork 245
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
abutaha
wants to merge
1
commit into
aws:master
Choose a base branch
from
abutaha:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "*" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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