Files
oam/knowledge base/cloud computing/aws/sns.md
2025-02-03 23:20:08 +01:00

5.3 KiB

Simple Notification Service

Managed pub/sub message delivery service.

  1. TL;DR
  2. Connect a Slack channel to SNS
  3. Further readings
    1. Sources

TL;DR

Publishers (or producers) send messages to a topic.
Topics are logical access points acting as communication channels for multiple endpoints like SQS, emails, lambda, and others.

SNS can be also used to send notifications to HTTP(S) endpoints such as Webhook URLs.
Anyway, SNS does not support sending requests using JSON key-value pairs, which some webhooks require (i.e. Slack).

Subscribers (or consumers) subscribe to the topic and receive published messages using a supported endpoint type.

Standard topics
  • Do not enforce strict message ordering, grouping, nor deduplication.
    Consumers of the queue may receive messages out of order, and more than once.
  • Support all delivery protocols.
FIFO topics
  • Integrate with FIFO queues in SQS.
  • Do enforce strict message ordering, grouping, and deduplication.
    They always deliver messages to subscribed SQS queues in the exact order in which the messages are published to the topic, and only once.
  • Do ensure strict message ordering, message grouping, and deduplication.
  • Allowing FIFO and standard queues to subscribe for message processing.

By default, only the topic's owner can publish or subscribe to the topic.
Configure additional access permissions by expanding the topic's Access policy.

Usage
# List topics.
aws sns list-topics

# Get information about topics.
aws sns get-topic-attributes --topic-arn 'arn:aws:sns:eu-west-1:012345678901:aSucculentTopic'

# List subscriptions.
aws sns list-subscriptions
aws sns list-subscriptions --query 'Subscriptions'
aws sns list-subscriptions-by-topic --topic-arn 'arn:aws:sns:eu-west-1:012345678901:aSucculentTopic'

# Get information about subscriptions.
aws sns get-subscription-attributes \
  --subscription-arn 'arn:aws:sns:eu-west-1:012345678901:aSucculentTopic:abcdef01-2345-6789-abcd-ef0123456789'

Connect a Slack channel to SNS

Refer How to Connect AWS SNS to Slack using Webhooks: Easy Step-by-Step Explanation.

SNS does not currently support integrating directly with third-party applications.
Leverage Slack's incoming webhooks or create a Chatbot configuration for the destination Slack channel.

Webhook

SNS does not currently support sending requests using JSON key-value pairs, and Slack's webhooks require JSON requests to include a message string as the value of the text key.
To solve this, use a Lambda function to modify the SNS message's body JSON document for the webhook endpoint.

Procedure:

  1. Ensure the existence of a Slack incoming webhook to send requests to.
  2. Create a topic.
    The standard type is usually enough.
  3. Create a Lambda function.
    And test it works.
  4. Add a topic trigger to the function.
Chatbot
  1. Ensure the existence of a Slack incoming webhook to send requests to.

  2. Create a topic.
    The standard type is usually enough.

  3. Create a IAM Role for the Chatbot configuration to use.

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Principal": {
                    "Service": "chatbot.amazonaws.com"
                },
                "Action": "sts:AssumeRole"
            }
        ]
    }
    
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "Autogenerated-AWS-Chatbot-NotificationsOnly-Policy",
                "Effect": "Allow",
                "Action": [
                    "cloudwatch:Describe*",
                    "cloudwatch:Get*",
                    "cloudwatch:List*"
                ],
                "Resource": "*"
            }
        ]
    }
    
  4. Create a Chatbot configuration for the destination Slack channel.

Further readings

Sources