How to use DynamoDB locally with Lambda
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Setting up and developing applications with AWS DynamoDB and AWS Lambda locally can greatly speed up your development process. This approach allows you to test your serverless applications without incurring costs or needing an internet connection. In this guide, we will explain how to set up and use AWS DynamoDB with AWS Lambda locally.
Prerequisites
- AWS CLI: Ensure AWS Command Line Interface is installed and configured on your local machine.
- AWS SAM CLI: The Serverless Application Model Command Line Interface aids in building serverless applications.
- Docker: Use this to run Lambda functions and DynamoDB locally.
- Java Runtime Environment: It's required for DynamoDB Local.
Setting Up DynamoDB Local
To start, you'll need to set up DynamoDB Local, which can be run as a Java application.
- Download DynamoDB Local: You can download DynamoDB Local from the AWS website as a
.tar.gzor.zipfile. - Extract the file: Once downloaded, extract the files to a desired location.
- Run DynamoDB: Navigate to the directory where DynamoDB Local is extracted and run it using the command:
The -sharedDb option is used to allow all clients to share a single database file. You can confirm that DynamoDB Local is running by visiting http://localhost:8000/ in a web browser.
Creating a Table in DynamoDB Local
To interact with DynamoDB Local, you can use the AWS CLI. Here’s how to create a table:
Here, we are creating a table named "Music" with a composite primary key comprised of "Artist" (HASH) and "SongTitle" (RANGE).
Running AWS Lambda Locally
Lambda functions can be executed locally using the AWS SAM CLI. Here’s how you can set it up:
- Create Lambda Function: Use
sam initto generate a new Lambda project. This provides boilerplate code and configuration. - Configure
template.yaml: Update yourtemplate.yamlas follows to specify the resource required by the Lambda function:
- Run the Lambda Function: Use the
sam local invokecommand to execute your Lambda function locally.
Make sure that event.json contains the event data necessary for your Lambda function.
Connecting Lambda to DynamoDB Locally
Your Lambda function needs to connect to DynamoDB Local. Setup the SDK in your Lambda as follows:
Summary Table
This table encapsulates the key points when setting up and using DynamoDB Local with Lambda:
| Key Point | Description |
| Download & Run DynamoDB Local | Java application with shared DB capabilities |
| Create Table | Use AWS CLI to define tables with attribute schemas |
| Configure Lambda with SAM CLI | Use sam init and configure AWS resources |
| Local Connection Configuration | Use boto3 library with endpoint as http://localhost:8000 |
| Environment Variable for Table Name | Set in template.yaml |
| Execution | sam local invoke for testing Lambda locally |
Additional Tips
- Error Handling: Ensure proper error catching and logging to troubleshoot issues effectively.
- Testing Frameworks: Leverage testing frameworks to automate tests of your Lambda functions.
- Data Lifecycle: Consider how to persist data changes between starts and stops of DynamoDB Local, particularly when used in a shared or testing environment.
By following these steps, you can efficiently build and test applications that interact with DynamoDB using AWS Lambda on your local machine. This local development environment setup helps you to save costs and time during the development lifecycle.
Related reading
- How to use kafka and storm on cloudfoundry?
- How to use MFA with AWS CLI?
- How to use multiple AWS accounts from the command line?
- How to use PodTemplate
- How to use imagePullSecrets in Helm 3
- how to use kafka acls?
- How to use SageMaker Estimator for model training and saving
- How to use spot instance with amazon elastic beanstalk?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.