Aws Sns To Slack

  



Aws sns slack python

1. Create a Topic on SNS

You can use private Slack channels with AWS Chatbot. To do so, choose Private channel. In Slack, copy the Channel ID of the private channel by right-clicking on the channel name in the left pane and choosing Copy Link. The Channel ID is the string at the end of the URL (for example, AB3BBLZZ8YY). Learn how to build on Slack your way with these tutorials and articles from around the web. We wrote some too! Build a customer feedback bot using AWS Lambda and SNS. Articles by Tag. We've tagged all this content so you can find it by your favorite language, feature, or flavor. Amazon Web Services (5) Real Time Messaging API (4.

To set up Amazon SNS notifications

Slack
  1. Open the Amazon SNS console at https://eu-central-1.console.aws.amazon.com/sns/v3/home
  2. Choose Topics > Create topic
  3. Create a topic with Name: CheckHealthW3Server and Display name: Health WWW servers. Other things should be default.
  4. After that go to view detail of CheckHealthW3Server to create a subscription.

2. Create a subscription

We can ignore this step if we didn’t create a lambda function before.

  1. Click Create subscription
  2. Topic ARN: Enter a ARN of the CheckHealthW3Server topic.
  3. Protocol: select AWS Lambda
  4. Endpoint: An AWS Lambda function that can receive notifications from Amazon SNS
  5. That’s all for a subscription. We can create many subscriptions with other protocol.
Aws Sns To Slack

3. Create a channel and config to get message from lambda function

  1. Slack > Create channel: #sns-notifications
  2. Create a apps to post messages from external sources into Slack. Visit the link: https://channel.slack.com/apps and search app name: Incoming WebHooks
  3. At homepage the app choose Add configuration and choose Post to Channel is #sns-notifications
  4. After that you can get value for Webhook URL like that: https://hooks.slack.com/services/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

4. Create a lambda function to be trigger by SNS service

  1. AWS Lambda > Create function
  2. We have three ways to create a function lambda. But we should use a blueprint because it helps us to assign this function to the topic we want automatically. At search field we use the keyword is sns-message and choose language is Nodejs.
  3. Fill these information for this function:
    1. Function name: notificationToSlack
    2. Execution role: Create a new role with basic Lambda permissions
    3. SNS topic: search CheckHealthW3Server
    4. Tick to Enable trigger
    5. Lambda function code: In this code you have to config again the path for calling webhook slack.

5. Publish a message to test

At the detail of the CheckHealthW3Server page.

  1. Click on Publish message
  2. Enter: Subject and Message body to send to the endpoint
  3. Open the #sns-notifications channel to see the result.

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(S) endpoints, such as webhook URLs. However, some webhooks expect JSON key-value pairs that Amazon SNS doesn't support when confirming the HTTP(S) 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, you use an AWS Lambda function.

Aws sns to slack test

Note: For a list of the key-value pairs in the Amazon SNS message body JSON document, see HTTP/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

Aws Sns Webhook

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.6 runtime.

Example Python code snippet for Amazon Chime

Aws sns to slack download

Amazon Chime webhooks expect a JSON request with a message string corresponding to a 'Content' key. For more information, see 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.

Example Python code snippet for Slack

Aws Sns Slack Chatbot

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.

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 Setting up a custom incoming webhook 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.

Test the Lambda function

  1. On the Functions page of the Lambda console, choose your function.
  2. At the top right, choose Select a test event. Then, choose Configure test events.
  3. In the Configure test event dialog box, choose Create new test event.
  4. For Event template, choose Amazon SNS Topic Notification.
  5. For Event name, enter a name for the test event.
  6. Choose Create.
  7. Choose Test.
  8. 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 and 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.

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 by doing the following:

  1. On the Functions page of the Lambda console, choose your function.
  2. Under Designer, choose Add trigger. For more information, see Use the designer.
  3. Under Trigger configuration, choose Select a trigger, and then choose SNS.
  4. For SNS topic, choose the SNS topic that you created earlier.
  5. Select the Enable trigger check box.
  6. Choose Add.

For more information, see Configuring functions in the AWS Lambda 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 Using AWS Chatbot with other AWS services.

Related information

Adding webhooks to chat rooms (Amazon Chime User Guide)

Raw message delivery (Amazon SNS FAQs)

Related videos

Close