简体   繁体   English

AWS Step Functions:将任务输入与 *部分* 任务输出相结合

[英]AWS Step Functions: Combine task input with *partial* task output

I'm working on AWS Step functions.我正在研究 AWS Step 函数。 I've read the documentation on InputPath, OutputPath, and ResultPath .我已阅读有关InputPath、OutputPath 和 ResultPath 的文档 My problem is that I want to combine values from the input to a Lambda Task with a partial output from the task.我的问题是我想将输入到 Lambda 任务的值与任务的部分输出相结合。

The input to the task is similar to below.任务的输入类似于以下内容。 I want to continue passing these values on to subsequent tasks.我想继续将这些值传递给后续任务。

{
    "previous_task_result": 100,
    "next_task_input": "asdf"
}

What I would like, is to have the output of my Lambda Task look like this:我想要的是让我的 Lambda 任务的输出看起来像这样:

{
    "previous_task_result": 100,
    "next_task_input": "asdf",
    "current_task_result": {
        "val1": "ghjk",
        "val2": [0,2,13,100]
    }
}

The specific problem is that the raw output of the Lambda Task looks like this.具体问题是 Lambda 任务的原始输出看起来像这样。 I only care about the Payload node.我只关心Payload节点。 The rest of the output is boilerplate I would rather not pass through the Step Function.其余的输出是样板文件,我宁愿不通过 Step Function。

{
  "resourceType": "lambda",
  "resource": "invoke",
  "output": {
    "ExecutedVersion": "$LATEST",
    "Payload": {
        "val1": "ghjk",
        "val2": [0,2,13,100]
    },
    "SdkHttpMetadata": {
      "HttpHeaders": {
        "Connection": "keep-alive",
        "Content-Length": "42",
        "Content-Type": "application/json",
        "Date": "Tue, 25 Feb 2020 14:36:29 GMT",
        "X-Amz-Executed-Version": "$LATEST",
        "x-amzn-Remapped-Content-Length": "0"
      },
      "HttpStatusCode": 200
    },
    "SdkResponseMetadata": {
      "RequestId": "redacted"
    },
    "StatusCode": 200
  }
}

I understand how to use ResultPath and OutputPath to combine the input with the output, or assign the output to a specific node, but what I can't find is a way to merge just the Payload node from the result with the existing input.我了解如何使用ResultPathOutputPath将输入与输出组合,或将输出分配给特定节点,但我找不到的是一种仅将结果中的Payload节点与现有输入合并的方法。

When working with Lambda Task States there's 3 ways in which you can structure the your task state:使用 Lambda 任务状态时,您可以通过 3 种方式构建任务状态:

1. Request Response: 1. 请求响应:

This method returns the full API response including the SdkHttpMetadata field.此方法返回完整的 API 响应,包括 SdkHttpMetadata 字段。 I suspect the reason for returning the full HTTP response is because this is the only way in which you can invoke a Lambda function asynchronously from Step Function (Asynchronous Lambda invocations only return a status code).我怀疑返回完整 HTTP 响应的原因是因为这是您可以从 Step Function 异步调用 Lambda 函数的唯一方法(异步 Lambda 调用仅返回状态代码)。 Example:例子:

"CallLambda": {
    "Type": "Task",
    "Resource": "arn:aws:states:::lambda:invoke",
    "Parameters": {
        "FunctionName": "MyFunction",
        "InvocationType": "Event|RequestResponse|DryRun",
        "Payload.$": "$"
    },
    "End": true
}

2. Lambda ARN as the Resource: 2. Lambda ARN 作为资源:

I believe this is what you are looking for.我相信这就是你正在寻找的。 When specifying the ARN of the Lambda function as the Resource, Step Functions will invoke your Lambda function synchronously and return only the result from your Lambda function without the API response metadata.将 Lambda 函数的 ARN 指定为资源时,Step Functions 将同步调用您的 Lambda 函数并仅返回来自您的 Lambda 函数的结果,而没有 API 响应元数据。 In this case your State Input will be passed as the payload to your Lambda function or you can use InputPath/Parameters to filter/modify the data sent as the Payload.在这种情况下,您的状态输入将作为负载传递给您的 Lambda 函数,或者您可以使用 InputPath/Parameters 过滤/修改作为负载发送的数据。

"CallLambda": {
    "Type": "Task",
    "Resource": "arn:aws:lambda:us-west-2:123456789012:function:my-function",
    "ResultPath": "$.current_task_result",
    "End": true
}

Using the input as in your example the above Task will give you the output as follows:使用您的示例中的输入,上述任务将为您提供如下输出:

{
    "previous_task_result": 100,
    "next_task_input": "asdf",
    "current_task_result": {
        <Your Lambda Functions Result>
    }
}

3. .WaitForTaskToken: 3. .WaitForTaskToken:

Passes a token to your function, pauses the execution untill either a call to SendTaskSuccess or SendTaskFailed has been received (this method will only return the Result/Error from SendTaskSuccess or SendTaskFailed with no additional HTTP metadata).将令牌传递给您的函数,暂停执行,直到收到对 SendTaskSuccess 或 SendTaskFailed 的调用(此方法将仅返回来自 SendTaskSuccess 或 SendTaskFailed 的结果/错误,而没有额外的 HTTP 元数据)。

"CallLambda": {
    "Type": "Task",
    "Resource":"arn:aws:states:::lambda:invoke.waitForTaskToken",
    "Parameters": {
        "FunctionName": "MyFunction",
        "Payload":{  
           "token.$":"$$.Task.Token"
        }
    },
    "End": true
}

For anyone who finds this now, the ResultSelector (new as of 8/2020) performs exactly the function I was looking for when this was posted.对于现在发现此问题的任何人, ResultSelector (自 2020 年 8 月起新增)执行的功能正是我在发布时正在寻找的功能。

InputPath, Parameters and ResultSelector 输入路径、参数和结果选择器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Step Functions - 将输入传递给另一个任务 - AWS Step Functions - Pass input to another task 为什么我的任务的 JSON output 被 AWS Step Functions 转义? - Why is the JSON output of my task being escaped by AWS Step Functions? 如何使用 AWS Step Functions 将失败任务的输入和错误传递给回退任务? - How can I pass the input and error of my failing task to a fallback task using AWS Step Functions? AWS Step Function map 任务未将输入传递给启动任务 - AWS Step Function map task not passing input to starting task AWS CDK Step Functions SNS 任务 - 注入到消息中的输入路径参数 - AWS CDK Step Functions SNS Task - input path parameter injected into message 在 AWS Step Functions 中将输入传递给输出 - Passthrough input to output in AWS Step Functions 如何将上下文 object 与步骤 function Lambda 调用中的任务输入结合起来? - How to combine context object with task input in step function Lambda invoke? MaxConcurrency 属性如何适用于 AWS Step Functions 中的 Map 任务? - How does the MaxConcurrency attribute work for the Map Task in AWS Step Functions? 使用 Boto3 的 AWS 步骤函数任务成功回调 - AWS step functions task success callback using Boto3 如何参考 AWS step function 并行任务输出? - How reference AWS step function parallel task output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM