Note: This is still a work in progress. Contributions are welcome.
This is a Lambda function that receives messages from an SNS queue and sends it to a specified Slack Channel. It includes logic to transform your SNS topic's notification messages for the type of webhook endpoint that you're using and is compatible with the Python 3.9 runtime.
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.
This project contains:
index.py
file - The file containing the lambda function.circleci
folder - A folder that contains theconfig.yml
for deployment using Circle CI to Amazon Web Services (AWS) Lambda Function if you want to.README.md
file - The file contains the lambda function setup guide
Incoming webhooks are a simple way to share information from external sources with your workspace:
- Create a new Slack app in the workspace where you want to post messages.
- From the Features page, toggle Activate Incoming Webhooks on.
- Click Add New Webhook to Workspace.
- Pick a channel that the app will post to, then click Authorize.
- Use your Incoming Webhook URL to post a message to Slack.
Note: Check out our Slack API documentation for more details about using incoming webhooks.
If you haven't done so already, create an SNS topic with a unique name.
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 will contain the index.py
file which is the code that includes logic to transform your SNS topic's notification messages for the type of Slack webhook endpoint.
Set the following environment variables for the Lambda function:
- SLACK_WEBHOOK_URL - The Slack Webhook URL. An example is
https://hooks.slack.com/services/T020CSDKTR5/B0472HJAAC8/DpkDFDGRg4q4yt40VFHbwKz
- SLACK_CHANNEL - The Slack Channel. An example is
#aws-notifications
- SLACK_ICON_EMOJI - The Slack Icon Emoji. An example is
:aws:
- SLACK_USERNAME - Your preferred Slack Username. An example is
aws-cloudwatch
- 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, then 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, then check the webhook URL to verify 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.
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 Invoking Lambda functions.
- 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.