MIKAEELS

Explore the blog

Build a Lambda HTTP API with AWS CDK and TypeScript

Create an API Gateway HttpApi, a Lambda function, and an HttpLambdaIntegration to expose a GET route. This example includes the handler and prints the endpoint after deployment, so there are no placeholder application assets to guess.

Create the project and handler

Use an AWS CDK v2 TypeScript app with sandbox AWS credentials. Install the CDK CLI using the official setup instructions, then:

mkdir serverless-demo cd serverless-demo cdk init app --language typescript npm install --save-exact aws-cdk-lib@2.272.0 constructs@10.5.0 mkdir lambda

Create lambda/index.js:

exports.handler = async () => ({ statusCode: 200, headers: { "content-type": "application/json" }, body: JSON.stringify({ message: "Hello from Lambda" }), });

Define the complete stack

Replace lib/serverless-demo-stack.ts:

import { CfnOutput, Stack, StackProps } from "aws-cdk-lib"; import * as lambda from "aws-cdk-lib/aws-lambda"; import * as apigw from "aws-cdk-lib/aws-apigatewayv2"; import { HttpLambdaIntegration } from "aws-cdk-lib/aws-apigatewayv2-integrations"; import { Construct } from "constructs"; export class ServerlessDemoStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); const handler = new lambda.Function(this, "Greeting", { runtime: lambda.Runtime.NODEJS_22_X, handler: "index.handler", code: lambda.Code.fromAsset("lambda"), }); const api = new apigw.HttpApi(this, "Api"); api.addRoutes({ path: "/", methods: [apigw.HttpMethod.GET], integration: new HttpLambdaIntegration("GreetingIntegration", handler), }); new CfnOutput(this, "ApiUrl", { value: api.apiEndpoint }); } }

The HttpLambdaIntegration construct connects the route to the handler and configures invocation permission. The Node.js runtime must be supported in your region; consult the Lambda runtime table before upgrading it.

Validate, deploy, and request the endpoint

npx tsc --noEmit npx aws-cdk synth npx aws-cdk diff npx aws-cdk deploy --outputs-file stack-outputs.json curl 'YOUR_API_URL/'

Replace YOUR_API_URL with ApiUrl from the output file. Expected JSON is {"message":"Hello from Lambda"}. Local compiler and synthesis checks are separate from deployment; this revision has not been deployed to AWS.

If the account and region need setup, follow CDK bootstrap. If the request fails, inspect the route, deployment outputs, and Lambda logs. A request to a route that does not exist will not invoke this function.

Boundaries and cleanup

This is a public, unauthenticated greeting endpoint. Before handling user data, configure an appropriate authorizer, input validation, access logs, and throttling. API Gateway, Lambda, and logs can incur charges; serverless does not mean free.

Review npx aws-cdk destroy for the exercise stack, and inspect retained logs after cleanup. For packaging third-party dependencies, continue with npm modules in Lambda. For an image-based function, see Dockerized Lambda with CDK.

Quick Recap

Which construct connects Lambda to an HTTP API?

Use HttpLambdaIntegration from aws-cdk-lib/aws-apigatewayv2-integrations and pass it to an HttpApi route.

Does this example require a VPC?

No. The function returns a greeting and does not access private network resources. Add VPC networking only when the workload needs it.

Add Mikaeels Blog as a preferred source on Google

Comments

Share your thoughts and questions below