Real-time metrics dashboard with Server-Sent Events on AWS Lambda Web Functions

Browser → AWS Lambda Web Functions

Stream live metrics to the browser over server-sent events (SSE) from a zero-dependency Node.js HTTP server running as an AWS Lambda Web Function.

This pattern deploys a plain Node.js HTTP server as an AWS Lambda Web Function. It has no framework, no dependencies, and no build step. The server streams a metrics snapshot to the browser every 2 seconds over a single SSE connection that stays open.
A web function runs a real server process. The runtime passes the listen address in the AWS_LAMBDA_HTTP_ENDPOINT environment variable, and the server listens on it as it would anywhere else. Because the process stays alive between requests, it can hold long-lived connections open and push data to the client, which the Lambda event model cannot do.
The dashboard reports values the process measures about itself: execution environment uptime, CPU time consumed, open SSE connections, and the serving Region. Select Generate CPU load to run about 1.5 seconds of SHA-256 hashing and watch CPU time climb and fall back. The environment stays up for minutes while using a fraction of a second of CPU, because you pay for active CPU time, not time spent waiting.
Each execution environment handles many concurrent requests (64 by default, up to 128). As environments near that limit, Lambda adds more and scales back to zero when traffic falls. Optionally, deploy a second MultiRegion endpoint to get one global URL that uses Amazon Route 53 latency-based routing to serve each request from the nearest Region.

< Back to all patterns
AWS Top Pick

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub

Variants

This pattern is available in the following frameworks/languages:

AWS CLI:Node.js
AWS SAM:Node.js

Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/lambda-web-sse-dashboard/nodejs/awscli

Deploy

npm installaws lambda-web deploy --name lambda-web-sse-dashboard --code . --timeout-seconds 900


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

Delete the function and its endpoints: aws lambda-web delete-web-function --function-name lambda-web-sse-dashboard.
See the GitHub repo for removing the execution role and log groups.

Created by:

Julian Wood

Julian Wood

Julian is a developer advocate at AWS who loves helping developers learn how serverless and AI technologies can transform the way they build and run applications in the cloud.

Follow on LinkedIn