AWS Certified Developer - Associate
API Gateway
API Gateway Demo
In this lesson, you'll learn how to work with the AWS API Gateway by creating a REST API that integrates with AWS Lambda functions. This step-by-step guide demonstrates how to configure resources, methods, and Lambda integrations for a library application.
Step 1: Access the AWS API Gateway Console
Open the AWS Console and search for API Gateway. You will see several options such as HTTP APIs, WebSocket APIs, REST APIs, and private REST APIs. For this demo, we will use the REST API.
Select REST API and click Build. When creating a REST API, you have several choices:
- Create a new API from scratch
- Import an API from an OpenAPI definition file
- Clone an existing API
- Use an AWS-provided demo API
For this demo, choose the "create a new API from scratch" option.
Provide an API name that reflects its purpose. For example, for a library application that keeps track of books, you might name it Library. Optionally, add a description, select the API endpoint type (regional, edge optimized, or private), and click Create API. Once created, the console will display your API resources. Initially, only the root resource ("/") will be visible.
Step 2: Create a Resource for Books
Since this is a library API, the next step is to create a new resource for books. Click on Create Resource and set the resource path to /books
. This ensures that any requests to /books
are handled by this resource.
Step 3: Define Methods for the Resource
For a basic REST API, a GET request is commonly used to retrieve data. In our case, a GET request to /books
should return a list of books. Under the /books
resource, click on Create Method and select GET from the dropdown menu of HTTP methods.
Next, choose Lambda Function as the integration type. Note that by default, API Gateway does not forward the complete HTTP request details (headers, IP address, or body) to the Lambda function unless you enable Lambda proxy integration. For this demo, we keep Lambda proxy integration disabled and only configure the necessary data.
Create the Lambda Function for Books
Before integrating with API Gateway, create a Lambda function. Open the AWS Lambda console and create a function named getBooks using Node.js as the runtime. The runtime selection does not affect the API Gateway integration since the process remains the same regardless of the language.
Update the sample code with the following handler function:
export const handler = async (event, context) => { const response = { body: "Here is a list of all books", }; return response; };
After updating the code, deploy the function changes. You can test the Lambda function by clicking Test in the Lambda console; it should return:
{ "body": "Here is a list of all books" }
Return to API Gateway, choose your getBooks Lambda function for the GET method integration, and leave the default timeout (29 seconds). You can adjust the integration timeout in the Lambda function configuration if needed.
Step 4: Understand API Gateway's Request Flow
API Gateway displays a visual diagram of the request flow:
- The client's request goes through the method request.
- API Gateway processes the integration request and forwards it (with optional data transformations) to the Lambda function.
- Once the Lambda function returns a response, API Gateway can modify it via the integration response before sending the final HTTP response to the client.
You can edit mapping templates for both integration requests and responses to tailor incoming data (like URL paths, query parameters, and headers) or modify the Lambda response. By default, API Gateway returns a 200 status code if the Lambda function completes successfully.
Step 5: Test the API Method
In API Gateway, you can simulate an HTTP request by providing dummy query strings and headers. Click Test to view the response, which should be:
{"body": "Here is a list of all books"}
This output includes details such as headers and execution logs that show the request flow through API Gateway and the Lambda function.
Step 6: Deploy Your API
After confirming that your method works as expected, deploy your API:
- Click Deploy API in API Gateway.
- Choose (or create) a stage. Stages represent environments like development, testing, or production. For this demo, create a stage named dev.
- Deploy the API.
Once deployed, navigate to the Stages section to locate the invoke URL. This URL is the endpoint where your API is hosted. Access it in a web browser; if you see an error such as:
{"message":"Missing Authentication Token"}
it means you are hitting the root URL without a configured method. Simply append the resource path (e.g., /books
) to the URL to correctly invoke the GET method.
To further test the endpoint, use an HTTP client like Postman. Paste the full invoke URL (including the resource path) into Postman and send a GET request. The expected response:
{ "body": "Here is a list of all books" }
with a status code of 200 along with additional headers such as Content-Type and X-Amzn-Trace-Id.
Step 7: Add an Authors Resource
Next, add a new resource for authors to enable users to retrieve a list of authors.
- In your API's resource tree (not the deployed stage view), click Create Resource.
- Set the resource path to
/authors
. - Add a GET method under this resource.
Similarly, create a Lambda function named getAuthors using Node.js. Update the function code to:
export const handler = async (event, context) => { const response = { body: "Here is a list of all authors", }; return response; };
Deploy this function, then return to API Gateway. Integrate the GET method under /authors
with the getAuthors Lambda function and deploy the updated API to the dev stage.
After a short propagation period, test the /authors
endpoint using the invoke URL appended with /authors
. You should receive:
{"body": "Here is a list of all authors"}
with a status code of 200.
Step 8: Create a Nested Endpoint for Top Books
To retrieve top-rated or top-selling books, create a nested endpoint under the /books
resource:
- Under
/books
, click Create Resource. - Define the nested resource path as
/top
.
Under the /books/top
resource, add a GET method. Then, create a new Lambda function named getTopBooks using Node.js with the following code:
export const handler = async (event, context) => { const response = { body: "Here is a list of all top books", }; return response; };
Deploy the Lambda function, configure the GET method integration to use getTopBooks, and then deploy the API to the dev stage.
Test this nested endpoint by appending /books/top
to your stage invoke URL in a web browser or Postman. If you receive a "Missing Authentication Token" error, ensure you are using the correct full URL with the nested resource path. After propagation, the endpoint should return:
{"body": "Here is a list of all top books"}
Deployment Reminder
Remember to redeploy your API after making any changes, as the deployment stage represents the active configuration.
Conclusion
This demo has guided you through setting up resources, methods, and Lambda integrations with AWS API Gateway for a library application. You learned how to configure a REST API, create and integrate Lambda functions, and test your endpoints using both the API Gateway console and Postman.
Happy developing, and see you in the next lesson!
References
Watch Video
Watch video content
Practice Lab
Practice lab