Skip to content

How to merge the output of stepfunction to input array

0

I have a input to a stepfunction state of type task as below.

{
  "token": "abc",
  "body": [
    {
      "Payload": {
        "client": "Client1",
        "data": "Some client data as a string"
      }
    },
    {
      "Payload": {
        "client": "Client2",
        "data": "Some client data as a string"
      }
    }
  ]
}

I this state I am calling a lambda which returns a data like the Payload object which I want to merge it to the body object. How can that be done? Just for information I cannot call the clients in parallel as the last one is dependent on the first two but I want to merge the data to body so that data of all clients is availabe in same place for further processing. Some constraint is I do not want to add a additional lambda to merge the data to into the input.

Expected output :

{
  "token": "abc",
  "body": [
    {...},
    {...},
    {
      "Payload": {
        "client": "Client3",
        "data": "Some client data as a string"
      }
    }
  ]
}

asked a year ago575 views

1 Answer
1

To merge the output of a Step Functions task state (Lambda function in this case) with the input array, you can use a combination of ResultPath and ResultSelector in your Step Functions state machine definition.

Here's how you can achieve this:

  1. In your task state, use ResultSelector to shape the Lambda function's output into the desired format.

  2. Use ResultPath to specify where in the input you want to merge the result.

Your task state definition might look something like this:

"ProcessClient3": {
  "Type": "Task",
  "Resource": "arn:aws:lambda:region:account-id:function:YourLambdaFunction",
  "ResultSelector": {
    "Payload.To merge the output of a Step Functions task state (Lambda function in this case) with the input array, you can use a combination of ResultPath and ResultSelector in your Step Functions state machine definition.

Here's how you can achieve this:

1. In your task state, use ResultSelector to shape the Lambda function's output into the desired format.

2. Use ResultPath to specify where in the input you want to merge the result.

Your task state definition might look something like this:

```json
"ProcessClient3": {
  "Type": "Task",
  "Resource": "arn:aws:lambda:region:account-id:function:YourLambdaFunction",
  "ResultSelector": {
    "Payload.$": "$"
  },
  "ResultPath": "$.body[-1]",
  "End": true
}

Let's break this down:

  • ResultSelector: This shapes the Lambda function's output. If your Lambda is already returning data in the correct "Payload" format, you might not need this. But if you need to wrap the output in a "Payload" object, this will do that.

  • ResultPath: "$.body[-1]" specifies that the result should be appended as the last item in the "body" array.

This approach will add the new client data as a new item in the "body" array, achieving the desired output structure without requiring an additional Lambda function for merging.

Remember that the exact configuration might need to be adjusted based on the specific structure of your Lambda function's output and your state machine's flow. Also, ensure that your Lambda function is designed to process the input correctly and return the data in the expected format.quot;: "To merge the output of a Step Functions task state (Lambda function in this case) with the input array, you can use a combination of ResultPath and ResultSelector in your Step Functions state machine definition.

Here's how you can achieve this:

  1. In your task state, use ResultSelector to shape the Lambda function's output into the desired format.

  2. Use ResultPath to specify where in the input you want to merge the result.

Your task state definition might look something like this:

"ProcessClient3": {
  "Type": "Task",
  "Resource": "arn:aws:lambda:region:account-id:function:YourLambdaFunction",
  "ResultSelector": {
    "Payload.$": "$"
  },
  "ResultPath": "$.body[-1]",
  "End": true
}

Let's break this down:

  • ResultSelector: This shapes the Lambda function's output. If your Lambda is already returning data in the correct "Payload" format, you might not need this. But if you need to wrap the output in a "Payload" object, this will do that.

  • ResultPath: "$.body[-1]" specifies that the result should be appended as the last item in the "body" array.

This approach will add the new client data as a new item in the "body" array, achieving the desired output structure without requiring an additional Lambda function for merging.

Remember that the exact configuration might need to be adjusted based on the specific structure of your Lambda function's output and your state machine's flow. Also, ensure that your Lambda function is designed to process the input correctly and return the data in the expected format.quot; }, "ResultPath": "$.body[-1]", "End": true }


Let's break this down:

- ResultSelector: This shapes the Lambda function's output. If your Lambda is already returning data in the correct "Payload" format, you might not need this. But if you need to wrap the output in a "Payload" object, this will do that.

- ResultPath: "$.body[-1]" specifies that the result should be appended as the last item in the "body" array.

This approach will add the new client data as a new item in the "body" array, achieving the desired output structure without requiring an additional Lambda function for merging.

Remember that the exact configuration might need to be adjusted based on the specific structure of your Lambda function's output and your state machine's flow. Also, ensure that your Lambda function is designed to process the input correctly and return the data in the expected format.  
**Sources**  
[Specifying state output using ResultPath in Step Functions - AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/input-output-resultpath.html)  
[Manipulate parameters in Step Functions workflows - AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/input-output-inputpath-params.html)

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.