Deploy an Express API to AWS Lambda with AWS SAM (Step by Step)
You already have an Express API, and you want to run it on AWS Lambda without rewriting every route. In this guide, we'll deploy an existing Express app to Lambda behind an API Gateway HTTP API, using AWS SAM (Serverless Application Model) for infrastructure as code.
By the end, you'll have a public HTTPS endpoint, local testing, logs, and a one-command cleanup.
In an earlier post, I wrote about what to watch for when migrating a Node.js endpoint to Lambda. This is the hands-on version.
What we're building
Client │ HTTPS ▼ API Gateway (HTTP API) │ every path and method ▼ AWS Lambda (Node.js 24) │ ▼ Express app (unchanged routes) The key piece is @codegenie/serverless-express. It translates API Gateway events into normal HTTP requests that Express understands, and converts Express responses back into the format Lambda returns.
Prerequisites
- An AWS account and the AWS CLI configured (
aws configureor IAM Identity Center) - The AWS SAM CLI installed (
sam --version) - Node.js 24 locally (the current version of
@codegenie/serverless-expressrequires Node.js 24 or later) - Docker, only if you want to test locally with
sam local
Step 1: Project structure
express-lambda-api/ ├── template.yaml └── src/ ├── app.js # your Express app ├── lambda.js # Lambda entry point ├── local.js # run locally with app.listen() └── package.json The most important design choice: app.js exports the app and never calls app.listen(). That way, the same app can run locally on a port and inside Lambda.
Step 2: The Express app
mkdir -p express-lambda-api/src && cd express-lambda-api/src npm init -y npm install express @codegenie/serverless-express src/app.js:
const express = require('express'); const app = express(); app.use(express.json()); app.get('/health', (req, res) => { res.json({ status: 'ok' }); }); app.get('/users/:id', (req, res) => { res.json({ id: req.params.id, name: 'Demo User' }); }); app.post('/users', (req, res) => { res.status(201).json({ created: req.body }); }); // Fallback for unknown routes app.use((req, res) => { res.status(404).json({ error: 'Not found' }); }); module.exports = app; src/local.js (for normal local development):
const app = require('./app'); const port = process.env.PORT || 3000; app.listen(port, () => console.log(`Listening on http://localhost:${port}`)); Step 3: The Lambda handler
src/lambda.js:
const serverlessExpress = require('@codegenie/serverless-express'); const app = require('./app'); // Created once per execution environment and reused across warm invocations exports.handler = serverlessExpress({ app }); Notice that the handler is created outside the request path. Lambda reuses execution environments between invocations, so anything initialized at module level (the Express app, database clients, configuration) is reused while the environment stays warm.
Step 4: The SAM template
template.yaml in the project root:
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Express API on AWS Lambda with an HTTP API Globals: Function: Runtime: nodejs24.x Architectures: - arm64 MemorySize: 512 Timeout: 10 Resources: ApiFunction: Type: AWS::Serverless::Function Properties: CodeUri: src/ Handler: lambda.handler Events: Root: Type: HttpApi Properties: Path: / Method: ANY Proxy: Type: HttpApi Properties: Path: /{proxy+} Method: ANY Outputs: ApiUrl: Description: Base URL of the HTTP API Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com" A few things worth explaining:
-
HttpApievents: SAM creates an API Gateway HTTP API for you. HTTP APIs are simpler and cheaper than REST APIs, and they're a good fit when you just need to proxy requests to a function. -
/{proxy+}withANY: every path and method goes to the same function, and Express does the routing. The extra/route catches requests to the root path. -
arm64: Lambda functions on Graviton (arm64) are priced lower per GB-second than x86. A pure JavaScript app like this one runs on arm64 without changes. -
ServerlessHttpApi: this is the logical ID SAM gives the HTTP API it creates implicitly, so we can output its URL.
Step 5: Build and deploy
From the project root:
sam build sam deploy --guided sam build installs the dependencies from src/package.json and packages the function. sam deploy --guided asks a few questions the first time (stack name, region, and so on) and saves your answers to samconfig.toml, so future deploys are just:
sam build && sam deploy When the deploy finishes, SAM prints the ApiUrl output. Test it:
curl https://abc123.execute-api.us-east-1.amazonaws.com/health # {"status":"ok"} curl -X POST https://abc123.execute-api.us-east-1.amazonaws.com/users \ -H "Content-Type: application/json" \ -d '{"name":"Sahinur"}' # {"created":{"name":"Sahinur"}} (Replace the URL with your own ApiUrl.)
Step 6: Test locally
You have two options.
Plain Express, fastest for day-to-day development:
node src/local.js Lambda-like environment with SAM (requires Docker):
sam build sam local start-api sam local start-api runs your function in a container that mimics the Lambda runtime, which is useful for catching differences between your machine and Lambda.
Step 7: Logs
Stream your function's CloudWatch logs from the terminal:
sam logs --stack-name express-lambda-api --name ApiFunction --tail console.log and console.error in Express end up in CloudWatch Logs automatically.
Things to watch for
Cold starts. When Lambda creates a new execution environment, it loads your code and dependencies first. Keep dependencies lean, and avoid heavy work at startup.
Database connections. Create clients once at module level and reuse them. Opening a new connection on every request can overwhelm your database when traffic spikes. I covered this in more detail in the migration post.
Long-running work. Lambda has a maximum timeout of 15 minutes, and API Gateway HTTP APIs have a much shorter integration timeout (30 seconds). Background jobs belong in a queue (SQS) or a separate function, not inside an HTTP request.
Not everything fits. WebSockets on the same server, in-memory sessions, and local file storage don't work the same way on Lambda. If your app depends on them, see my comparison of EC2 vs Lambda for Node.js backends.
Clean up
To avoid leaving resources behind, delete the stack when you're done experimenting:
sam delete This removes the Lambda function, the HTTP API, and the IAM role SAM created.
Summary
| Step | Command |
|---|---|
| Build | sam build |
| First deploy | sam deploy --guided |
| Later deploys | sam build && sam deploy |
| Local (Express) | node src/local.js |
| Local (Lambda-like) | sam local start-api |
| Logs | sam logs --name ApiFunction --tail |
| Clean up | sam delete |
The big idea is to keep your Express app independent of where it runs. Once app.js doesn't know whether it's behind app.listen() or a Lambda handler, moving between EC2 and Lambda becomes a deployment decision instead of a rewrite.
Have you moved an Express app to Lambda? What surprised you? Let me know in the comments.