python-3.xaws-step-functions

Need to read response values from Step Function - lambda


I have a stepfunction that triggers a Python lambda function. The lambda function returns a dictionary which contains data in key:value format.

I want to use one of the key:value returned from Lambda in a choice function in subsequent StepFunction. Am having issues in parsing the key:value attributes. I have tried both ResultPAth, OutputPath and parse the result values.

The dictionary from the lambda:

load_result={}
load_result['DEPT_NAME'] = dataset["dept"]
load_result['number_of_higher_grade_promotions'] = 0

Step Function definition configured for Lambda function:

{
  "Type": "Task",
  "Resource": "arn:aws:states:::lambda:invoke",
  "Parameters": {
    "Payload.$": "$",
    "FunctionName": "arn:aws:lambda:eu-central-1:1234456789:function:my_lambda_function:$LATEST"
  },
  "Retry": [
    {
      "ErrorEquals": [
        "Lambda.ServiceException",
        "Lambda.AWSLambdaException",
        "Lambda.SdkClientException",
        "Lambda.TooManyRequestsException"
      ],
      "IntervalSeconds": 1,
      "MaxAttempts": 3,
      "BackoffRate": 2
    }
  ],
  "OutputPath": "$",
  "Next": "Is queue empty"
}

The result when the lambda function is executed from step function:

{
  "ExecutedVersion": "$LATEST",
  "Payload": {
    "dept": [
      "DEPT_FIN"
    ],
    "number_of_higher_grade_promotions": 0
  }
}

Am trying to access the dictionary key, number_of_higher_grade_promotion, in a Choice step (Is Queue Empty) function, but getting errors:

{
  "Type": "Choice",
  "Choices": [
    {
      "Variable": "$.number_of_higher_grade_promotion",
      "NumericEquals": 0,
      "Next": "Empty queue"
    }
  ],
  "Default": "lambda_function_2"
}

The error am getting is:

An error occurred while executing the state 'Is queue empty' (entered at the event id #7). Invalid path '$.number_of_higher_grade_promotion': The choice state's condition path references an invalid value.

Please help, am new to Step Functions.


Solution

  • You need to specify $.Payload.number_of_higher_grade_promotions because that response you shared has that key nested, not at the root. Alternatively, you could set the OutputPath to $.Payload to have Step Functions pass on only a portion of the response payload from your Lambda function.