Bootstrap FreeKB - Amazon Web Services (AWS) - Modify Step Function State Machine Output using OutputPath
Amazon Web Services (AWS) - Modify Step Function State Machine Output using OutputPath


Step Functions can be used to submit a request to your one of your API Gateway Routes. For example, let's say you have an API Gateway Route https://abcdefg123.execute-api.us-east-1.amazonaws.com/foo that returns JSON "greeting": "Hello World", like this.

~]$ curl --request GET --url https://abcdefg123.execute-api.us-east-1.amazonaws.com/foo
{"greeting": "Hello World"}

 

You can create a Step Function that forwards the request onto the API Gateway Route. Let's create a Step Function State Machine.

  1. Open the Step Functions console and select Create state machine.
  2. In the Choose a template dialog box, select Blank.
  3. Choose Select. This opens Workflow Studio in Design mode.
  4. In the left panel, drag API Gateway Invoke into the middle panel.
  5. In the right panel, update the JSON to have something like this.
{
  "ApiEndpoint": "abcdefg123.execute-api.us-east-1.amazonaws.com",
  "Method": "GET",
  "Path": "/foo",
  "AuthType": "IAM_ROLE"
}

 

Select Test state > Start test and check to see if the request was successful. Likewise, you can select your State Machine > Start Execution and check to see if the request was successful. And the output JSON should look something like this.

{
  "Headers": {
    "Apigw-Requestid": [
      "QodbChRwoAMEPKw="
    ],
    "Connection": [
      "keep-alive"
    ],
    "Content-Length": [
      "99"
    ],
    "Content-Type": [
      "text/plain; charset=utf-8"
    ],
    "Date": [
      "Thu, 28 Dec 2023 02:24:07 GMT"
    ]
  },
  "ResponseBody": {
    "greeting": "Hello World"
  },
  "StatusCode": 200,
  "StatusText": "OK"
}

 

OutputPath can be used to modify the output JSON. In Workflow Studio Design mode, select API Gateway Invoke in the center panel. Select the Output tab in the right panel. If you set "Filter output with OutputPath" to $.ResponseBody then the JSON output should be.

"greeting": "Hello World"

 

If you set "Filter output with OutputPath" to $.ResponseBody.greeting then the JSON output should be.

"Hello World"

 

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 723d9c in the box below so that we can be sure you are a human.