In this lesson, we walk through creating an API Gateway construct that deploys all the necessary resources for our API. The API provides an HTTP endpoint that triggers an AWS Lambda function. All the logic is encapsulated within a reusable construct. Below is an overview of the process:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
- Create a new construct file named
lambda-rest-api.tsin the appropriate folder. - Instantiate the API Gateway, define its resources (including both root and proxy resources), attach HTTP methods with Lambda integrations, and finally expose the API URL.
Creating the Basic Construct
We begin by setting up a construct to initialize the API Gateway. In your stack file, create a file calledlambda-rest-api.ts with the following content:
Building the API Gateway Construct
Withinlambda-rest-api.ts, we now create the API Gateway construct. This includes importing the required AWS provider modules:
- The construct accepts a Lambda function handler and a stage name.
- Unique API names are generated with the
getConstructNamefunction. - Both the root and proxy resources are integrated with the Lambda function using an AWS_PROXY integration.
- Lambda permissions are set to allow API Gateway to invoke the Lambda function.
Utility Function: getConstructName
This helper function creates unique resource names by appending the stack name to an identifier. Add the following code in a file likeutils/utils.ts:
Integrating the API Gateway Construct in the Stack
Update your main stack file to instantiate the Lambda function and link it to the API Gateway construct:Verifying Deployment and API Invocation
After deployment, you should see outputs similar to the following in your console:


Summary
In this lesson, we built a Lambda REST API construct that:- Defines an API Gateway REST API with both root and proxy resources.
- Integrates HTTP methods with an AWS Lambda function using AWS_PROXY.
- Sets up necessary Lambda permissions for API Gateway.
- Deploys the API and exposes its invoke URL.