So, AWS Lambda Web Functions just came out, and I'm super excited about them.

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.

Here, you can see that it's an Express.js server binding to a port.

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}`);
});
Enter fullscreen mode Exit fullscreen mode

I can run the code locally with pnpm dev.

Now, let's look at the CDK stack used to deploy this function.

export class SpinningLambdaStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // code bucket stuff omitted
    const 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
    });

  }
}

Enter fullscreen mode Exit fullscreen mode

Now for deployment. I'm using the new AWS experience, so I have a project dedicated to this demo.

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:

https://cxrj1w1j65n0k7ss.p7v.lambda-web.us-east-2.on.aws/embed

Enter fullscreen mode Exit fullscreen mode

Full repo:
https://github.com/sboult/lwf-three-js


As always, happy coding šŸ˜‰!

Follow AWS for more articles like this.

Follow me for all things tech!