AWS in GO (Part 1 - SNS)

AWS Series

In the next 3-5 articles I will talk about creating and deploying gocode that uses or talks to the Amazon cloud services. I believe that AWS is the top of the line cloud services and so I have made that the top topic of my first few gocodecloud posts. Please share your thoughts with me about these posts on Facebook or twitter.

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 many 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. Place your key and secret in your .aws/credentials file []
  3. Use GoLang 1.5+ []
  4. Set your GOPATH []
  5. Install Amazon Core and SNS libraries
    • go get
    • go get
    • go get
  6. Create a AwsSns.go file (Code Explained below)

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("message"), // This is the message itself (can be XML / JSON / Text - anything you want)
		TopicArn: aws.String("arn:aws:sns:us-east-1:478989820108:MCP_DEV_CATEGORY_EX_TOPIC"),  //Get this from the Topic in the AWS console.

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

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

	// Pretty-print the response data.

*** Sign up for my email list to keep in touch with all the interesting new happenings in the go community with the GolangNewsFeed

comments powered by Disqus