0

I am trying to properly structure my AWS lambda. I am attempting to exit the Lambda when I encounter an error. With the simple one function Lambda, I do this:

exports.handler = async (event, context) => {    
    const someError = new Error('Something is wrong');
    throw someError;
};

It ends up in DeadQueue as expected (after several tries).

When I want to resolve it with success, I do this:

exports.handler = async (event, context) => {  
    // ... some code here        
    return {};    
};

Now I want to structure my application using requires, so I have something like this:

//Main execution point
const validator = require('./Validate/Validator');

exports.handler = async (event, context) => {    
    let aaa = validator.validate(operationName);
    console.log('I should not bere here')        
};

And the validator itself:

exports.validate = async (schemaName, payload) => {
    console.log('I am coming here')
    try {            
        const Schema = require(`../ValidationSchemas/${schemaName}`).schema;
    }
    catch (e) {            
        const schemaError = new Error('Validation schema not found. Operation does not exist.');
        throw schemaError;
        //process.exit(0);            
    }
};

What happens in validator, if I throw an error, error is thrown, but my execution is continued in the main (caller) lambda function. I thought to stop it there using

process.exit(0)

It does work. Lambda is terminated. But it looks like a bad approach for some reason. Ideally I would do it from the main function, but I am thinking about the best approach.

1 Answer 1

1

You need to implement error handling in Lambda function.

const validator = require('./Validate/Validator');

exports.handler = (event) => {

  try {
    let aaa = validator.validate(operationName);
  }
  catch (error) {
    return error;
  }  

};
Sign up to request clarification or add additional context in comments.

5 Comments

If I understand correctly, I should handle all errors in my execution function?
It works. Based on your suggestion, I control the flow inside the main function. Side question. Is it ok (in Lambda) context to use this: process.exit(0)?
it depends on your use case why would you want to use process.exit. It would just terminate the container execution.
At the end using it, has same side effect like throwing an error? In terms, that it ends up in the DeadSQS?
@VedranMaricevic it should have the same ultimate effect but it's almost certainly going to come with a future performance penalty, because the process -- and possibly the container -- can't be reused. The process will need to be respawned or the container will need to be replaced, by the service, either of which can reasonably be expected to cause a future invocation of the function to incur some amount of unnecessary startup latency.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.