Set up AWS SQS for Queuing and Consumption from Node.js

Vivek Kaushal
AWS in Plain English
3 min readJun 13, 2021

--

SQS stands for Simple Queuing Service — an offering from AWS which is simple to use, highly scalable to build upon and reliable.

In this post, I’ll walk through setting up SQS for queueing and consumption from Node.js.

Create AWS Account

Create and account on AWS, sign in to the console and find SQS.

Create Queue

Click on the Create Queue button, choose a name – we’ll call our queue sample-queue – and continue with the default pre-filled configurations. Click on the Create Queue button at the bottom of the page. Done! Your queue would be created and you’ll see a screen like the one attached below.

Create Queue User

It’s not a good practice to use the root user of your AWS account as the owner of resources which are accessed by external applications, like here in this scenario. Hence, we’ll create a new user specifically for our SQS queue and assign it to our newly created queue.

  • Search for IAM on AWS search bar, click on it
  • On the IAM dashboard, click on Users in the left panel, under Access Management.
  • Click on Add New User Button
  • On the screen that opens, set the user name — we’ll use sqs-user – and provide Programmatic Access.
  • On the permissions screen, choose Attach existing policies directly and choose the AmazonSQSFullAccess policy.
  • Skip the Add Tags (Optional) page that opens.
  • Click on Create user.
  • Copy the ACCESS KEY ID and SECRET ACCESS KEY from the screen that opens. Note: the secret access key will ONLY be visible on this screen.
  • Click on close. In the screen that opens next, click on the created user and copy the User ARN.

Assign User to Queue

  • Go to SQS dashboard on AWS Console.
  • Click on the queue you want to assign the created user to.
  • Go to the Access Policy Tab and click on Edit.
  • Replace the Principal->AWS value to the ARN you copied in the previous step, then click SAVE.

Writing to Queue from Node.js

You write to your newly created SQS queue from Node.js by building upon the tiny sample shared in the code below:

Note that there is no fixed format or structure that your message should follow. Choose whatever structure works best for your use-case.

Consuming from Queue using Node.js

You can write a program that consumes messages from your SQS queue as follows:

Conclusion

And that’s it! You’re done. You can also additionally add a Dead-Letter Queue for your created queue, which stores and re-tries messages which throw error upon consumption.

Hope this article helps! Be sure to let us know your thoughts in the comments.

More content at plainenglish.io

--

--