Handle errors in lambda hapening on the init phase

0

Hi team,

I have set the connection opening code for my Redis cluster outside the event handler code in the lambda,

I would like to handle any (Redis connection) error(happening outside the lambda handler function) like I handle any other error happening inside the lambda handler code.

so that the lambda doesn't fail and the caller did not get InternalServerError but gets a proper response (StatusCode, errorCode, message...).

I added in the Redis connection code the error catch/ handling (on the init phase outside the handler function):

return {
    statusCode:"500",
    headers: {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "*",
    },
    body: JSON.stringify(MyresponseErrorObject),
  };

but lambda still fails and I did not get that error response object I set on the catch part.

is there a way to return a response back when any error happens on the init phase of lambda and make lambda not fail but return the error object?

1 Answer
0

Take a look at the Example app.py in this tutorial - https://docs.aws.amazon.com/lambda/latest/dg/services-rds-tutorial.html for sample code in Python.

This example is specific to RDS, but you will need to apply the same principle to Redis.

Note how a sys.exit() is called if the connection code results in an exception. It makes no sense to keep the lambda instance alive if the connection could not even be established. Then that particular lambda instance will keep receiving new events and they will all fail. It is better to start a new lambda instance with a new event, just in case it is able to establish the connection and process new events.

profile pictureAWS
EXPERT
answered a year ago
  • Thank you for your answer,

    my code is in nodeJs not in python, so I did process.exit(); instead of sys.exit() but the lambda still fail and no custom error returned

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions