12k
All articles

Running an Express App on Lambda Web Functions

Deploy an Express app to AWS Lambda Web Functions with app.listen, AWS CLI, and S3. Configure revisions, test the endpoint, and migrate from serverless-http.

OpenReplay Team
OpenReplay Team
Running an Express App on Lambda Web Functions

A Lambda Web Function runs an ordinary Node.js HTTP server. Your Express app calls app.listen() on the host and port that Lambda puts in the AWS_LAMBDA_HTTP_ENDPOINT environment variable. You don’t export a handler and you don’t need a request adapter.

Until now, putting Express on Lambda meant wrapping it in serverless-http or serverless-express, putting API Gateway in front, and accepting that local and deployed behavior would differ in small ways. Most guides you’ll find still teach that handler model.

This article deploys one Express app to Lambda Web Functions from start to finish. It covers the one-line code change, the one-command shortcut, the explicit S3 path, how to test the deploy, how to ship a revision, and what to delete from a serverless-http project.

Key Takeaways

  • On Lambda Web Functions, the only Lambda-specific code in an Express app is the line that reads host and port from AWS_LAMBDA_HTTP_ENDPOINT and passes them to app.listen(port, host).
  • The Web Functions Node.js runtime loads index.js from the root of the deployment package by default. Set AWS_LAMBDA_NODEJS_ENTRYPOINT if your entry file has a different name.
  • aws lambda-web deploy creates a versioned S3 bucket, an execution role, the Web Function, its first revision, and a HomeRegion endpoint named dev.
  • A revision showing Active means Lambda has prepared your code. It does not mean your server is listening, so check every deploy with a real HTTP request.
  • create-web-function-revision needs the full build and service configuration every time, because each revision is an immutable snapshot.

What Do Lambda Web Functions Change for Express Apps?

Lambda Web Functions run your app’s own HTTP server and send requests to the port it listens on. Lambda no longer calls an exported handler function. AWS announced Web Functions in October 2026 as a new kind of Lambda function for port-listening web apps, with Express, Hono and Fastify named as examples. See the comparison of Express vs Hono if you’re choosing between those two.

The Developer Guide’s core concepts page describes three resources:

  • Web Function: the function itself.
  • Revision: an immutable snapshot of code plus configuration.
  • Endpoint: the HTTPS front door that routes requests to a revision.

Node.js was the only language at launch. The runtimes page now lists nodejs24.x and nodejs26.x as supported, plus python3.15 in public preview. Web Functions run on Arm64. Existing Lambda functions are now called Lambda Event Functions. This is a name change only, and they need no code changes or redeployment.

On cost, AWS’s Lambda FAQ says Web Functions are billed for active CPU time and memory as separate dimensions, plus requests. Time spent waiting on I/O doesn’t count as active CPU time. Rates are on the Lambda pricing page.

The One Code Change

An Express app needs one change to run on a Web Function: bind the server to the address in AWS_LAMBDA_HTTP_ENDPOINT. That variable holds host:port. The sample code splits it on the colon. If the variable is missing, as it is on your laptop, the code falls back to localhost:3000, so the same index.js runs locally and on Lambda.

const [host, port] = (process.env.AWS_LAMBDA_HTTP_ENDPOINT || 'localhost:3000').split(':');

If the server listens anywhere else, Lambda can’t reach it.

The Express Sample

Start with a fresh project:

npm init -y
npm install express

Create index.js. This is the Express sample from the AWS docs, copied as-is:

const express = require('express');
const app = express();
const [host, port] = (process.env.AWS_LAMBDA_HTTP_ENDPOINT || 'localhost:3000').split(':');

app.get('/', (request, response) => {
  response.send('Hello from Lambda Web Functions!');
});

app.listen(port, host);

The sample uses CommonJS, so you don’t need to change package.json. Run it locally to check it:

node index.js
curl http://localhost:3000/

The response body is Hello from Lambda Web Functions!. The server prints nothing when it starts. The runtime loads index.js from the package root by default. Set AWS_LAMBDA_NODEJS_ENTRYPOINT if your entry file has a different name, as the Node.js guide for Web Functions describes.

Package and Deploy

There are two ways to deploy an Express app to a Lambda Web Function. The deploy shortcut provisions everything for you. The S3 path lets you supply each piece yourself. You need AWS CLI version 2 with the aws lambda-web commands. Run aws lambda-web help to check you have them, and if not, update using the official AWS CLI install guide.

