Using Dead Letter Queues in Amazon SQS - AWS SDK for Go (version 1)

We announced the upcoming end-of-support for AWS SDK for Go V1. We recommend that you migrate to AWS SDK for Go V2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

Using Dead Letter Queues in Amazon SQS

This AWS SDK for Go example shows you how to configure source Amazon SQS queues that send messages to a dead letter queue.

You can download complete versions of these example files from the aws-doc-sdk-examples repository on GitHub.

Scenario

A dead letter queue is one that other (source) queues can target for messages that can’t be processed successfully. You can set aside and isolate these messages in the dead letter queue to determine why their processing didn’t succeed. You must individually configure each source queue that sends messages to a dead letter queue. Multiple queues can target a single dead letter queue.

The code uses this method of the Amazon SQS client class:

Prerequisites

Configure Source Queues

After you create a queue to act as a dead letter queue, you must configure the other queues that route unprocessed messages to the dead letter queue. To do this, specify a redrive policy that identifies the queue to use as a dead letter queue and the maximum number of receives by individual messages before they are routed to the dead letter queue.

Create a new Go file with the name DeadLetterQueue.go.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

import ( "encoding/json" "flag" "fmt" "strings" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sqs" )

Get the name of the queue and the dead letter queue from the commmand line.

queue := flag.String("q", "", "The name of the queue") dlQueue := flag.String("d", "", "The name of the dead-letter queue") flag.Parse() if *queue == "" || *dlQueue == "" { fmt.Println("You must supply the names of the queue (-q QUEUE) and the dead-letter queue (-d DLQUEUE)") return }

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials and the default AWS Region from ~/.aws/config.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, }))

Create a service client and call GetQueueUrl to get the URL for the queue.

svc := sqs.New(sess) result, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{ QueueName: queue, })

The URL of the queue is in the QueueUrl property of the returned object.

queueURL := result.QueueUrl

Similarly, get the URL of the dead letter queue.

Create the ARN of the dead-letter queue from the URL.

parts := strings.Split(*queueURL, "/") subParts := strings.Split(parts[2], ".") arn := "arn:aws:" + subParts[0] + ":" + subParts[1] + ":" + parts[3] + ":" + parts[4]

Define the redrive policy for the queue.

policy := map[string]string{ "deadLetterTargetArn": *dlQueueARN, "maxReceiveCount": "10", }

Marshal the policy to use as input for the SetQueueAttributes call.

b, err := json.Marshal(policy)

Set the policy on the queue.

_, err = svc.SetQueueAttributes(&sqs.SetQueueAttributesInput{ QueueUrl: queueURL, Attributes: map[string]*string{ sqs.QueueAttributeNameRedrivePolicy: aws.String(string(b)), }, })

See the complete example on GitHub.