DynamoDB with Serverless Framework: API and Local Setup

Written by Rafal Wilinski
Last updated: October 23rd, 2026
Define DynamoDB tables in the resources block of serverless.yml, then pass each table name and the required IAM permissions to your Lambda functions. Serverless Framework deploys the table through CloudFormation alongside the API. The example below uses Framework v4, Node.js 24 and AWS SDK for JavaScript v3 to create, list and delete animals.
Why use DynamoDB with Serverless Framework?
Lambda and DynamoDB remove server provisioning from this application. Framework connects API Gateway routes, Lambda handlers, IAM roles and the table in one deployment. DynamoDB on-demand capacity bills for requests, while storage and optional features such as backups have their own charges. A quiet application is not necessarily free.
Framework v4 requires authentication through the Dashboard or a license key. It is free for individuals and organizations at or below $2 million in annual revenue; organizations above that threshold need a paid subscription. Framework licensing is separate from your AWS bill. Check the v4 upgrade and licensing guide when adopting it. Framework v3 is no longer maintained.
If you want AWS's own template extension and local Lambda tooling, compare AWS SAM. If you prefer TypeScript resource linking, compare SST. Both can work with DynamoDB, but their configuration and deployment engines differ.
Step 1: create a project
Use a supported Node.js installation, AWS CLI v2 and an AWS profile. Check the account before deploying:
The login authenticates the Framework CLI; your AWS credentials authorize deployment. Teams using a license key should follow the vendor's private credential configuration instead of committing it. Create serverless.yml and handler.mjs in this directory. No archived template repository is needed.
Step 2: define the table, routes and permissions
Use this complete serverless.yml:
The stage-specific name keeps development and production tables separate. You can instead use Ref: AnimalsTable for the environment variable when CloudFormation generates the physical name; the explicit name here also makes local setup predictable.
provider.iam.role.statements grants exactly the data operations used below on this table. It does not grant schema-management permissions to the application. All three functions share this role in the example; separate per-function roles can narrow access further. If you later Query a GSI, add dynamodb:Query and its index ARN as well.
The resources block accepts normal CloudFormation resources. Backups, TTL, indexes, resource policies and throughput caps use the same properties as the CloudFormation examples. Only key attributes belong in AttributeDefinitions; do not declare every field an animal might contain.
The example disables the v4 esbuild integration and packages ordinary JavaScript with production dependencies. handler.mjs is already an ES module. Inspect the packaged ZIP to confirm both SDK packages are present, rather than relying on the runtime's bundled SDK version. Framework also supports built-in bundling; see its build configuration.
Step 3: write the SDK v3 handlers
Start handler.mjs with the shared client. A local endpoint is used only when serverless-offline sets IS_OFFLINE:
The local credentials are dummy signing values for DynamoDB Local. Deployed functions use their execution role. Do not leave a local endpoint in production configuration.
Create an animal
Append this handler. It validates the key and rejects a duplicate name instead of silently replacing an existing item:
A Put without the condition overwrites an item with the same key. Decide whether the application should reject, replace or update duplicates, and express that choice explicitly. Add a full request schema and size validation for your own fields before accepting arbitrary user data.
List animals with a continuation token
Append this handler. Scan reads a page, not necessarily the whole table:
Call again with nextToken until it is null. This is a small catalog example, not a recommended access pattern for every application. For per-owner lists or large datasets, design a partition key or index that supports Query. A continuation token is not authentication, and Scan does not provide a consistent snapshot across pages.
Delete an animal
Append the final handler:
DeleteItem succeeds even when the item is absent, making this delete naturally repeatable. Add a condition if the application must distinguish missing items, and enforce ownership in the handler before allowing users to delete records.
Step 4: package, deploy and test
Inspect the generated CloudFormation and packaged functions before deployment. Use the printed HTTP API URL for these requests:
Check the duplicate-create response and follow pagination with enough test data to require another page. Function logs and DynamoDB metrics should show successful calls without unexpected throttles.
These routes are public demonstration endpoints. Before serving real users, add an authorizer, derive identity from verified claims, validate request fields and enforce per-item permissions. Scoped Lambda IAM limits which AWS resources the function can touch; it does not authenticate the person calling the HTTP API.
Local development with serverless-dynamodb
Use the maintained serverless-dynamodb plugin, a continuation of serverless-dynamodb-local. The older package has not had an npm release since 2021. The replacement README still lists Framework v1 through v3, so test its compatibility with your pinned v4 release rather than assuming formal v4 support.
Install it alongside serverless-offline and add these top-level settings:
Keep the DynamoDB plugin before serverless-offline. With Docker running, start local development:
The plugin creates local tables from the resources definitions. inMemory: true discards data when stopped; use persistent storage when retaining local fixtures matters. If you use its Java mode instead, current DynamoDB Local requires JRE 17 or newer, despite the plugin README's older Java 11 requirement.
Local testing does not prove IAM, PITR, TTL cleanup, global replication or production throughput behavior. If a plugin version is incompatible, run Docker directly and create the table with the CLI as shown in running DynamoDB locally. Point the SDK at that endpoint explicitly.
Monitoring, retention and existing boilerplates
Monitor Lambda errors and latency, API errors, table throttles and GSI backfill. The SDK retries eligible transient failures, but your application still needs an idempotency strategy for operations that may be repeated. A failed response does not always mean the write did not happen.
serverless remove removes the application's stack, but the template's retention policies preserve this table. Deletion protection additionally blocks a direct table deletion. Document who owns retained tables and how they are backed up; they continue to incur charges after the application is removed.
The older Dynobase API boilerplate is useful as historical context, not a substitute for checking current SDK, runtime and Framework dependencies. Review its versions and authorization model before reuse. For a database shared by several services, consider managing it independently with Terraform and granting each application's role the necessary access.
Frequently Asked Questions
Where does DynamoDB go in serverless.yml?
AWS::DynamoDB::Table under resources.Resources. Pass its name through environment variables and grant the Lambda role the required DynamoDB actions on its ARN.Is Serverless Framework v4 free?
Which plugin should I use for DynamoDB Local?
serverless-dynamodb, replacing the old serverless-dynamodb-local package. Its README does not formally list Framework v4 support, so verify your pinned combination or use Docker directly.Does removing a Serverless service delete its DynamoDB table?
Does one Scan return every animal?
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today
Product Features
DynamoDB Tools
- SSH GUI Client
- Chat With Cloud
- MongoDB GUI Client
- AWS Cost Monitoring
- DynamoDB Query Builder
- DynamoDB Code Examples
- DynamoDB Node.js Queries
- DynamoDB JSON Converter
- DynamoDB UUID Generator
- DynamoDB Local Admin GUI
- DynamoDB Pricing Calculator
- DynamoDB Single-Table Design
- Best DynamoDB GUI Clients
- Dynobase vs DynoTable