如何通过 AWS Step Functions 在 AWS SNS 通知中传递 AWS Lambda 错误?

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

我创建了一个 AWS Step Function,它会触发 Lambda python 代码,如果 Lambda 成功则无错误终止,否则如果 Lambda 失败则调用 SNS 主题向订阅用户发送消息。它是 运行,但消息已修复。阶跃函数JSON如下:

{
  "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
    }
  }
}

唯一的问题是,我想将失败的错误消息附加到我的消息字符串中,我试过了,但没有按预期工作。

但是我收到邮件如下:

怎么办?

此行已将异常对象附加到 'error' 路径。

 "ResultPath": "$.error"

我们只需将“$”传递给 Message.$ 传递给 SNS 任务,输入和错误详细信息都会发送到 SNS。

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

如果我们不希望在电子邮件中附加 Lambda 的输入,我们应该跳过 ResultPath 或只使用“$”作为 ResultPath,输入对象将被忽略。

"ResultPath": "$"

我可以使用 "Error.$": "$.Cause".

解决问题

以下是状态机故障部分的工作示例:

"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
            }

希望对您有所帮助!

这是代码的完整版本

{
  "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
    }
  }
}