AWS Step Function 工作流程在执行

问题描述

我正在按照 this 教程使用 AWS Step Functions 和 AWS Lambda 创建示例工作流程。我已经按照博客完成了所有操作,但是当我运行它时,它在“Is Case Resolved”阶段显示失败状态。

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

我做了如下:

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

Step2:使用博客中提供的 ASL 代码创建了一个新的状态机并添加了角色 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,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,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,result); 
};

EscalateCaseFunction

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

CloseCaseFunction

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

我缺少什么?

解决方法

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

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

它被 choice 状态消耗,该状态尝试使用 Status jsonpath 获取 $.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 设为静态。

这给了我这样的输出

enter image description here

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

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

enter image description here

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

,

试试这个 - 它运行完美并经过多次测试。

enter image description here

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

Create AWS serverless workflows by using the AWS SDK for Java