10

Let's say I have an AWS step function that simply passes the output from the first step to the second step. But now let's say I need to add an additional input value for that second step.

How do I set the parameters for the second step to keep ALL input fields (without specifying them individually) and add a new input value?

The closest I can get is setting the Parameters like this:

"Second Step": {
  "Type": "Task",
  "Resource": "arn:aws:lambda:blahblahblah",
  "InputPath": "$",
  "Parameters": {
    "Input.$":"$",
    "additionalValue": "ABC"
  }
}

But that results in pushing all of the input values under that new "Input" key, where I really just want them in the root of the dictionary. I could swear there was some magic expression I saw once that made this work the way I want it to, but now I can't find it.

AWS now has a simulator you can try this in. Set InputPath to $ and Parameters to {"Input.$":"$","additionalValue":"ABC"} to see an example of this situation.

2 Answers 2

4

You can have this kind of manipulation with ResultSelector. But it can only be applied at ouput step. With this approach you need to either

  • Alter the output of previous step with ResultSelector
  • Create a pass-through step that add the static additional value you wanted
Sign up to request clarification or add additional context in comments.

2 Comments

I appreciate the answer. Wish there were a better way, though. Really seems like an obvious gap.
A previous step is not an option when you need the current step to pass the extra parameters... like AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID during the call to another workflow: docs.aws.amazon.com/step-functions/latest/dg/…
3

I found something that works great using a chain of Amazon States Language intrinsic functions:

{
  "Comment": "Initializes and increments a counter without modifying the original workflow input.",
  "StartAt": "InitializeCounter",
  "States": {
    "InitializeCounter": {
      "Type": "Pass",
      "Next": "IncrementCounter",
      "Parameters": {
        "output.$": "States.JsonMerge($, States.StringToJson('{\"counter\": 0}'), false)"
      },
      "OutputPath": "$.output"
    },
    "IncrementCounter": {
      "Type": "Pass",
      "End": true,
      "Parameters": {
        "output.$": "States.JsonMerge($, States.StringToJson(States.Format('\\{\"counter\": {}\\}', States.MathAdd($.counter, 1))), false)"
      },
      "OutputPath": "$.output"
    }
  }
}

When I execute this with the input

{
  "Comment": "Insert your JSON here"
}

the output of InitializeCounter, which is the input to IncrementCounter, is

{
  "Comment": "Insert your JSON here",
  "counter": 0
}

and the output of IncrementCounter and indeed the whole workflow is

{
  "Comment": "Insert your JSON here",
  "counter": 1
}

.


(Of course, InitializeCounter could also have been defined as

{
  "Type": "Pass",
  "Next": "IncrementCounter",
  "Result": 0,
  "ResultPath": "$.counter"
}

but the purpose of this exercise is to devise a strategy that we could also use for the input of a Task/Activity state.)

1 Comment

This post was the only thing that allowed me to have a simple LOOP-Counter (where the loop involved calling 3 Lambdas in sequence). Nothing else works! Especially when creating StepFunctions within CDK-TypeScript, I had to use Milosz code above as a CustomTask. Note: Use the OutputPath exactly as shown above and Do Not change even a single character in that line. Thanks Milosz.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.