
Ship a Lambda Web Function with CDK
A first look at how you can deploy a lambda web function with CDK
What are they? Well, you can now ship Express, Hono, or React Router with no Lambda-specific adapters or wrappers.
Let me show you how I got this demo set up with CDK.
λ: https://cxrj1w1j65n0k7ss.p7v.lambda-web.us-east-2.on.aws (this may not live forever but the code should)
⚠️ DISCLAIMER: The code we are about to see is written with L1 CDK constructs. The L2 constructs are being tracked via this PR .
First, let's look at the server code I have in Express. I've omitted the bulk of its logic for brevity.
ts
import express from "express";const [hostname, rawPort] = (process.env.AWS_LAMBDA_HTTP_ENDPOINT ??`127.0.0.1:${process.env.PORT ?? "3000"}`).split(":");const port = Number(rawPort);export const app = express();export const server = app.listen(port, hostname, () => {console.log(`Spinning Lambda is listening on http://${hostname}:${port}`);});
I can run the code locally with
pnpm dev .
Now, let's look at the CDK stack used to deploy this function.
ts
export class SpinningLambdaStack extends Stack {constructor(scope: Construct, id: string, props?: StackProps) {super(scope, id, props);// code bucket stuff omittedconst webFunction = new CfnWebFunction(this, "WebFunction", {functionName: `${this.stackName}-web`,// many props omitted for brevity});const revision = new CfnWebFunctionRevision(this, "WebFunctionRevision", {functionName: webFunction.ref,// many props omitted for brevity});const endpoint = new CfnWebFunctionEndpoint(this, "WebFunctionEndpoint", {functionName: webFunction.ref,// many props omitted for brevity});}}
I grab some credentials for my account with
aws login . I need to do a one-time setup to bootstrap CDK with npx cdk bootstrap .Once that's done, I can run
pnpm deploy to ship it.
Then it's live 🚀!

Share this on X/Twitter, and it should render as an embed:
plaintext
https://cxrj1w1j65n0k7ss.p7v.lambda-web.us-east-2.on.aws/embed
As always, happy coding 😉!
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article