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개 답변
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
전문가
답변함 2년 전
  • 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

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