h2ik Hell If I Know

GoLang AWS SQS Poller

I recently took up learning Go. While trying to find something to rewrite I go, I noticed at work that we had a poller running in ruby that would take messages out of a SQS queue and pass them along to another server.

This small project seemed like the best thing to rewrite. It is a considerably small service and I was able to map out everything in about 2 hours.

What I ended up with was go-sqs-poller. It’s rather simple, and just polls a SQS and when a message it received, just hands it off to a handler function.

It couldn’t be simpler to setup and run as well

package main

import (
	"fmt"

	
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/sqs"
	"github.com/aws/aws-sdk-go/aws/credentials"

	"github.com/h2ik/go-sqs-poller/worker"
)

var (
	aws_access_key string
	aws_secret_key string
	aws_region     string
)

func main() {

	// create a config
	aws_config := &aws.Config{
		Credentials: credentials.NewStaticCredentials(aws_access_key, aws_secret_key, ""),
		Region:      aws.String(aws_region),
	}

	// create the new client with the aws_config passed in
	svc, url := worker.NewSQSClient("go-webhook-queue-test", aws_config)
	// set the queue url
	worker.QueueURL = url
	// start the worker
	worker.Start(svc, worker.HandlerFunc(func(msg *sqs.Message) error {
		fmt.Println(aws.StringValue(msg.Body))
		return nil
	}))
}

This example will just print the message to Standard out but you could do anything you want with it.

I hope someone will find this useful, In know I’ve learned a lot by writing it.