Step Functions 入门,但我无法让 Choice 正常工作

Geo*_*eet 3 aws-lambda aws-step-functions

我正在修改我的第一步功能,作为一个新手,我正在努力使这项工作正常进行。AWS 上的文档很有帮助,但缺少我试图理解的示例。我在这里的网站上发现了几个类似的问题,但他们也没有真正回答我的问题。

我有一个非常简单的测试 Step Function。我有一个小的 Lambda 函数,它从 DynamoDB 中的请求中踢出带有“计数”的单行 JSON:

def lambda_handler(event, context):
    """lambda_handler

    Keyword arguments:
    event -- dict -- A dict of parameters to be validated.
    context -- 

    Return:
    json object with the hubID from DynamoDB of the new hub. 

    Exceptions:
    None
    """
    # Prep the Boto3 resources needed
    dynamodb        = boto3.resource('dynamodb')
    table           = dynamodb.Table('TransitHubs')

    # By default we assume there are no new hubs
    newhub = { 'Count' : 0 }

    # Query the DynamoDB to see if the name exists or not:
    response = table.query(
        IndexName='Status-index',
        KeyConditionExpression=Key('Status').eq("NEW"),
        Limit=1
    )

    if response['Count']:
        newhub['Count'] = response['Count']

    return json.dumps(newhub)
Run Code Online (Sandbox Code Playgroud)

正常的输出是:

{“计数”:1}

然后我创建了这个 Step Function:

{
  "StartAt": "Task",
  "States": {
        "Task": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-west-2:OMGSUPERSECRET:function:LaunchNode-get_new_hubs",
      "TimeoutSeconds": 60,
      "Next": "Choice"
    },
    "Choice": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.Count",
          "NumericEquals": 0,
          "Next": "Failed"
        },
        {
          "Variable": "$.Count",
          "NumericEquals": 1,
          "Next": "Succeed"
        }
      ]
    },
    "Succeed": {
      "Type": "Succeed"
    },
    "Failed": {
      "Type": "Fail"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

所以我启动了状态函数,我得到了这个输出:

任务状态退出

{
  "name": "Task",
  "output": {
    "Count": 1
  }
}
Run Code Online (Sandbox Code Playgroud)

选择状态进入

{
  "name": "Choice",
  "input": {
    "Count": 1
  }
}
Run Code Online (Sandbox Code Playgroud)

执行失败

{
  "error": "States.Runtime",
  "cause": "An error occurred while executing the state 'Choice' (entered at the event id #7). Invalid path '$.Count': The choice state's condition path references an invalid value."
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题:我不明白为什么这会因该错误消息而失败。选择不应该只是从 JSON 中获取该值吗?不是默认的“$”输入“输入”路径吗?

Geo*_*eet 5

我已经弄清楚问题是什么,这里是:

在我的 Python 代码中,我试图将 json.dumps(newhub) 用于响应,认为我需要的是表示 json 格式响应的字符串输出。但似乎这是不正确的。当我将代码更改为简单地“返回 newhub”并返回 DICT 时,步骤函数过程正确地接受了这一点。我假设它为我将 DICT 解析为 JSON?但是输出差异很明显:

上面的旧任务输出返回 json.dumps(newhub):

{
  "name": "Task",
  "output": {
    "Count": 1
  }
}
Run Code Online (Sandbox Code Playgroud)

从上面返回 newhub 的新任务输出:

{
  "Count": 1
}
Run Code Online (Sandbox Code Playgroud)

现在,Choice 正确匹配了我输出中的 Count 变量。