Here are the steps to use sqs-consumer to read a SQS message within an EC2 instance:
Install Node.js on your EC2 instance.
Create a new directory and initialize a Node.js project using the command npm init
.
Install the sqs-consumer
package using the command npm install sqs-consumer
.
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();
Replace YOUR_QUEUE_URL
with the URL of your SQS queue.
Save the file.
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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-08 08:28:20 +0000
Seen: 9 times
Last updated: May 08 '23
How can I obtain the error message in Angular 4 when using the subscribe method?
What is the method of focusing on a specific ESLint error message in the scripts of my package.json?
What does the message "CopyPlistFile duplicated task in Xcode 10" signify?
How can I compose an email with HTML formatting using TextBox?
What does the error message "Import 'pandas' could not be resolved from source Pylance" mean?