0

I have node/express apis project. from node/express, I connect to S3 bucket/ RDS mysql to perform different operations.

local

In local machine, everything works fine. No exception, no error !! It always return data. I performed different operations thousand times and never got a single error.

AWS Once local development is done, I move all apis to one AWS lambda function. In other words, entire node/express apis project to one AWS lambda function.

Here is my tech stack at cloud side:

HTTP API Gateway <=> Lambda function <=> S3 bucket
                                                  ||
                                                  ||
                                                    =========> RDS MySQL

FE makes API calls, it goes to HTTP API to Lambda to S3 bucket/Mysql.

As I mentioned, It works absolutely fine in local but on AWS side I get intermittent error Unhandled Promise Rejection

I have one api which gets me some data from S3 bucket. When I call it, it fetches data successfully and then after that when I make other api call, SOMETIMES ONLY, it throws promise rejection error. IT IS NOT EVEN HITTING THE NEXT CONTROLLER as shown below.

enter image description here

As you can see in pic, API request executed successfully. And then, form UI when I make other API call, it fails sometimes.

YES, I use promise to connect to S3 bucket.

S3-Service.js

const AWS = require("aws-sdk");
const environment = require("../../config");

AWS.config.update({
  accessKeyId: environment.aws.accessKeyId,
  secretAccessKey: environment.aws.secretAccessKey,
  region: environment.aws.region
});

const S3 = new AWS.S3();

const getBufferFromS3 = (file) => {
  const bucketName = environment.S3.bucketName;
  const objectKey = file;
  return S3.getObject({ Bucket: bucketName, Key: objectKey }).promise();
};

const listBucketContent = (filePath, bucketName) => {
  const params = { Bucket: bucketName, Prefix: filePath };
  return S3.listObjects(params).promise();
}

 ...

As I mentioned, yes exception handling is added but still I can't seem to capture this failure in it. Trying to resolve since past few days but I really have no idea how to get to the problem.

NOTE: It usually occurs after some operation on S3 bucket.

micronyks
  • 54,797
  • 15
  • 112
  • 146
  • Given that your Lambda function connects to an RDS instance, I presume the Lambda function is connected to your VPC. When you configured the subnets for the Lambda function, did you indicate multiple subnets and are they a mix of private and public? Make them all/both private. – jarmod Apr 19 '23 at 13:08
  • Yes we are using VPC and I guess private subnets (not sure though). I can confirm in sometime. Assume, If it make all subnets private, what difference does it make then ? will it solve the problem ? – micronyks Apr 19 '23 at 13:11
  • Private subnets ensures that your network requests from the Lambda function route via NAT/IGW (assuming you have those vs. private VPC endpoints). Insufficient information in your post to determine what the root cause is but review [this post](https://stackoverflow.com/questions/40500490/what-is-an-unhandled-promise-rejection) on unhandled promise rejection. – jarmod Apr 19 '23 at 13:26
  • Yes all subnets are private. – micronyks Apr 19 '23 at 13:37
  • Also expand the REPORT log to check memory used. If it's >= configured memory size then increase RAM available to the Lambda function. But, I think your best bet is to correct the error handing as that will surface the stack trace and possible root cause error message. – jarmod Apr 19 '23 at 13:43
  • I did increase the memory size and it didn't help me. Tried many other things. I have exception handling mechanism also which doesn't capture `this specific error` and I don't understand why. – micronyks Apr 19 '23 at 14:05
  • Challenge is that your post contains none of the Lambda-specific code or exception handling so we can't help review it. – jarmod Apr 19 '23 at 14:21
  • Yes I agree and understand it but it is not even small piece of code. Also, I can't post it here but the problem is once one specific api (calling S3 services functions) is called, then when I try to execute other api, request doesn't even hit the controller and instead throws mentioned error. Adding logs in each line to understand where it fails but no luck. – micronyks Apr 19 '23 at 14:34

0 Answers0