List Queues in AWS SQS using Go
development golang aws sqs- Create Queues in AWS SQS using Go
- List Queues in AWS SQS using Go
- Delete Queues in AWS SQS using Go
- Send Messages to AWS SQS using Go
- Receive Messages from AWS SQS using Go
In order to work with AWS SQS using Go, we will use github.com/aws/aws-sdk-go-v2
. In this gist, we will use the aws-sdk-go-v2 package to list queues in AWS SQS. The complete source code for the snippet is available here https://github.com/abvarun226/blog-source-code/tree/master/list-queues-in-aws-sqs-using-go
In order to test the snippet, we will use localstack to run SQS emulator in a docker container on the laptop. The container exposes SQS endpoint on port 4566
. The snippet will then list all queues in the AWS SQS account.
In order to start the localstack
container, you will need to first install localstack:
$ pip install localstack
Localstack provides a fully functional local AWS cloud stack. It is a cloud service emulator that runs in a single container on a laptop or a development machine.
In order to start the AWS services, run:
$ localstack start -d
You can check the status of the services by running:
$ localstack status services
package main
import (
"context"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
)
type SQSQueueAPI interface {
ListQueues(ctx context.Context,
params *sqs.ListQueuesInput,
optFns ...func(*sqs.Options)) (*sqs.ListQueuesOutput, error)
}
func ListQueues(c context.Context, api SQSQueueAPI, input *sqs.ListQueuesInput) (*sqs.ListQueuesOutput, error) {
return api.ListQueues(c, input)
}
// creates an sqs client.
func client(ctx context.Context, awsURL, region string) *sqs.Client {
// customResolver is required here since we use localstack and need to point the aws url to localhost.
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: awsURL,
SigningRegion: region,
}, nil
})
// load the default aws config along with custom resolver.
cfg, err := config.LoadDefaultConfig(ctx, config.WithEndpointResolverWithOptions(customResolver))
if err != nil {
log.Fatalf("configuration error: %v", err)
}
return sqs.NewFromConfig(cfg)
}
// list all queues in AWS SQS account.
func listQueues(ctx context.Context, c *sqs.Client) {
inputList := &sqs.ListQueuesInput{}
resultList, err := ListQueues(ctx, c, inputList)
if err != nil {
log.Printf("error retrieving queue URLs: %v", err)
return
}
for i, url := range resultList.QueueUrls {
log.Printf("%d: %s", i+1, url)
}
}
func main() {
ctx := context.TODO()
awsURL := "http://127.0.0.1:4566"
awsRegion := "us-west-2"
// create aws client.
c := client(ctx, awsURL, awsRegion)
// list all queues in the SQS account.
listQueues(ctx, c)
}
Output:
➜ export AWS_SECRET_ACCESS_KEY=foobar
➜ export AWS_ACCESS_KEY_ID=foobar
➜ go run main.go
2022/09/27 23:01:28 1: http://127.0.0.1:4566/000000000000/first-queue
2022/09/27 23:01:28 2: http://127.0.0.1:4566/000000000000/second-queue
2022/09/27 23:01:28 3: http://127.0.0.1:4566/000000000000/third-queue
Related Posts
Validate email address in GoListing Files in AWS S3 using Go
Ordered Printing With Goroutines In Go
List Queues in AWS SQS using Go
Checksum Validation In Go