Aakash Basu
Aakash Basu

Reputation: 1767

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?

Upvotes: 1

Views: 4121

Answers (3)

Glib Martynenko
Glib Martynenko

Reputation: 338

Here is the full version of the code

{
  "Comment": "A Hello World example of the Amazon States Language using an AWS Lambda function",
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXXX:function:StepFunctionTest",
      "End": true,
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "NotifyFailure"
        }
      ]
    },
    "NotifyFailure": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sns:publish",
      "Parameters": {
        "Subject": "[ERROR]: Task failed",
        "Message": {
          "Alarm": "Batch job submitted through Step Functions failed with the following error",
          "Error.$": "$.Cause"
        },
        "TopicArn": "arn:aws:sns:us-east-1:XXXXXXXXXXXXX:Notificaiton"
      },
      "End": true
    }
  }
}

enter image description here

Upvotes: 1

Aakash Basu
Aakash Basu

Reputation: 1767

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!

Upvotes: 1

Balu Vyamajala
Balu Vyamajala

Reputation: 10333

This line is already appending exception object to 'error' path.

 "ResultPath": "$.error"

We just need pass '$' to Message.$ to SNS task, both input and error details will be sent to SNS.

{
   "TopicArn":"${SnsTopic}",
   "Message.$":"$"
}

if we don't want input to Lambda to be appended in email, we should skip ResultPath or have just '$' as ResultPath, input object is ignored.

"ResultPath": "$"

Upvotes: 0

Related Questions