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.