Go Language

AWS in GO (Part 1 – SNS)

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

  1. An AWS Account to set up an SNS topic and 1 queue that receives from the topic you just created.
  2. Use GoLang 1.5+ [https://golang.org/doc/install]
  3. Set your GOPATH [https://golang.org/doc/install#install]
  4. 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
  5. Create AmazonWebServicesSNS.go file

AWS Setup

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


package main

import (

func main() {
         //Create a session object to talk to SNS (also make sure you have your key and secret setup in your .aws/credentials file)
	svc := sns.New(session.New())
         // params will be sent to the publish call included here is the bare minimum params to send a message.
	params := &sns.PublishInput{
		Message: aws.String("sample message"), // This is the message itself (can be XML / JSON / Text - anything you want)
		TopicArn: aws.String("arn:aws:sns:us-west-1:948333390847:Topic1"),  //Get this from the Topic in the AWS console.

	resp, err := svc.Publish(params)   //Call to publish the message

	if err != nil {                    
		// Print the error, cast err to awserr.Error to get the Code and
		// Message from an error.

	// Pretty-print the response data.

  • In order to publish messages with this code topic policy should be edited as follow



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s