Express.js CRUD API on AWS Lambda Web Functions with Amazon DynamoDB

Client → Lambda Web Function (Express) → DynamoDB

Run an unmodified Express.js REST API as a Lambda Web Function that reads and writes items in an Amazon DynamoDB table, using AWS SAM.

This pattern deploys a standard Express.js 5 application as an AWS Lambda Web Function with a public HTTPS endpoint and an Amazon DynamoDB table. Lambda Web Functions run a long-lived HTTP server and forward requests to it directly, so the Express app needs no adapter, no Lambda handler and no event-to-request mapping. The same index.js runs locally with node index.js and on AWS.
The API implements the usual CRUD surface: POST /items creates an item with a conditional write that returns 409 on a duplicate id, GET /items/{id} reads one item and returns 404 when it is missing, PUT /items/{id} updates name, description or metadata and fails with 404 if the item does not exist, and GET /items scans the table with cursor pagination through the limit and lastKey query parameters. Missing required fields return 400. Every response includes the Region that served it, which becomes useful when the same code is deployed behind a MultiRegion endpoint with a DynamoDB global table.
The DynamoDB DocumentClient is created once per sandbox and reused across the many concurrent requests that a single sandbox handles, and it authenticates with the function's execution role. That role is defined explicitly in the template so it can carry AWSLambdaBasicExecutionRole for CloudWatch Logs plus GetItem, PutItem, UpdateItem and Scan scoped to the table ARN. The table name is generated by CloudFormation and injected through the TABLE_NAME environment variable.
A single AWS::Serverless::WebFunction resource expands into AWS::Lambda::WebFunction, an immutable AWS::Lambda::WebFunctionRevision that references the zip in a versioning-enabled S3 bucket, and an AWS::Lambda::WebFunctionEndpoint routed 100% to that revision. The endpoint uses AuthType ApplicationManaged, so it is publicly reachable and the application owns authentication; switch to IamAuth to accept only SigV4-signed requests.
Because the code bundle contains node_modules and no build step runs at deploy time, install production dependencies before zipping. To ship a change, upload the zip under a new S3 key and deploy again; CloudFormation creates a new immutable revision only when the revision's properties change, and the LatestRevision endpoint mode moves traffic to it as soon as it is active.

< Back to all patterns
AWS Top Pick

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/lambda-web-dynamodb-api/nodejs/sam

Deploy

See the GitHub repo for deployment instructions.


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

Delete the stack: sam delete --stack-name lambda-web-dynamodb-api-sam.

Created by:

Jon Loinaz Anton

Jon Loinaz Anton

Enterprise Solutions Architect at AWS based in Spain, helping large organizations adopt serverless and generative AI.

Follow on LinkedIn