Search code examples
error-handlingaws-lambdaamazon-snsaws-step-functions

How to pass AWS Lambda error in AWS SNS notification through AWS Step Functions?


I have created an AWS Step Function which triggers a Lambda python code, terminates without error if Lambda succeeds, otherwise calls an SNS topic to message the subscribed users if the Lambda fails. It is running, but the message was fixed. The Step Function JSON is as follows:

{
  "StartAt": "Lambda Trigger",
  "States": {
    "Lambda Trigger": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-2:xxxxxxxxxxxx:function:helloworldTest",
      "End": true,
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "ResultPath": "$.error",
          "Next": "Notify Failure"
        }
      ]
    },
    "Notify Failure": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sns:publish",
      "Parameters": {
        "Message": "Batch job submitted through Step Functions failed with the following error, $.error",
        "TopicArn": "arn:aws:sns:us-east-2:xxxxxxxxxxxx:lambda-execution-failure"
      },
      "End": true
    }
  }
}

Only thing is, I want to append the failure error message to my message string, which I tried, but is not working as expected.

Eg: The step function run on failure

But I get a mail as follows:

Mail Received

How to go about it?


Solution

  • I could solve the problem using "Error.$": "$.Cause".

    The following is a working example of the failure portion of state machine:

    "Job Failure": {
                  "Type": "Task",
                  "Resource": "arn:aws:states:::sns:publish",
                  "Parameters": {
                    "Subject": "Lambda Job Failed",
                    "Message": {
                      "Alarm": "Lambda Job Failed",
                      "Error.$": "$.Cause"
                    },
                    "TopicArn": "arn:aws:sns:us-east-2:xxxxxxxxxxxx:Job-Run-Notification"
                  },
                  "End": true
                }
    

    Hope this helps!