6

I wrote an AWS lambda function in NodeJs that pulls id's off a database and queues them onto a AWS SQS queue. I had it working fine until I deployed a function to receive the messages and now the original function is not working. Here is the relevant code:

    async function sendHelper(rows)
    {
        let i;
        let params;
        let res;
        let sqs = new AWS.SQS({apiVersion: '2012-11-05'});
        for (i = 0; i < rows.length; i++) { 
            params = {
                MessageBody: rows[i].ID.toString(),
                QueueUrl: 'https://sqs.' + process.env.AWS_REGION_NAME + '.amazonaws.com/' + process.env.AWS_ID_SHORT + '/' + process.env.SQS_VENDPERSON_QUEUE_NAME
            };

            res = await sqs.sendMessage(params).promise().catch(err => errorHandler(err));

            console.log(res);
        }

        return null;
    }

The issue is that it isn't throwing an error anywhere. It just times out on the first sendMessage no matter how long you give it. I have since removed the message receiving function and the issues persists. Some additional info that might help is I have been using serverless to deploy.

IBirle
  • 63
  • 1
  • 4
  • I might add that the queue can be added to fine with a test NodeJS file run locally. It is only on AWS that this issues exists. – IBirle Jan 05 '19 at 05:15

1 Answers1

3

You didn't include the rest of your lambda function but I assume your database instance is in a VPC. If so, you need to:

  1. Place your lambda inside your VPC
  2. Set up a VPC endpoint for SQS or NAT gateway (depending on whether you just need to access SQS only or also need to access the internet).

Key note (that I learned the hard way): When you add your lambda function to a subnet, make sure you ONLY add it to the private subnets, otherwise nothing will work.

It is going to take quite a bit of tinkering to get right, but it is doable. Here are some relevant docs:

https://docs.aws.amazon.com/lambda/latest/dg/vpc.html

and

https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/

Guillermo Alvarez
  • 1,695
  • 2
  • 18
  • 23
  • a more in-depth explanation of the above: https://stackoverflow.com/questions/52992085/why-cant-an-aws-lambda-function-inside-a-public-subnet-in-a-vpc-connect-to-the/52994841#52994841 – donbunkito May 25 '22 at 09:03