node.jsamazon-ec2amazon-sqsdaemonidentity-management

How can I read a SQS message using sqs-consumer inside an EC2 instance?


I have a node.js. daemon running in an EC2 instance that uses sqs-consumer library to read messages from a SQS.

The problem I am having is that messages are written to the SQS but they are not consumed by my daemon. I am getting no error messages. The message simply stays at the SQS and my daemon seems to be not notified of the activity.

I have added a IAM role to the EC2 that gives full access to SQS and I have also setup both the AWS_ACCESS_KEY_ID and the AWS_SECRET_ACCESS_KEY on the EC2. I know that is not the problem because I have already solved that one (I was getting an error on SQS credentials before this).

This is the code I have for consuming the SQS (of course it works like a charm in localhost):

const consumer = Consumer.create({
  region: config.sqs.region,
  queueUrl: config.sqs.endpoint,
  shouldDeleteMessages: true,
  handleMessage: (message) => {
    console.log('Processing message: ', message);
  },
});

consumer.on('error', (err) => {
  console.log(err.message);
});

consumer.start();

What am I missing?


Solution

  • I was missing the assignment of the following permission policy to the EC2 instance:

    AmazonSQSFullAccess

    To do this you have to first create a role with that policy attached and then assign the created IAM role to the instance. If the instance is already running it is very important to reboot it. If not, it won't pick the new role.