How to combine context object with task input in step function Lambda invoke?

Question:

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?

Asked By: l0b0

||

Answers:

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"
    }
...
}
Answered By: b.b3rn4rd

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']

Answered By: this guy