hitwill
hitwill

Reputation: 625

How do you throw a TaskFailed error inside a Lambda function called by a Step function

I have a lambda function invoked by a step function. Internally, the lambda calls an api and catches errors.

However, if the api call fails, I would like to throw/ return a TaskFailed error to the step function, so it can retry later. How would I go about this? (Or is there another way to get the step to retry itself later?)

Upvotes: 1

Views: 1008

Answers (1)

Balu Vyamajala
Balu Vyamajala

Reputation: 10333

Take this definition, if lambda throws exact error MY_CUSTOM_ERROR, it will be retried, also for Lambda errors, it will be retried. We can set different retry mechanisms for different error types.

{
  "StartAt": "invoke-lambda",
  "States": {
    "invoke-lambda": {
      "End": true,
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 6,
          "BackoffRate": 2
        },
        {
          "ErrorEquals": [
            "MY_CUSTOM_ERROR"
          ],
          "IntervalSeconds": 2
        }
      ],
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:111122223333:function:HelloWorld"
    }
  }
}

here is the Lambda code, we just need to pass error object to callback

  exports.handler = (event, context, callback) => {
    console.log('event',event)
    let e = new Error();
    e.name = 'MY_CUSTOM_ERROR';
    callback(e)
  }

Upvotes: 3

Related Questions