What is Amazon SNS?
Amazon Simple Notification Service (Amazon SNS) is a web service that coordinates and manages the delivery or sending of messages to subscribing endpoints or clients. In Amazon SNS, there are two types of clients—publishers and subscribers—also referred to as producers and consumers. Publishers communicate asynchronously with subscribers by producing and sending a message to a topic, which is a logical access point and communication channel. Subscribers (i.e., web servers, email addresses, Amazon SQS queues, AWS Lambda functions) consume or receive the message or notification over one of the supported protocols (i.e., Amazon SQS, HTTP/S, email, SMS, Lambda) when they are subscribed to the topic.
When using Amazon SNS, you (as the owner) create a topic and control access to it by defining policies that determine which publishers and subscribers can communicate with the topic. A publisher sends messages to topics that they have created or to topics they have permission to publish to. Instead of including a specific destination address in each message, a publisher sends a message to the topic. Amazon SNS matches the topic to a list of subscribers who have subscribed to that topic and delivers the message to each of those subscribers. Each topic has a unique name that identifies the Amazon SNS endpoint for publishers to post messages and subscribers to register for notifications. Subscribers receive all messages published to the topics to which they subscribe, and all subscribers to a topic receive the same messages.
Publish to SNS With Golang
Many times in the course of writing code we need to send async messages either to other applications or other parts of our application. In Development as well as in production there are a variety of popular systems such as RabbitMQ, JBossMQ and much more. In our case, we adopted Amazon SNS/SQS systems so that we do not have to worry about the hardware infrastructure. We simply create queues and start using them. This post will show you how to write a sample application to send messages to an SNS topic. If you don’t already have an amazon AWS account I would highly recommend that you create one so you can follow along.
Steps to publish an SNS message on an Amazon SNS Topic
- An AWS Account to set up an SNS topic and 1 queue that receives from the topic you just created.
- Use GoLang 1.5+ [https://golang.org/doc/install]
- Set your GOPATH [https://golang.org/doc/install#install]
- Install Amazon Core and SNS libraries
- go get github.com/aws/aws-sdk-go/aws
- go get github.com/aws/aws-sdk-go/aws/session
- go get github.com/aws/aws-sdk-go/service/sns
- Create AmazonWebServicesSNS.go file
In order to send and receive messages in AWS, you need to setup an SNS Topic and an SQS queue to receive the messages then you need to subscribe your queue to the topic you created. This effectively is all you need to do. Below are some screen shots that show you how I set up my Topic/Queue for the test code below.
Complete Application code to send an SNS Message
- In order to publish messages with this code topic policy should be edited as follow