The Shortcut

Install production dependencies first, so node_modules is in the directory you deploy:

npm ci --omit=dev
aws lambda-web deploy --name my-web-app --code .

According to the Creating a Web Function guide, a first run creates or reuses:

  • a versioned S3 bucket
  • an execution role that can write to CloudWatch Logs
  • the Web Function and its first revision
  • a HomeRegion endpoint named dev

When it finishes, it prints the endpoint URL. When you create a function without --hello-world, the command asks for confirmation. Add --create in CI to skip the prompt. The endpoint is public by default. Pass --auth-type IamAuth if only SigV4-signed callers should get in.

The S3 Path

Package the app and upload it:

npm ci --omit=dev
zip -r function.zip index.js package.json package-lock.json node_modules
aws s3 cp function.zip s3://amzn-s3-demo-bucket/samples/function.zip

The S3 code requirements are:

  • versioning enabled on the bucket
  • a bucket policy that grants lambda.amazonaws.com both s3:GetObject and s3:GetObjectVersion

Next, set up the execution role. Its trust policy must allow the Lambda service to assume it, and its permissions policy must grant whatever the app needs. For logs, attach AWSLambdaBasicExecutionRole, or grant logs:CreateLogGroup, logs:CreateLogStream and logs:PutLogEvents, as the troubleshooting guide describes. The IAM policy explainer helps you read a policy before you attach it. If you already ran the shortcut, you can reuse the role it created, which you’ll find in the IAM console.

The identity that runs the deploy needs three permissions, according to the permissions page: lambda:CreateWebFunction (or lambda:CreateWebFunctionRevision for new revisions), iam:PassRole on the execution role, and read access to the package in S3.

Create the function, its first revision and an endpoint in one call:

aws lambda-web create-web-function \
    --function-name my-web-app \
    --revision-config '{
      "buildConfig": {
        "codeConfig": {
          "s3Object": {
            "bucket": "amzn-s3-demo-bucket",
            "key": "samples/function.zip"
          }
        },
        "runtimeConfig": { "runtime": "nodejs24.x" }
      },
      "serviceConfig": {
        "executionRoleArn": "arn:aws:iam::111122223333:role/my-web-app-execution-role"
      }
    }' \
    --endpoint-config '{
      "endpointName": "my-endpoint",
      "endpointType": "HomeRegion",
      "authType": "ApplicationManaged",
      "autoDeploymentMode": "LatestRevision"
    }'

If you leave out autoDeploymentMode, it defaults to Disabled. New revisions then get no traffic until you route it to them yourself. You can’t change the endpoint type after the endpoint is created.

Copy the revision ID from the response, then wait for the revision and fetch the endpoint:

aws lambda-web wait web-function-revision-active \
    --function-name my-web-app \
    --revision-id <revision-id>

aws lambda-web get-web-function-endpoint \
    --function-name my-web-app \
    --endpoint-name my-endpoint

Use the domainName field from that output to call the app.

Active Does Not Mean Listening

Lambda doesn’t check that your server bound to the port. A revision and its endpoint can both show Active while every request fails, so check every deploy with a real request. If a request fails, check these causes:

  • The server isn’t bound to the host and port from AWS_LAMBDA_HTTP_ENDPOINT.
  • index.js isn’t at the root of the zip, and AWS_LAMBDA_NODEJS_ENTRYPOINT isn’t set.
  • node_modules is missing from the package.
  • A native module was built for x86_64, which doesn’t run in the Arm64 environment.

Test the Endpoint With curl

A curl -i against the endpoint’s domainName checks that the server is up and shows the headers Lambda adds:

curl -i https://<domainName>/

A HomeRegion domain has the form <id>.<shard>.lambda-web.<region>.on.aws. The body should be the sample greeting. Lambda adds a lambda-web-request-id header to each request it forwards to your app and returns the same ID in the response. The response also includes lambda-web-logstream, which names the CloudWatch Logs stream for the execution environment that handled the request. Together, the two headers take you straight from a failing request to its logs.

Ship a Change With create-web-function-revision

To change a deployed Web Function, you create a new revision. You never edit the existing one. Add this route above app.listen:

app.get('/whoami', (request, response) => {
  response.json({
    requestId: request.get('lambda-web-request-id'),
    region: process.env.AWS_REGION,
  });
});

Repackage and upload with the same npm ci --omit=dev, zip and aws s3 cp commands as before. Then create the revision:

