Skip to main content
In this lesson we build a reusable API Gateway construct that exposes an AWS Lambda function over HTTP using a REST API and Lambda proxy integration. The construct will:
  • Create a REST API.
  • Add a root ANY method that proxies to the Lambda.
  • Add a {proxy+} resource and an ANY proxy method (Lambda proxy integration, AWS_PROXY) to catch all sub-paths.
  • Grant API Gateway permission to invoke the Lambda.
  • Deploy the API to a stage and expose the public invoke URL.
The resulting architecture looks like this:
A presentation slide titled "Exposing Lambda Function With API Gateway – Solution" showing a stylized monitor labeled "API" next to a smartphone with code brackets. A footer reads "Create a construct for API Gateway."
Below is a minimal stack example showing how to create the Lambda function and consume the LambdaRestApi construct once implemented.

Naming helper: getConstructName

A small helper prefixes construct identifiers with the current Terraform stack id. This produces readable and unique resource names in the AWS console. Create utils/utils.ts:
If your editor doesn’t pick up the new file immediately, reload Visual Studio Code to refresh the project files.
A Visual Studio Code window with a project file explorer on the left and a large editor area showing an error: "The editor could not be opened because the file was not found." A red X icon and a "Create File" button are visible in the center.

LambdaRestApi construct

Create constructs/LambdaRestApi.ts. The LambdaRestApi construct encapsulates all API Gateway resources and wiring required to expose a Lambda function via HTTP. Key responsibilities:
  • Create an ApiGatewayRestApi.
  • Add root ANY method integrated with Lambda.
  • Create a {proxy+} resource with ANY method and AWS_PROXY integration to forward all sub-path requests.
  • Add a LambdaPermission that lets API Gateway invoke the Lambda.
  • Deploy the API to the specified stage and expose a url property.
Create the file constructs/LambdaRestApi.ts with the following implementation:

Implementation notes and best practices

  • The root ANY method and the {proxy+} ANY method together allow all HTTP methods (GET, POST, PUT, DELETE, etc.) to be proxied to the Lambda.
  • For Lambda proxy integrations, integrationHttpMethod must be POST.
  • Use integration type: 'AWS_PROXY' to enable forwarding of the full request payload and headers to the Lambda.
  • The sourceArn in LambdaPermission restricts invocation to this API. The pattern ${restApi.executionArn}/*/* covers all stages and HTTP methods for the API.
If your deployment uses a different AWS region, update the execute-api hostname in the url property or derive the region from the provider config to construct the correct invoke URL programmatically.
Carefully scope LambdaPermission with sourceArn. Overly broad permissions can allow unintended services to invoke your function. The example uses ${restApi.executionArn}/*/* to limit access to this API.

Resources created by the construct

How to use the construct in your stack

  1. Implement your Lambda function as a construct (example provided in the sample stack).
  2. Instantiate LambdaRestApi, passing the Lambda construct instance and the desired stageName.
  3. Use the construct’s url property to create a TerraformOutput or to wire that URL into other systems.
This pattern encapsulates all API Gateway wiring into a single, reusable construct. It keeps stack code concise, improves resource naming consistency, and makes it easy to reuse the same API wiring across multiple services.

References

This construct is a straightforward approach to exposing Lambda functions via HTTP with CDK for Terraform and TypeScript, using Lambda proxy integration for flexible request handling.

Watch Video