How do I use webhooks to publish Amazon SNS messages to Amazon Chime, Slack, or Microsoft Teams?
I want to use webhooks to connect my AWS environment to my Amazon Chime chat room, or to my Slack or Microsoft Teams channel. How do I send notifications from Amazon Simple Notification Service (Amazon SNS) to a webhook?
Short description
You can use Amazon SNS to send notification messages to HTTP or HTTPS endpoints, such as webhook URLs. However, some webhooks expect JSON key-value pairs that Amazon SNS doesn't support when confirming the HTTP or HTTPS subscription.
For example, Amazon Chime webhooks expect a JSON request with a message string corresponding to a "Content" key. Similarly, Slack and Microsoft Teams webhooks both expect a JSON request with a message string corresponding to a "text" key.
To transform the Amazon SNS message body JSON document for the webhook endpoint to process, use an AWS Lambda function.
Note: For a list of the key-value pairs in the Amazon SNS message body JSON document, see HTTP and HTTPS notification JSON format.
Resolution
Create an SNS topic
If you haven't done so already, create an SNS topic with a unique name.
Create a Lambda function
For instructions to create a Lambda function, see Getting Started with AWS Lambda. For more information, see Using AWS Lambda with Amazon SNS.
Your Lambda function code must include logic to transform your SNS topic's notification messages for the type of webhook endpoint that you're using. For examples, see the following Python code snippets for Amazon Chime, Slack, and Microsoft Teams webhooks. These code examples are provided as-is. They're compatible with the Python 3.7 runtime .
Example Python code snippet for Amazon Chime
Amazon Chime webhooks expect a JSON request with a message string corresponding to a "Content" key. For more information, see Creating webhooks for Amazon Chime.
Note: In this example function code for Amazon Chime webhooks, replace https://hooks.chime.aws/incomingwebhooks/xxxxxxx with the webhook URL.
import urllib3 import json http = urllib3.PoolManager() def lambda_handler(event, context): url = "https://hooks.chime.aws/incomingwebhooks/xxxxxxx" msg = {"Content": event["Records"][0]["Sns"]["Message"]} 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, } )
Example Python code snippet for Slack
Slack Incoming Webhooks expect a JSON request with a message string corresponding to a "text" key. They also support message customization, such as adding a user name and icon, or overriding the webhook's default channel. For more information, see Sending messages using incoming webhooks on the Slack website.
Note: In this example function code for Slack Incoming Webhooks, replace https://hooks.slack.com/services/xxxxxxx with the Incoming Webhook URL. Also replace #CHANNEL_NAME with the destination channel's name.
import urllib3 import json http = urllib3.PoolManager() def lambda_handler(event, context): url = "https://hooks.slack.com/services/xxxxxxx" msg = { "channel": "#CHANNEL_NAME", "username": "WEBHOOK_USERNAME", "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, } )
Example Python code snippet for Microsoft Teams
Microsoft Teams incoming webhooks also expect a JSON request with a message string corresponding to a "text" key. For more information, see Create and send messages on the Microsoft Docs website.
Note: In this example function code for Microsoft Teams incoming webhooks, replace https://outlook.office.com/webhook/xxxxxxx with the webhook URL.
import urllib3 import json http = urllib3.PoolManager() def lambda_handler(event, context): url = "https://outlook.office.com/webhook/xxxxxxx" msg = {"text": event["Records"][0]["Sns"]["Message"]} 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, } )
Test the Lambda function
- On the Functions page of the Lambda console, choose your function.
- Choose the Test dropdown list. Then, choose Configure test event.
- In the Configure test event dialog box, choose Create new event.
- For Event template, choose SNS Topic Notification.
- For Event name, enter a name for the test event.
- Choose Save.
- After it's saved, choose Test. Then, review the Execution result.
If the test invocation succeeds with a 200 status code:
The Amazon SNS notification message is accepted by your webhook, which delivers it to the corresponding channel.
If the invocation fails with a 4xx status code:
Verify the webhook URL to confirm that the key-value pair is correct and accepted by your destination webhook.
For more information about testing functions in the Lambda console, see Invoke the Lambda function using the console.
Add an SNS topic trigger to your Lambda function
After sending an SNS message to your webhook as a test in the Lambda console, subscribe your function to your SNS topic. To configure this from the Lambda console, add an SNS topic trigger:
- On the Functions page of the Lambda console, choose your function.
- Under Function overview, choose Add trigger. For more information, see Invoke the Lambda function using the console.
- Under Trigger configuration, choose Select a trigger. Then, choose SNS.
- For SNS topic, choose the SNS topic that you created earlier.
- Choose Add.
For more information, see Configuring functions (console).
With your function subscribed to your SNS topic, messages published to the topic are forwarded to the function, and then to your webhook.
Note: For information on how to get Amazon SNS notifications through other AWS services, see Monitoring AWS Services using AWS Chatbot.
Related information
How do I subscribe a Lambda function to an Amazon SNS topic in the same account?
Related videos
Relevant content
- asked 9 months agolg...
- Accepted Answer
- asked 9 months agolg...
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 5 months ago
- AWS OFFICIALUpdated 20 days ago