aws lambda-web create-web-function-revision \
    --function-name my-web-app \
    --build-config '{
      "codeConfig": {
        "s3Object": {
          "bucket": "amzn-s3-demo-bucket",
          "key": "samples/function.zip"
        }
      },
      "runtimeConfig": { "runtime": "nodejs24.x" }
    }' \
    --service-config '{
      "executionRoleArn": "arn:aws:iam::111122223333:role/my-web-app-execution-role"
    }'

Every revision is a complete, immutable snapshot. That’s why this command takes the full build and service configuration each time, not just the fields you changed. Run wait on the new revision ID, then curl https://<domainName>/whoami. The endpoint uses LatestRevision, so it moves traffic to the new revision as soon as that revision is Active. With the shortcut, the same change is just another run of aws lambda-web deploy --name my-web-app --code ..

The Old Way: serverless-http and API Gateway

Before Web Functions, Express on Lambda used the handler model. serverless-http wrapped the app, and API Gateway invoked the exported handler:

const serverless = require('serverless-http');
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello from Lambda!');
});

module.exports.handler = serverless(app);

The handler model and the Web Functions model differ in one way. The handler model turns API Gateway events into fake requests inside your process. The Web Functions model sends real HTTP to a server your app started itself. To migrate:

  1. Delete the module.exports.handler export.
  2. Remove the serverless-http dependency.
  3. Add the host/port line and app.listen(port, host).
  4. Remove the API Gateway resources once the Web Function endpoint is serving traffic.

Limits Worth Knowing

The AWS samples page lists three constraints for Lambda Web Functions:

  • No WebSocket upgrades: server-sent events work, but WebSockets don’t.
  • Hono needs ES modules: the Hono sample sets "type": "module" with npm pkg set type=module.
  • No session state in the environment: don’t store sessions in the execution environment’s memory. Keep them in an external store.

When you’re done, run aws lambda-web delete-web-function --function-name my-web-app. This removes the function’s endpoints and revisions. As the Creating a Web Function guide notes, it leaves the execution role, the S3 packages and the CloudWatch Logs log groups, which you delete separately.

Next Steps

On Lambda Web Functions, your Express app’s own app.listen(port, host) is the deployment contract. The handler export, the adapter and API Gateway are no longer needed. Take an existing app, add the endpoint line, deploy with aws lambda-web deploy, and confirm it with curl.

FAQs

Does one Lambda Web Function environment handle several Express requests at once?

Yes. A single Web Function execution environment serves multiple concurrent requests, unlike the one-request-per-environment model of Event Functions. Node.js handles them through async I/O on one shared event loop, so keep per-request data in request scope rather than global variables, and avoid synchronous CPU work that delays every in-flight request. The maxConcurrencyPerEnvironment revision setting caps how many requests one environment takes.

How long can a request or server-sent events stream run on a Web Function?

Each request is bound by the revision's timeoutSeconds setting, which defaults to 30 seconds and accepts 3 to 900 seconds. For server-sent events, set it to the longest stream you expect. When the timeout elapses, Lambda ends the response and the client receives the status code already sent with a truncated body. Changing the timeout requires a new revision.

How do I roll back a bad revision on a Web Function endpoint?

Run aws lambda-web update-web-function-endpoint to route all traffic to the previous known-good revision and set the auto-deployment mode to Disabled, then run aws lambda-web wait web-function-endpoint-updated. Because revisions are immutable, rollback is a routing change rather than a redeploy. List revision IDs with list-web-function-revisions, and keep the old S3 object version, since a revision fails if Lambda loses access to its code.

What happens if my Express app starts slowly, and how should it handle shutdown?

The server must begin listening on the address in AWS_LAMBDA_HTTP_ENDPOINT within 10 seconds of startup, so initialize only what the first request needs before calling app.listen. On scale-down, Lambda stops routing new requests, lets in-flight requests finish on a best-effort basis, then sends SIGTERM. A SIGTERM handler gets up to 1 second for cleanup and is not guaranteed to run in every failure mode.

DevTools for the frontend

Gain Debugging Superpowers

Unleash the power of session replay to reproduce bugs, track slowdowns and uncover frustrations in your app. Get complete visibility into your frontend with OpenReplay — the most advanced open-source session replay tool for developers.

Star on GitHub12k

We use cookies to improve your experience. By using our site, you accept cookies.