Bootstrap FreeKB - Amazon Web Services (AWS) - Subscribe to a Simple Notification Service (SNS) Topic using the AWS CLI
Amazon Web Services (AWS) - Subscribe to a Simple Notification Service (SNS) Topic using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

A Simple Notification Service (SNS) Topic can be used to create notification alerts, such as an email notification. This uses the messaging services pub sub model.

  • pub (publish) - create a new alert in a Topic
  • sub (subscribe) - get alerts in a Topic

Before setting up sub (subscribers), you will to create an SNS Topic. Check out my article on Creating a Simple Notification Service (SNS) Topics using the AWS CLI.

~]$ aws sns create-topic --name my-topic 
{
    "TopicArn": "arn:aws:sns:us-east-1:123456789012:my-topic"
}

 

There are various types of subscriptions.

  • application - messages published to the topic will be delivered to an EndpointArn for a mobile app or device - the message must be JSO
  • email - messages published to the topic will be emailed to the subscriber
  • email-json - messages published to the topic will be emailed to the subscriber - the message must be JSON
  • firehouse - messages published to the topic will be delivered to an AWS Kinesis Data Firehose delivery stream - the message must be JSON
  • http - messages published to the topic will be POST to an HTTP url - the message must be JSON
  • https - messages published to the topic will be POST to an HTTPS url - the message must be JSON
  • lambda - messages published to the topic will be sent to a Lambda Function
  • sms - messages published to the topic will be published to an SMS device (e.g. cellphone)
  • Simple Queue Service (SQS) - messages published to the topic will be put onto a SQS queue

 

For example, the aws sns subscribe command can be used to subscribe an email to the topic.

 ~]$ aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic --protocol email --notification-endpoint john.doe@example.com
{
    "SubscriptionArn": "pending confirmation"
}

 

Or, the aws sns subscribe command can be used to subscribe a Lambda Function to the topic.

~]$ aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic --protocol lambda --notification-endpoint arn:aws:lambda:us-east-1:123456789012:function:my-lambda-function
{
    "SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:my-topic:e68a4a47-ce56-48a5-865d-31f5920c4355"
}

 

This creates a Lambda Trigger. For example, over in the AWS Lambda console, you should have an SNS trigger with your Lambda Function.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 613a64 in the box below so that we can be sure you are a human.