diff --git a/aws-node-express-web-app/README.md b/aws-node-express-web-app/README.md index feae46ded..b7fc03a27 100644 --- a/aws-node-express-web-app/README.md +++ b/aws-node-express-web-app/README.md @@ -12,119 +12,169 @@ authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4' # Express web app on AWS Lambda Web Functions -This example deploys an [Express](https://expressjs.com/) 5 server to [AWS Lambda Web Functions](https://docs.aws.amazon.com/lambda/latest/dg/web-functions.html). A web function runs your HTTP server as it is: [`src/server.js`](src/server.js) creates an Express app and calls `app.listen(process.env.PORT ?? 3000)`, with no handler and no adapter, so the same file runs on your machine and in AWS. +[![Verified](https://raw.githubusercontent.com/serverless/examples/verified/badges/aws-node-express-web-app.svg)](https://github.com/serverless/examples/blob/verified/results.json) -The whole configuration is one `webApps` entry: +This example deploys an [Express](https://expressjs.com/) 5 server to [AWS Lambda Web Functions](https://docs.aws.amazon.com/lambda/latest/dg/web-functions.html) with one `webApps` entry in `serverless.yml`. A web function runs your HTTP server as it is: [`src/server.js`](https://github.com/serverless/examples/blob/v4/aws-node-express-web-app/src/server.js) creates an Express app and calls `app.listen(process.env.PORT ?? 3000)`, with no handler and no adapter, so the same file runs on your machine and in AWS. -```yaml -webApps: - api: - entry: src/server.js -``` +## What it deploys -`entry` is the file that starts your server. On `serverless deploy`, the Framework: +- A Lambda web function, `aws-node-express-web-app--api`, that runs the Express server on Node.js 24 and arm64, with a revision that holds this code. +- A public HTTPS endpoint for the web function, `https://..lambda-web..on.aws`. +- An IAM role for the web function, and two CloudWatch Logs log groups: `/aws/lambda/web/aws-node-express-web-app--api` and the same name ending in `.local`. -- zips the service directory, with the production dependencies from `node_modules`, and uploads it to the deployment bucket; -- creates the web function, a revision that runs this code, and a public HTTPS endpoint for it; -- sets `PORT` and `HOSTNAME` from the address the runtime gives the server to listen on, before your code starts; -- creates the log groups the web function writes to, `/aws/lambda/web/aws-node-express-web-app--api` and the same name ending in `.local`. +## Prerequisites -The server answers three routes: +- [Node.js](https://nodejs.org/) 24 or later. Web functions run Node.js 24. +- The Serverless Framework CLI: `npm i -g serverless`. See [Getting started](https://www.serverless.com/framework/docs/getting-started). +- An AWS account, with credentials configured on your machine. See [AWS credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials). +- A Serverless account, or a license key in `SERVERLESS_LICENSE_KEY`. The CLI asks you to sign in the first time you run it. -| Route | Response | -| --- | --- | -| `GET /` | `{"message":"Hello from Express on AWS Lambda Web Functions!"}` | -| `GET /hello/:name` | `{"message":"Hello, !"}` | -| `GET /health` | `{"status":"ok"}` | - -## Requirements - -- Node.js 24. Web functions run Node.js 24. -- AWS credentials, and a Serverless Framework sign-in (`serverless login`). +## Deploy -## Run it locally +Install the dependencies and deploy from this directory: ```bash npm install -npm start -``` - -The server listens on port 3000: - -```bash -curl http://localhost:3000/health -``` - -```json -{ "status": "ok" } +serverless deploy ``` -## Deploy +Without options, the deploy uses the stage `dev` and the Region `us-east-1`. To change them, pass `--stage` or `--region`, and pass the same options to every later command, including `serverless remove`. -```bash -serverless deploy -``` +You should see output like this: ```text Deploying "aws-node-express-web-app" to stage "dev" (us-east-1) -✔ Service deployed to stack aws-node-express-web-app-dev (51s) +✔ Service deployed to stack aws-node-express-web-app-dev (49s) endpoint: https://xxxxxxxxxxxxxxxx.xxx.lambda-web.us-east-1.on.aws ``` `endpoint` is the web app's URL. `serverless info` prints it again later. The endpoint is public: anyone with the URL can call it. -## Call the app +## Try it + +Call the `/hello/:name` route with the URL the deploy printed: ```bash curl https://xxxxxxxxxxxxxxxx.xxx.lambda-web.us-east-1.on.aws/hello/serverless ``` +Express answers with status 200 and this body: + ```json -{ "message": "Hello, serverless!" } +{"message":"Hello, serverless!"} ``` -`serverless invoke --web-app` sends a request to the web app without the URL at hand, and prints the status, the headers and the body: +The server answers three routes: + +| Route | Response | +| --- | --- | +| `GET /` | `{"message":"Hello from Express on AWS Lambda Web Functions!"}` | +| `GET /hello/:name` | `{"message":"Hello, !"}` | +| `GET /health` | `{"status":"ok"}` | + +`serverless invoke --web-app` sends the same request without the URL at hand, and prints the status, the headers and the body: ```bash -serverless invoke --web-app api --path /health +serverless invoke --web-app api --path /hello/serverless ``` ```text HTTP 200 OK -content-length: 15 +content-length: 32 content-type: application/json; charset=utf-8 -date: Thu, 15 Oct 2026 08:35:16 GMT -etag: W/"f-VaSQ4oDUiZblZNAEkkN+sX+q3Sg" -lambda-web-logstream: 2026/10/15/aws-node-express-web-app-dev-api[rev-yuqer]dbda8e7bbdc74f8694d1f19dfaff2990 -lambda-web-request-id: RDG5RQS3KAYQEA2CTWUPVK2774 +date: Wed, 25 Nov 2026 21:57:13 GMT +etag: W/"20-iauLf21YNtkc/qCizlfTPLAhpMc" +lambda-web-logstream: 2026/11/25/aws-node-express-web-app-dev-api[rev-xxxxx]xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +lambda-web-request-id: XXXXXXXXXXXXXXXXXXXXXXXXXX x-powered-by: Express -{"status":"ok"} +{"message":"Hello, serverless!"} ``` The status line and headers go to standard error and the body to standard output, so `serverless invoke --web-app api --path /health | jq` sees the JSON alone. A status of 400 or above makes the command exit with code 1, so it also works as a health check after a deploy. -Edit `src/server.js` and run `serverless deploy` again to publish the change. A deploy with no changes to the code or configuration is skipped. +## How it works + +The whole configuration is one `webApps` entry in [`serverless.yml`](https://github.com/serverless/examples/blob/v4/aws-node-express-web-app/serverless.yml): + +```yaml +webApps: + api: + entry: src/server.js +``` + +`api` is the web app's name, and `entry` is the file that starts your server. On `serverless deploy`, the Framework: + +- zips the service directory, with the production dependencies from `node_modules`, and uploads it to the deployment bucket; +- adds a start file to the package that sets `PORT` and `HOSTNAME` from the address Lambda gives the server to listen on, then starts `entry`; +- creates the web function, a revision that runs this code, the public endpoint, and the stack output `ApiWebFunctionUrl` with the endpoint's URL; +- creates the log groups the web function writes to. + +`src/server.js` is an ordinary Express server. It reads the port from `PORT`, which the start file sets, and listens without naming a host: + +```javascript +import express from 'express' + +const app = express() + +app.get('/hello/:name', (req, res) => { + res.json({ message: `Hello, ${req.params.name}!` }) +}) + +const port = process.env.PORT ?? 3000 + +app.listen(port, () => { + console.log(`Listening on port ${port}`) +}) +``` -## Logs +Lambda sends many concurrent requests to each execution environment, so keep per-request state in the request, not in module-level variables. The server must start listening within 10 seconds of starting. Edit `src/server.js` and run `serverless deploy` again to publish a change: the new revision serves the same URL. A deploy with no changes to the code or configuration is skipped. + +### Run it locally + +The same file runs on your machine: + +```bash +npm start +curl http://localhost:3000/health +``` + +```json +{"status":"ok"} +``` + +### Logs The server's output goes to CloudWatch Logs. `serverless logs --web-app` prints it, together with Lambda's own lines about starting the server and serving requests: ```bash serverless logs --web-app api -serverless logs --web-app api --tail ``` ```text -{"timestamp":"2026-10-15T08:35:16.370Z","level":"INFO","message":"Listening on port 3000","region":"us-east-1"} -{"timestamp":"2026-10-15T08:35:16.375Z","level":"INFO","type":"platform.initReport","region":"us-east-1","record":{"status":"success","instanceId":"d6a20bdb-df41-455a-8c4b-b0df43ebf406","initializationType":"native-http","metrics":{"totalDurationMs":390.000,"totalCpuTimeMs":210.000,"maxMemoryUsageMb":154}}} +{"timestamp":"2026-11-25T21:57:07.999Z","level":"INFO","message":"Listening on port 3000","region":"us-east-1"} +{"timestamp":"2026-11-25T21:57:08.005Z","level":"INFO","type":"platform.initReport","region":"us-east-1","record":{"status":"success","revisionId":"rev-xxxxx","instanceId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","initializationType":"native-http","metrics":{"totalDurationMs":339.000,"totalCpuTimeMs":294.000,"maxMemoryUsageMb":155}}} ``` -## Remove +Add `--tail` to follow new lines as they arrive. + +## Cost and cleanup + +While the example is deployed, Lambda bills each request to the web function, the CPU time the server uses, and the memory each execution environment uses while it runs. CloudWatch Logs bills for the log data it stores, and Amazon S3 for the uploaded code in the deployment bucket. See [Lambda pricing](https://aws.amazon.com/lambda/pricing/), [CloudWatch pricing](https://aws.amazon.com/cloudwatch/pricing/) and [S3 pricing](https://aws.amazon.com/s3/pricing/). Remove the stack, with the web function, its endpoint, its role and its log groups, when you are done: ```bash serverless remove ``` -This deletes the stack, with the web function, its endpoint and its log groups, and the uploaded code. +```text +Removing "aws-node-express-web-app" from stage "dev" (us-east-1) +✔ Service aws-node-express-web-app has been successfully removed (28s) +``` + +## Next steps + +- Read the [Web Apps docs](https://www.serverless.com/framework/docs/providers/aws/guide/web-apps) for TypeScript, environment variables, IAM, VPC access, scaling limits and IAM-authorized endpoints. +- Deploy a Next.js app from its standalone output with the [Next.js web app example](https://github.com/serverless/examples/tree/v4/aws-node-nextjs-web-app). +- Serve a frontend and an API from one domain with the [web apps on a custom domain example](https://github.com/serverless/examples/tree/v4/aws-node-web-app-custom-domain). +- Serve requests to the deployed URL from your working copy with [Dev Mode](https://www.serverless.com/framework/docs/providers/aws/guide/dev-mode#web-apps). diff --git a/aws-node-express-web-app/example.json b/aws-node-express-web-app/example.json new file mode 100644 index 000000000..3c4c2cda9 --- /dev/null +++ b/aws-node-express-web-app/example.json @@ -0,0 +1,30 @@ +{ + "title": "Express web app on AWS Lambda Web Functions", + "description": "This example deploys a plain Express server to AWS Lambda Web Functions with the Serverless Framework. The server has no Lambda-specific code and answers on a public HTTPS endpoint.", + "language": "node", + "platform": "AWS", + "framework": "v4", + "author": { + "name": "Serverless, Inc.", + "link": "https://github.com/serverless", + "avatar": "https://avatars1.githubusercontent.com/u/13742415?s=200&v=4" + }, + "services": [ + "lambda", + "iam", + "logs" + ], + "test": { + "tier": "fast", + "checks": [ + { + "http": { + "output": "ApiWebFunctionUrl", + "path": "/hello/serverless", + "status": 200, + "contains": "Hello, serverless!" + } + } + ] + } +} diff --git a/aws-node-express-web-app/package-lock.json b/aws-node-express-web-app/package-lock.json index e8bd14853..73418abb0 100644 --- a/aws-node-express-web-app/package-lock.json +++ b/aws-node-express-web-app/package-lock.json @@ -8,7 +8,7 @@ "name": "aws-node-express-web-app", "version": "1.0.0", "dependencies": { - "express": "^5.2.1" + "express": "5.3.0" } }, "node_modules/accepts": { @@ -25,18 +25,18 @@ } }, "node_modules/body-parser": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.3.0.tgz", - "integrity": "sha512-2cGmJupaNgg+QUwVLAucDuWuoMZ6EX9iHDRswZ5lsNYEmwPaRknMPCLZz07yTzVq/83p4o/wzbDZbBrTvGGTIw==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.4.0.tgz", + "integrity": "sha512-Ifk1KcLrxaGt1occQagQNFNNOqRt1LFuehEnHHzB8Z6cRgIxRU1TBaiIsUiCdA+gy5PL7Y1IX31DNKkRmxtXjQ==", "license": "MIT", "dependencies": { "bytes": "^3.1.2", - "content-type": "^2.0.0", + "content-type": "^2.1.0", "debug": "^4.4.3", "http-errors": "^2.0.1", - "iconv-lite": "^0.7.2", + "iconv-lite": "^0.7.3", "on-finished": "^2.4.1", - "qs": "^6.15.2", + "qs": "^6.16.1", "raw-body": "^3.0.2", "type-is": "^2.1.0" }, @@ -602,9 +602,9 @@ } }, "node_modules/qs": { - "version": "6.16.0", - "resolved": "https://registry.npmjs.org/qs/-/qs-6.16.0.tgz", - "integrity": "sha512-h6fhOIaRrID2CbEY2fqs+7t+UXZo+MLAnU5gRIq85uFtdiUPCdsApMlHhXogKVM4HM2DVbIjGNTTYH2OcmP1vA==", + "version": "6.17.1", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.17.1.tgz", + "integrity": "sha512-jd3COkaKVgGUW+ZHyEHCG9bH7RoaaiWuMgzsIBjUMVjLAaWGWpoUAVwljVvBHxRu/2qEHnadLGZrkutYUBPQJA==", "license": "BSD-3-Clause", "dependencies": { "es-define-property": "^1.0.1", diff --git a/aws-node-express-web-app/package.json b/aws-node-express-web-app/package.json index d71638c84..51bd67147 100644 --- a/aws-node-express-web-app/package.json +++ b/aws-node-express-web-app/package.json @@ -8,6 +8,6 @@ "start": "node src/server.js" }, "dependencies": { - "express": "^5.2.1" + "express": "5.3.0" } }