2

aws_stepfunctions_tasks.LambdaInvoke.__init__ takes an input_path argument, which defaults to $ - the entire task input. How can I combine that with the context object ($$), since my Lambda needs information from both? Or do I need to use something else, like the payload argument, to specify more than one input?

l0b0
  • 55,365
  • 30
  • 138
  • 223

2 Answers2

4

To pass both, the payload and context to lambda function, you will need to wrap the original input inside the another attribute for instance Payload

{
....
"ACCESS": {
      "Type": "Task",
      "Parameters": {
        "Payload.$": "$",
        "Context.$": "$$"
      },
      "Resource": "my_lambda_arn",
      "Next": "SLACK_MESSAGE"
    }
...
}
b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57
  • 1
    Superb, thank you! I'll never get used to how CDK uses different names for stuff like this, like how `Parameters` becomes the `payload` argument. FYI, I ended up using `payload=aws_stepfunctions.TaskInput.from_object({"Input.$": "$", "Context.$": "$$"})`. – l0b0 May 13 '21 at 01:07
  • 1
    I am using the newer lambda invokaction and receiving `could not be used to start the Task: [The field \"Context\" is not supported by Step Functions]` – vfrank66 May 10 '22 at 19:50
0

The previous solution no longer works as pointed out by @vfrank66. There are two alternative methods however that will work.

Option 1

Change the Payload.$ to be equal to $$ in the Step Functions definition document as below:

{...
"<state_name>": {
      "Type": "Task",
      "Resource": "arn:aws-us-gov:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "<lambda_arn>",
        "Payload.$": "$$"
      },...
}

Values from the context can be accessed from the event variable in this manner(execution name from context object in python):

execution = event['Execution']['Name']

Values from the input state can be accessed from the event variable in this manner (custom environment variable in python):

environment = event['Execution']['Input']['environment']

Option 2

Option one will not be reflected in the studio view, but this option can be done in the studio view. In workflow studio view on the Configuration tab, under Payload, choose the Enter Payload option. In the window that appears, enter the following:

{
"<name_for_context_object>": "$$",
"<name_for_input_state_object>": "$"
}

When accessing in python you only need to access these objects with the custom names preceding them as shown below:

execution = event['<name_for_context_object>']['Execution']['Name']

and

environment = event['<name_for_input_state_object>']['environment']

this guy
  • 1
  • 1