alxolr

posts about software engineering craft

How to test locally AWS SQS queues in node.js

My most effective improvement strategies

Intro

Recently got a particular task to mock amazon environment locally. The problem is if you want to test your application you need to connect to Amazon SQS. We were developing in a team, and it was challenging to manage who is using the queues.

I found about ElasticMQ that it can mock AmazonSQS and use it locally, as an alternative.
We will build a simple project which uses ElasticMQ instead of Amazon SQS.

The source code for this tutorial can be found at elasticmq-node-tutorial

ElasticMQ

ElasticMQ is a message queue system, offering an actor-based Scala and an SQS-compatible REST (query) interface.

Docker Image

For this tutorial, you will need to have docker installed. Some links to install docker

Next, we will create a docker image with our queues configuration.
Create a folder sqs-mock with the following structure

sqs-mock/
  Dockerfile
  elasticmq.conf

The content of Dockerfile:

FROM s12v/elasticmq
COPY elasticmq.conf /etc/elasticmq/elasticmq.conf

The most import part is the elasticmq.conf file:

include classpath("application.conf")

queues {
  first-queue {
    defaultVisibilityTimeout = 30 seconds
    delay = 0 seconds
    receiveMessageWait = 0 seconds
    deadLetterQueue {
      name: "dead-first-queue"
      maxReceiveCount = 5
    }
  }
  dead-first-queue {
    defaultVisibilityTimeout = 30 seconds
    delay = 0 seconds
    receiveMessageWait = 0 seconds
  }

  second-queue {
    defaultVisibilityTimeout = 30 seconds
    delay = 0 seconds
    receiveMessageWait = 0 seconds
    deadLetterQueue {
      name: "dead-second-queue"
      maxReceiveCount = 5
    }
  }
  dead-second-queue {
    defaultVisibilityTimeout = 30 seconds
    delay = 0 seconds
    receiveMessageWait = 0 seconds
  }
}

Build the image and start the sqs server.

docker build -t sqs-mock .
docker run --name sqs-mock -p 9324:9324 -d sqs-mock

Now we should have a running elasticmq service, so the simplest thing that we can do is to list the existing working queues.

import { SQS } from 'aws-sdk';

const queue = new SQS({
  endpoint: 'http://localhost:9324',
  region: 'us-east-1', // it does not matter
});

(async () => {
  const queues = await queue.listQueues().promise();
  console.log(queues);
})();
# output
{
    ResponseMetadata:{
        RequestId:'00000000-0000-0000-0000-000000000000'
   },
   QueueUrls:[
        'http://localhost:9324/queue/dead-second-queue',
        'http://localhost:9324/queue/dead-first-queue',
        'http://localhost:9324/queue/second-queue',
        'http://localhost:9324/queue/first-queue'
   ]
}

Now we need to change the urls from amazon sqs to the new ones.


I hope that this article was helpful. If you like it, please share it with your friends and leave a comment; I will gladly answer all the questions.

Related articles

×