Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here are the steps to use sqs-consumer to read a SQS message within an EC2 instance:

  1. Install Node.js on your EC2 instance.

  2. Create a new directory and initialize a Node.js project using the command npm init.

  3. Install the sqs-consumer package using the command npm install sqs-consumer.

  4. Create a file named consumer.js and add the following code:

const Consumer = require('sqs-consumer');

const app = Consumer.create({
  queueUrl: 'YOUR_QUEUE_URL',
  handleMessage: (message, done) => {
    console.log(message.Body);
    done();
  }
});

app.start();
  1. Replace YOUR_QUEUE_URL with the URL of your SQS queue.

  2. Save the file.

  3. Run the command node consumer.js to start the consumer.

The handleMessage function in the code above defines what to do with each message received from the SQS queue. In this case, it simply logs the message body to the console. You can modify this function to perform any other action you need.

Note that you may need to configure permissions for your EC2 instance to access the SQS queue depending on your setup.