How to push a message from an AWS SNS topic to a Slack channel

AWS SNS is a pub/sub messaging service that delivers messages from a publisher to a subscriber. Publishers send messages to a SNS topic, which stores those messages in a queue. A list of AWS services capable of publishing to a SNS topic can be found here. Clients can subscribe to a SNS topic and receive messages from that topic using a supported endpoint like AWS Lambda or an HTTP webhook. A list of message subscribers can be found here.

To push messages to Slack, we will use the HTTP webhook endpoints for a SNS topic. When used in conjunction with a Slack webhook, SNS can be used to publish alerts, logs or other information about your AWS resources directly to a Slack channel, which makes this information easily accessible to your entire team. 

Process:

  1. Create a new Standard SNS topic where the messages should be sent
    • This SNS topic will receive any messages sent by our publisher services and forward those messages to our Slack channel
    • You can use the AWS console or the AWS SDK to create a new SNS topic; see this guide for a detailed guide on how to create a SNS topic
  2. Create a new Slack workflow
    • Slack workflows are automated tasks that run in response to some trigger
    • In our case, our workflow will be triggered whenever SNS sends a HTTPS request to a Slack webhook; our workflow will parse the request body and then send the contents of the SNS message to a Slack channel of our choice
    • For more information on how to create a workflow, see this article
  3. Select a webhook workflow trigger when prompted 
    • Add two text variables to the workflow trigger: “Message” and “SubscribeURL”; the example body should look like this (these are the variables that Slack pulls out of the request sent by SNS)
    • The “Message” variable stores the body of the message sent by SNS
    • The “SubscribeURL” variable stores a URL that SNS generates to confirm that we have access to the webhook; we need to enter this URL in the SNS console to prove that we have access to our webhook; once we have confirmed our access, SNS will now be able to send messages to our webhook; after we have confirmed the URL, we can remove the “SubscribeURL” variable from our workflow, as it is only the sent in the confirmation message and not in any subsequent SNS messages

    Example HTTP body
  4. Add a step to your workflow and select the “Send message” type when prompted
    • For the “Send this message to field”, choose the Slack channel you wish to send your notifications to
  5. For the “Message text”, copy the image below

    Message text with subscribe URL
  6. Publish your Slack workflow and then copy the webhook URL that Slack generated for you
  7. Go back to the AWS SNS console and create a new HTTPS subscription to the webhook URL generated in the last step
    • You should see a message with the below format in your Slack channel; this is a confirmation message sent by SNS; you must confirm your webhook endpoint before you can send messages to it
  8. Copy the SubscribeURL sent to your Slack channel and then navigate back to your SNS console
    • Select your HTTPS subscription, choose “Confirm subscription” and then paste in the SubscribeURL

      Message textbox
  9. Your webhook subscription should now be subscribed; you should now remove the “SubscribeURL” variable from the workflow trigger and update the Send message step so that the message text looks like this
  10. Try publishing a message to your SNS topic; you should see the message text appear in your Slack channel

 

How it works:

When you create a webhook trigger for a Slack workflow, you can specify variables that pull values from the JSON request body. In our case, we’re pulling the “Message” and “SubscribeURL” properties from the request body. When we first create the HTTPS subscription, SNS sends a confirmation request to the endpoint with the following format:

JSON representing SNS confirmation message format

We use the “SubscribeURL” variable to capture the “SubscribeURL” property from the request body so that we can use it to confirm the subscription. Once our subscription is confirmed, we can remove the “SubscribeURL '' variable, as that is only sent with the confirmation request; normal SNS messages do not have that property. Anytime a message is published to the topic, SNS will send a request to the webhook with the following format: 

JSON representing SNS request format

Our “Message” variable grabs the value of the “Message” property from the SNS request and posts it to our Slack channel. If we want to grab other properties from this request, we just need to add that property name to workflow trigger variables and then insert the variable into the message text in the send message step.

Once the SNS to Slack pipeline is set up, you can send information from any SNS-supported AWS services to your Slack channel by having those services publish messages to your SNS topic. This can be used to send alerts, logs, or other messages to your team’s Slack channel, which gives all of your developers quick and easy access to this information. However, it is important to ensure that you are not including sensitive information in your SNS messages, as anyone who is able to access your developer Slack channel will be able to view this information. 

Ryan Shendler

Author Bio

Ryan Shendler is an associate software engineer at Jahnel Group, Inc., a custom software development firm based in Schenectady, NY. Jahnel Group is a consulting firm that specializes in helping companies leverage technology to improve their business operations. We provide end-to-end strategic consulting and deployment services, helping companies optimize their operations and reduce costs through the use of technology. Jahnel Group is an Advanced Tier AWS Services Partner, with expertise in AWS Lambdas, Amazon API Gateway, Amazon DynamoDB, and other AWS services.

Unlock Your Future!

Send us a message