AWS Step Function 工作流在执行时显示失败状态

AWS Step Function workflow is showing failed status when excuting

我正在学习 this 使用 AWS Step Functions 和 AWS Lambda 创建示例工作流程的教程。我已按照博客完成所有操作,但是当我 运行 它在“案件已解决”阶段显示失败状态。

截图: https://i.stack.imgur.com/ebmbb.png

我做了如下操作:

Step1:在 AWS 管理控制台中创建角色并复制角色 ARN 值。

Step2: 使用博客中提供的 ASL 代码创建了一个新的状态机,并添加了 Role ARN 值。

Step3:创建了所有 5 个 AWS Lambda 函数

Step4: 编辑了状态机并添加了 Lambda 函数的 Resource 值。

Step5:使用以下代码执行工作流:

{
    "inputCaseID": "001"
}

我在状态机中的 ASL 代码:

    {
      "Comment": "A simple AWS Step Functions state machine that automates a call center support session.",
      "StartAt": "Open Case",
      "States": {
        "Open Case": {
          "Type": "Task",
          "Resource": "arn:aws:lambda:us-east-2:AccountId:function:OpenCaseFunction",
          "Next": "Assign Case"
        }, 
        "Assign Case": {
          "Type": "Task",
          "Resource": "arn:aws:lambda:us-east-2:AccountId:function:AssignCaseFunction",
          "Next": "Work on Case"
        },
        "Work on Case": {
          "Type": "Task",
          "Resource": "arn:aws:lambda:us-east-2:AccountId:function:WorkOnCaseFunction",
          "Next": "Is Case Resolved"
        },
        "Is Case Resolved": {
            "Type" : "Choice",
            "Choices": [ 
              {
                "Variable": "$.Status",
                "NumericEquals": 1,
                "Next": "Close Case"
              },
              {
                "Variable": "$.Status",
                "NumericEquals": 0,
                "Next": "Escalate Case"
              }
          ]
        },
         "Close Case": {
          "Type": "Task",
          "Resource": "arn:aws:lambda:us-east-2:AccountId:function:CloseCaseFunction",
          "End": true
        },
        "Escalate Case": {
          "Type": "Task",
          "Resource": "arn:aws:lambda:us-east-2:AccountId:function:EscalateCaseFunction",
          "Next": "Fail"
        },
        "Fail": {
          "Type": "Fail",
          "Cause": "Engage Tier 2 Support."    }   
      }
    }

错误显示:

{
  "error": "States.Runtime",
  "cause": "An error occurred while executing the state 'Is Case Resolved' (entered at the event id #17). Invalid path '$.Status': The choice state's condition path references an invalid value."
}

OpenCaseFunction

exports.handler = (event, context, callback) => {
    // Create a support case using the input as the case ID, then return a confirmation message   
   var myCaseID = event.inputCaseID;
   var myMessage = "Case " + myCaseID + ": opened...";   
   var result = {Case: myCaseID, Message: myMessage};
   callback(null, result);    
};

AssignCaseFunction

exports.handler = (event, context, callback) => {    
    // Assign the support case and update the status message    
    var myCaseID = event.Case;    
    var myMessage = event.Message + "assigned...";    
    var result = {Case: myCaseID, Message: myMessage};
    callback(null, result);        
};

WorkOnCaseFunction

exports.handler = (event, context, callback) => {    
    // Generate a random number to determine whether the support case has been resolved, then return that value along with the updated message.
    var min = 0;
    var max = 1;    
    var myCaseStatus = Math.floor(Math.random() * (max - min + 1)) + min;
    var myCaseID = event.Case;
    var myMessage = event.Message;
    if (myCaseStatus == 1) {
        // Support case has been resolved    
        myMessage = myMessage + "resolved...";
    } else if (myCaseStatus == 0) {
        // Support case is still open
        myMessage = myMessage + "unresolved...";
    } 
    var result = {Case: myCaseID, Status : myCaseStatus, Message: myMessage};
    callback(null, result); 
};

EscalateCaseFunction

exports.handler = (event, context, callback) => {    
    // Escalate the support case 
    var myCaseID = event.Case;    
    var myCaseStatus = event.Status;    
    var myMessage = event.Message + "escalating.";    
    var result = {Case: myCaseID, Status : myCaseStatus, Message: myMessage};
    callback(null, result);
};

CloseCaseFunction

exports.handler = (event, context, callback) => { 
    // Close the support case    
    var myCaseStatus = event.Status;    
    var myCaseID = event.Case;    
    var myMessage = event.Message + "closed.";    
    var result = {Case: myCaseID, Status : myCaseStatus, Message: myMessage};
    callback(null, result);
};

我错过了什么?

试试这个 - 它工作完美并且已经过多次测试。

它很相似 - 但不同之处在于 Lambda 函数是使用 Java 运行-time API 编写的。它还向您展示了如何从 Lambda 函数调用其他 AWS 服务(例如 Amazon DynamoDB):

Create AWS serverless workflows by using the AWS SDK for Java

命名的 WorkOnCaseFunction 没有产生正确的输出,应该类似于下面的结构。

{ Case: '001', Status: 0, Message: 'hellounresolved...' }

它被 choice 状态消耗,该状态试图使用 jsonpath $.Status.

获取 Status

如果它不存在,则会出错并取消状态机的进一步执行。

For example:

下面是我的状态机 without DefaultState 用于 Choice 状态,FailSuccess 取决于生成的输出 function:mytestfunction 可以是 01.

{
  "Comment": "An example of the Amazon States Language using a choice state.",
  "StartAt": "FirstState",
  "States": {
    "FirstState": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:eu-central-1:1234567890:function:mytestfunction",
      "Next": "ChoiceState"
    },
    "ChoiceState": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.Status",
          "NumericEquals": 1,
          "Next": "Success"
        },
        {
          "Variable": "$.Status",
          "NumericEquals": 0,
          "Next": "Failed"
        }
      ]
    },
    "Success": {
      "Type": "Pass",
      "Comment": "Success",
      "End": true
    },
    "Failed": {
      "Type": "Pass",
      "Comment": "Failed",
      "End": true
    }
  }
}

function:mytestfunction代码

exports.handler = (event, context, callback) => {    
    // Generate a random number to determine whether the support case has been resolved, then return that value along with the updated message.
    var min = 0;
    var max = 1;    
    var myCaseStatus = Math.floor(Math.random() * (max - min + 1)) + min;
    var myCaseID = "001";
    var myMessage = "hello";
    if (myCaseStatus == 1) {
        // Support case has been resolved    
        myMessage = myMessage + "resolved...";
    } else if (myCaseStatus == 0) {
        // Support case is still open
        myMessage = myMessage + "unresolved...";
    } 
    var result = {Case: myCaseID, Status : myCaseStatus, Message: myMessage};
    console.log('result: %s', result);
    callback(null, result); 
};

我刚刚添加了一条 log 消息返回到 stepfunction,另外为了测试我将 messagecase 设为静态。

这给了我这样的输出

然后 lambda 的这个输出被 Choice 状态消耗,它试图从 function:mytestfunction 产生的以下 json 中只获取 Status

{ Case: '001', Status: 0, Message: 'hellounresolved...' }

现在您可以看到名为 WorkOnCaseFunction 的 lambda 出了什么问题。它没有以上述格式生成输出,并且您的状态机出现故障。