'Python Lambda Function not producing an output for AWS Eventbridge

Basically, I use this Example Python code snippet for Slack from an example python Lambda function provided by aws.

#!/usr/bin/python3.6
import urllib3
import json
http = urllib3.PoolManager()
def lambda_handler(event, context):
    url = "https://hooks.slack.com/services/xxxxxxx"
    msg = {
        "channel": "aws-events",
        "username": "Event_Notifier",
        "text": event['Records'][0]['Sns']['Message'],
        "icon_emoji": ""
    }
    
    encoded_msg = json.dumps(msg).encode('utf-8')
    resp = http.request('POST',url, body=encoded_msg)
    print({
        "message": event['Records'][0]['Sns']['Message'], 
        "status_code": resp.status, 
        "response": resp.data
    })

Unfortunately, the above code does not produce any output and does not send messages to the slack channel.

So we made some adjustment as follows, and now it sends a generic message(that we set) always whenever there is an event, but not a meaningful message for the team and it does not help really to find the correct event on the message itself. The team have to go to the aws control panel each time to check what event the trigger was for.

#!/usr/bin/python3.6
import urllib3
import json
http = urllib3.PoolManager()
def lambda_handler(event, context):
    print(event)
    url = "https://hooks.slack.com/services/xxxxxxx"
    msg = {
        "channel": "aws-events",
        "username": "Event_Notifier",
        #"text": event['Records'][0]['Sns']['Message'],
        "text": "Upcoming Events, check here https://phd.aws.amazon.com/phd/home?region=eu-west-1#/event-log",
        "icon_emoji": ""
    }
    
    encoded_msg = json.dumps(msg).encode('utf-8')
    resp = http.request('POST',url, body=encoded_msg)
    print({
        #"message": event['Records'][0]['Sns']['Message'], 
        "message": "Upcoming Events, check here https://phd.aws.amazon.com/phd/home?region=eu-west-1#/event-log",
        "status_code": resp.status, 
        "response": resp.data
    })

Now, we have made further changes, but it stopped fully now.

#!/usr/bin/python3.6
import urllib3
import json
http = urllib3.PoolManager()

def lambda_handler(event, context):
    print(event)
    url = "https://hooks.slack.com/services/xxxxxxx"
    last_event = event['Records'][0]['Sns']['Message']

    msg = {
        "channel": "aws-events",
        "username": "Event_Notifier",
        "text": "Upcoming Events, check here https://phd.aws.amazon.com/phd/home?region=eu-west-1#/event-log" + "```\n" + str(last_event) + "\n```",
        "icon_emoji": ""
    }
    
    encoded_msg = json.dumps(msg).encode('utf-8')
    resp = http.request('POST',url, body=encoded_msg)
    
    print({
        "message": "Upcoming Events, check here https://phd.aws.amazon.com/phd/home?region=eu-west-1#/event-log" + "```\n" + str(last_event) + "\n```",
        "status_code": resp.status, 
        "response": resp.data
    })

Now it is going clueless how to make it properly work with the Amazon EventBridge.



Solution 1:[1]

Let me know if you are doing a similar type of this:

https://www.cloudkaramchari.com/blog/receive-email-in-aws-ses-and-save-in-dynamodb/

If yes, then we can customise your lambda to achieve the goal.

Solution 2:[2]

Validate the payload, and capture error response. There has been an update in slack messaging structure formats that you can use.

Refer to the Slack docs for details

https://api.slack.com/messaging/webhooks#handling_errors


There's an easier way to do the integration using AWS Chatbot that you can explore. It eliminates lambda and takes care of notification parsing directly from SNS.

AWS Blog on Slocak Integration with AWS Chatbot

https://aws.amazon.com/blogs/mt/monitor-amazon-eventbridge-events-in-your-slack-channels-with-aws-chatbot/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cloud Karamchari
Solution 2 Nick