Bootstrap FreeKB - Amazon Web Services (AWS) - API Gateway Step Function
Amazon Web Services (AWS) - API Gateway Step Function


Step Functions can be used to connect two or more functions, passing the output from one function into another function. 

For example, let's say you have an API Gateway Route that validates if a users account exists, perhaps like this.

~]$ curl --request POST --url https://abcdefg123.execute-api.us-east-1.amazonaws.com/validateUsername --data '{"email":"john.doe@example.com"}'
{"result": "success", "message":"john.doe@example.com is a valid username"}

 

And you have another API Gateway Route that generates a random token.

~]$ curl --request POST --url https://abcdefg123.execute-api.us-east-1.amazonaws.com/generateToken --data '{"email":"john.doe@example.com"}'
{"token": "abc123","email":"john.doe@example.com"}

 

And you have another API Gateway Route that uses the email and token, perhaps like this.

curl --request POST --url https://abcdefg123.execute-api.us-east-1.amazonaws.com/authenticateUser --data '{"email":"john.doe@example.com","token": "abc123"}'

 

You can create a Step Function that forwards the request onto the first API Gateway Route (/validateUsername) and then onto the second API Gateway Route (/authenticateUser) and then onto the third API Gateway Route (/authenticateUser). 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 two API Gateway Invoke into the middle panel.

 

In the right panel, update the JSON of the generateToken API Gateway Route to have something like this.

{
  "ApiEndpoint": "abcdefg123.execute-api.us-east-1.amazonaws.com",
  "Method": "GET",
  "Path": "/validateUsername",
  "AuthType": "IAM_ROLE",
  "RequestBody": {
    "email.$": "$.email"
  }
}

 

Update the JSON of the generateToken API Gateway Route to have something like this.

{
  "ApiEndpoint": "abcdefg123.execute-api.us-east-1.amazonaws.com",
  "Method": "POST",
  "Path": "/generateToken",
  "AuthType": "IAM_ROLE",
  "RequestBody": {
    "email.$": "$.ResponseBody.email"
  }
}

 

Update the JSON of the generateToken API Gateway Route to have something like this.

{
  "ApiEndpoint": "abcdefg123.execute-api.us-east-1.amazonaws.com",
  "Method": "POST",
  "Path": "/authenticateUser",
  "RequestBody": {
    "email.$": "$.ResponseBody.email",
    "token.$": "$.ResponseBody.token"
  },
  "AuthType": "IAM_ROLE"
}

 

Select Start Execution and check to see if the request was successful.




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 39dd55 in the box below so that we can be sure you are a human.