【问题标题】:Getting started with Step Functions and I can't get Choice to work correctly开始使用 Step Functions,我无法让 Choice 正常工作
【发布时间】:2020-12-09 05:04:15
【问题描述】:

我正在修改我的第一步功能,作为一个新手,我正在努力使这项工作正确。 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)

正常的输出是:

{ "计数": 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"
    }
  }
}

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

TaskStateExited

{
  "name": "Task",
  "output": {
    "Count": 1
  }
}

ChoiceStateEntered

{
  "name": "Choice",
  "input": {
    "Count": 1
  }
}

执行失败

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

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

【问题讨论】:

  • 在执行失败的步骤函数控制台中,当您单击Task Lambda,然后单击“输出”选项卡时,将显示 Lambda 的实际输出。这是否显示了您期望的结构?
  • 谢谢,这部分是导致我弄清楚这一点的原因。我在下面记录了我的问题的答案。

标签: aws-lambda aws-step-functions


【解决方案1】:

我已经找出问题所在,这里是:

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

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

{
  "name": "Task",
  "output": {
    "Count": 1
  }
}

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

{
  "Count": 1
}

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

【讨论】:

  • 谢谢,正是这样做的:D
【解决方案2】:

如果这对其他人有帮助。我也遇到了你做的那种错误(你有下面......只是复制粘贴你的......)

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

但是当我同时丢失了"Count" 键时,我的问题就出现了。

但我不想要详细的有效负载。

但每次阅读these docs 我发现我也可以做到...

"Choice": {
"Type": "Choice",
"Choices": [
  {
    "And": [
      {
          "Variable": "$.Count",
          "IsPresent": true
          },
      {
          "Variable": "$.Count",
          "NumericEquals": 0,
          }
      ],
    "Next": "Failed"
    },
  {
    "And": [
      {
          "Variable": "$.Count",
          "IsPresent": true
          },
      {

          "Variable": "$.Count",
          "NumericEquals": 1,
          }
      ],
    "Next": "Succeed"
    }
  ]
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2017-11-14
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    相关资源
    最近更新 更多