Can we add a node to state object after Map state in step Functions, AWS?

402 Views Asked by At

Let's suppose there is a Step Function with a MAP state that processes an orders and add address to each order, Input for Map state object:

{
   "Id":"djhkjs",
    "Orders":["1","2","3"]
}

Map state is for "Orders" list, During Map state execution there is a Lambda to append address.

Input State Object for each iteration:

Order: "1"

Output State Object for each iteration:

Order: "1",
Address: "CA"

Combined Output after MAP:

[
  {
    Order: "1",
    Address: "CA"
   }
  {
    Order: "2",
    Address: "CA"
   }
   {
    Order: "3",
    Address: "CA"
   }
]

What should i do to get the following output?(Instead of list, a JSON Object with a parameter)

{
 [
  {
    Order: "1",
    Address: "CA"
   }
  {
    Order: "2",
    Address: "CA"
   }
   {
    Order: "3",
    Address: "CA"
   }
 ]
ID:"hjshjdhsj",
Name:"Services"
}

1

There are 1 best solutions below

3
Pooya Paridel On

By default the output of each state in your workflow will be the input of the next state and all previous input will be replaced with the last output.

To change it, use the ResultPath filter to include the original input in the state's output.

Example

Workflow Definition:

{
  "StartAt": "Map",
  "States": {
    "Map": {
      "Type": "Map",
      "ItemProcessor": {
        "ProcessorConfig": {
          "Mode": "INLINE"
        },
        "StartAt": "Pass",
        "States": {
          "Pass": {
            "Type": "Pass",
            "End": true,
            "Result": {
              "Order.$": "$",
              "Address": "Address"
            }
          }
        }
      },
      "End": true,
      "ItemsPath": "$.Orders",
      "ResultPath": "$.result"
    }
  }
}

Execution input:

{"Orders": [1,2,3,4,5]}

Output:

{
  "Orders": [
    1,
    2,
    3,
    4,
    5
  ],
  "result": [
    {
      "Order.$": "$",
      "Address": "Address"
    },
    {
      "Order.$": "$",
      "Address": "Address"
    },
    {
      "Order.$": "$",
      "Address": "Address"
    },
    {
      "Order.$": "$",
      "Address": "Address"
    },
    {
      "Order.$": "$",
      "Address": "Address"
    }
  ]
}

As you see in the output, the Map result (the array) is not the root element anymore.