GenAI with Stock Trading Website (Module 4)

Hui Yee Leong
4 min readJul 10, 2024

--

Module 4: Create API Gateway

In this module, we will set up an AWS API Gateway to expose the Lambda function created in the previous module. The API Gateway will act as an interface between the chatbot and the Lambda function, allowing the chatbot to send queries and receive responses.

Step 1: Set Up API Gateway

4.1 Introduction to API Gateway

AWS API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. It handles all the tasks involved in accepting and processing up to hundreds of thousands of concurrent API calls, including traffic management, authorization and access control, monitoring, and API version management.

4.2 Create a New API

To create a new API, follow these steps:

a. Open the API Gateway Console: In the search bar, type “API Gateway” and select it from the list of services.

b. Create a REST API:

  • Click on “Create API.”
  • Select “REST API” and choose “Build.”
  • Enter a name for your API (e.g., “asx-api”) and a description.
  • Choose “Regional” as the endpoint type.
  • Click “Create API.”

4.3 Create a Resource

A resource represents an entity that your API manages. To create a resource:

  1. Add a Resource:
  • In the API Gateway console, select your newly created API.
  • Click on “Resources” in the left-hand menu.
  • Click “Create Resource.”
  • Enter a Resource Name (e.g., “chatbot”) and a Resource Path (e.g., /chatbot).
  • Click “Create Resource.”

4.4 Create a Method

Methods represent the API operations that can be performed on the resource. To create a method:

a. Add a Method:

  • Select the resource you just created.
  • Click on the “Actions” dropdown and select “Create Method.”
  • Choose “POST” from the dropdown and click the checkmark.

b. Configure the Method:

  • Select “Lambda Function” as the integration type.
  • Check the box for “Use Lambda Proxy Integration.”
  • Enter the region where your Lambda function is deployed.
  • Enter the name of your Lambda function
  • Click “Save” and acknowledge the required permissions by clicking “OK.”

4.5 Enable CORS

To allow cross-origin requests, enable CORS on your resource:

a. Enable CORS:

  • Select the resource you created.
  • Click on the “Enable CORS”.
  • Review the settings and click “Save”

4.6 Deploy the API

To make your API available, deploy it:

a. Deploy the API:

  • select “Deploy API.”
  • Create a new deployment stage (e.g., “prod”).
  • Click “Deploy.”

b. Get the Invoke URL:

  • After deployment, note the Invoke URL for your API (e.g., https://<api-id>.execute-api.<region>.amazonaws.com/prod/chatbot).

4.7 Test the API

Before integrating the API with the chatbot, test it to ensure it works correctly:

a. Test the API:

  • Use a tool like Postman or curl to send a POST request to the Invoke URL.
  • Include a sample query in the request body like below example using curl:
curl -X POST https://<api-id>.execute-api.<region>.amazonaws.com/prod/chatbot \
-H "Content-Type: application/json" \
-d '{
"inputTranscript": "Provide me the listed company in ASX",
"sessionState": {
"sessionAttributes": {},
"intent": {
"state": "InProgress"
}
},
"sessionId": "test-session",
"requestAttributes": {}
}'
  • Verify that the API returns the expected response from the Lambda function.

Summary

In this module, we set up an AWS API Gateway to expose the Lambda function created in the previous module. We created a REST API, added a resource and method, enabled CORS, deployed the API, and tested it to ensure it works correctly. In the next module, we will create a chatbot and integrate it with this API Gateway.

Next Module: GenAI with Stock Trading Website (Module 5)

--

--