Created
February 18, 2017 21:32
-
-
Save htnosm/97bfc7bff7e109b84b8dcb2aaffd19ee to your computer and use it in GitHub Desktop.
[Lambda] e.g. API Gateway->Lambda->Slack
This file contains hidden or 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
from __future__ import print_function | |
import boto3 | |
import json | |
import logging | |
import os | |
from base64 import b64decode | |
from urllib2 import Request, urlopen, URLError, HTTPError | |
# The base-64 encoded, encrypted key (CiphertextBlob) stored in the kmsEncryptedHookUrl environment variable | |
ENCRYPTED_HOOK_URL = os.environ['kmsEncryptedHookUrl'] | |
# The Slack channel to send a message to stored in the slackChannel environment variable | |
SLACK_CHANNEL = os.environ['slackChannel'] | |
HOOK_URL = "https://" + boto3.client('kms').decrypt(CiphertextBlob=b64decode(ENCRYPTED_HOOK_URL))['Plaintext'] | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
def lambda_handler(event, context): | |
logger.info("Event: " + str(event)) | |
slack_message = { | |
'channel': SLACK_CHANNEL, | |
'text': "%s" % (str(event)) | |
} | |
req = Request(HOOK_URL, json.dumps(slack_message)) | |
try: | |
response = urlopen(req) | |
response.read() | |
logger.info("Message posted to %s", slack_message['channel']) | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment