0

I created the following Step Functions. In the first Lambda Function, after getting the product_name and product_price, I generate a product ID. In the CheckStatusFrom_AddToCart step I check if everything is okay on the Lambda side and if there is, I move on to the next step. In this step (Success_AddToCart) I would like to send the product ID generated earlier along with the product_price and paid_price variables. How can I store that variable? Choice type does not support ResultPath.

{
  "StartAt": "LambdaFunction_AddToCart",
  "States": {
    "LambdaFunction_AddToCart": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:xxx",
      "Parameters": {
        "product_name": "Television",
        "product_price": 1500
      },
      "Next": "CheckStatusFrom_AddToCart"
    },
    "CheckStatusFrom_AddToCart": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.statusCode",
          "NumericEquals": 200,
          "Next": "Success_AddToCart"
        },
        {
          "Variable": "$.statusCode",
          "NumericEquals": 400,
          "Next": "Fail_AddToCart"
        }
      ]
    },
    "Success_AddToCart": {
      "Type": "Task",
      "Resource": "arn:aws:yyy",
      "Parameters": {
        "product_price": 1500,
        "paid_price": 1500
      },
      "Next": "CheckStatusFrom_Payment"
    },
    "Fail_AddToCart": {
      "Type": "Fail"
    },
    "CheckStatusFrom_Payment": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.statusCode",
          "NumericEquals": 200,
          "Next": "Success_Payment"
        },
        {
          "Variable": "$.statusCode",
          "NumericEquals": 400,
          "Next": "Fail_Payment"
        }
      ]
    },
    "Success_Payment": {
      "Type": "Pass",
      "End": true
    },
    "Fail_Payment": {
      "Type": "Fail"
    }
  }
}

UPDATE

I implemented ResultPath and I can forward an incoming value to the next step, so I can call LambdaFunction_MakePayment with the productID. But after that, in LambdaFunction_ThankYou, it's not working anymore:

An error occurred while executing the state 'LambdaFunction_ThankYou' (entered at the event id #18). The JSONPath '$.productID' specified for the field 'product_id.$' could not be found in the input '{"statusCode": 200, "body": "Paid price 1500 equals Product price 1500"}'

  {
    "StartAt": "LambdaFunction_AddToCart",
    "States": {
      "LambdaFunction_AddToCart": {
        "Type": "Task",
        "Resource": "arn:aws:xxx",
        "Parameters": {
          "product_name": "Television",
          "product_price": 1500
        },
        "Next": "GetProductID_From_AddToCart"
      },
      "GetProductID_From_AddToCart": {
        "Type": "Pass",
        "ResultPath": "$.purchase",
        "Result": { "product_id": "$.productID"},
        "Next": "CheckStatusFrom_AddToCart"
      },
      "CheckStatusFrom_AddToCart": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.statusCode",
          "NumericEquals": 200,
          "Next": "LambdaFunction_MakePayment"
        },
        {
          "Variable": "$.statusCode",
          "NumericEquals": 400,
          "Next": "Fail_Payment"
        }
        ]
      },
      "LambdaFunction_MakePayment": {
         "Type": "Task",
        "Resource": "arn:aws:yyy",
        "Parameters": {
          "product_price": 1500,
          "paid_price": 1500,
          "product_id.$": "$.productID"
        },
        "Next": "CheckStatusFrom_Payment"
      },
       "CheckStatusFrom_Payment": {
        "Type": "Choice",
        "Choices": [
          {
            "Variable": "$.statusCode",
            "NumericEquals": 200,
            "Next": "LambdaFunction_ThankYou"
          },
          {
            "Variable": "$.statusCode",
            "NumericEquals": 400,
            "Next": "Fail_Payment"
          }
        ]
      },
      "LambdaFunction_ThankYou": {
        "Type": "Task",
        "Resource": "arn:aws:lambda:zzz",
        "Parameters": {
          "product_name": "Television",
          "product_id.$": "$.productID"
        },
        "Next": "Success_Payment"
      },
      "Success_Payment": {
        "Type": "Pass",
        "End": true
      },
      "Fail_Payment": {
        "Type": "Fail"
      }
  }
  }
2
  • Could it the reason that RESULTPATH is not defined in Lambda task, thats why you are not able to access the variables? Commented Mar 22, 2023 at 14:33
  • 1
    Your new Pass state isn't doing what you think it is. The Pass state's Result field sets *literal* (non-JSONPath) output. The good news is you probably don't need the Pass state. You probably want to set Lambda task's ResultSelector and/or ResultPath. I can't be more specific, because it's not clear what your actual/expected state inputs and outputs are. Commented Mar 23, 2023 at 7:49

1 Answer 1

0

After all I figured it out.

{
  "StartAt": "LambdaFunction_AddToCart",
  "States": {
    "LambdaFunction_AddToCart": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:eu-north-1:413975335361:function:step-function-checkStatusFromAddToCart",
      "Parameters": {
        "product_name": "Television",
        "product_price": 1500
      },
      "ResultPath": "$.purchase",
      "Next": "CheckStatusFrom_AddToCart"
    },
    "CheckStatusFrom_AddToCart": {
      "Type": "Choice",
      "InputPath": "$.purchase",
      "Choices": [
        {
          "Variable": "$.statusCode",
          "NumericEquals": 200,
          "Next": "LambdaFunction_MakePayment"
        },
        {
          "Variable": "$.statusCode",
          "NumericEquals": 400,
          "Next": "Fail_AddToCart"
        }
      ]
    },
    "LambdaFunction_MakePayment": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:eu-north-1:413975335361:function:step-function-checkStatusFromPayment",
      "Parameters": {
        "product_price": 1500,
        "paid_price": 1500,
         "product_id.$": "$.productID"
      },
      "Next": "CheckStatusFrom_Payment"
    },
    "Fail_AddToCart": {
      "Type": "Fail"
    },
    "CheckStatusFrom_Payment": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.statusCode",
          "NumericEquals": 200,
          "Next": "LambdaFunction_ThankYou"
        },
        {
          "Variable": "$.statusCode",
          "NumericEquals": 400,
          "Next": "Fail_Payment"
        }
      ]
    },
    "LambdaFunction_ThankYou": {
        "Type": "Task",
        "Resource": "arn:aws:lambda:eu-north-1:413975335361:function:step-function-ThankYou",
        "Parameters": {
          "product_name": "Television",
          "product_id.$": "$.productID"
        },
        "Next": "Success_Payment"
      },
    "Success_Payment": {
      "Type": "Pass",
      "End": true
    },
    "Fail_Payment": {
      "Type": "Fail"
    }
  }
}

I'd like to note that I did not use InputPath in the second Choice step, but if the productID changes in the meantime (e.g. in the second Lambda function), it should be added to the Choice step to update that variable to the latest value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.