Slack messages from AWS Lambda

When developing Python code in AWS Lambda, there are times where you have a need to send a message to Slack. For some reason, AWS decided not to include the requests library with Python 3.8. Instead, you can use urllib to achieve the same.

Go ahead and create your webhook, and use the following code snippit in your AWS Lambda function to send your Slack message.

import json
import urllib.request
import urllib.parse

def slackMe(webHook,message):
    req = urllib.request.Request(
        webHook,
        json.dumps({'text': message}).encode('utf-8'),
        {'Content-Type': 'application/json'}
    )
    resp = urllib.request.urlopen(req)
    response = resp.read()
    
    print(response)

Leave a Reply

Your email address will not be published. Required fields are marked *