Skip to content

Instantly share code, notes, and snippets.

Last active September 14, 2020 14:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
AWS Lambda Function to Post an alert to a Slack channel every time Cloudwatch triggers the SNS-Backed Lambda
import logging
from json import dumps
from os import environ
from urllib3 import PoolManager
def post_to_slack(event, context):
slack_channel = environ.get("SLACK_NET_CHANNEL")"Event: " + str(event))
http = PoolManager()
slack_message = {
"text": "Sample message from Lambda - lambdaSlackNetAlerts - Network Alert - Traffic has exceeded your defined threshold!!"
response = http.request(
headers={"Content-Type": "application/json"},
res_text ="utf-8")"Message posted to: #network-aws-alerts")"Slack's Response:{res_text.upper()} - {response.status}")
if response.status == 200:
return {"statusCode": 200, "body": dumps("Lambda has executed correctly!")}
return {
"statusCode": f"{response.status}",
"body": dumps(f"Lambda Failed - Reason: {res_text}"),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment