aws-cli
dynamoDB
local setup
cloud computing
tutorial

How to use aws-cli with local dynamoDB ?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Amazon Web Services Command Line Interface (AWS CLI) is a unified tool that allows users to interact with AWS services, including DynamoDB, a NoSQL database service. This article focuses on using AWS CLI with a local instance of DynamoDB. Using DynamoDB locally can be beneficial for development and testing purposes without incurring costs.

Prerequisites

Before diving into AWS CLI with local DynamoDB, ensure the following are installed on your machine:

  1. AWS CLI: You can install it by following the instructions provided here.
  2. Java: Required to run local DynamoDB. Install Java from this link.
  3. Local DynamoDB: Downloadable as a JAR file from the AWS DynamoDB local page.

Once these tools are in place, you can proceed to set up and use DynamoDB locally.

Setting Up Local DynamoDB

Step 1: Running DynamoDB Locally

Extract the downloaded .zip file for DynamoDB Local. Navigate to the extracted directory and run the following command to start DynamoDB:

bash
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

The -sharedDb flag ensures that all requests are directed to a single database instance, useful for development environments.

Step 2: Configuring AWS CLI

Configure AWS CLI to work with the local version of DynamoDB. Specifically, you'll change the endpoint URL to point to the local DynamoDB. Use the AWS CLI configure command:

bash
aws configure

During this process, provide dummy AWS Access Key and Secret Key, as local DynamoDB doesn't require actual keys. Ensure the Region is set (e.g., us-west-2), and output format (e.g., json).

AWS CLI Operations on Local DynamoDB

Below are examples of basic operations you can perform on a local DynamoDB instance using AWS CLI:

Creating a Table

To create a table named MusicCollection with Artist as the partition key, use the following command:

bash
1aws dynamodb create-table \
2    --table-name MusicCollection \
3    --attribute-definitions AttributeName=Artist,AttributeType=S \
4    --key-schema AttributeName=Artist,KeyType=HASH \
5    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
6    --endpoint-url http://localhost:8000

Inserting Data

To put an item into the MusicCollection table:

bash
1aws dynamodb put-item \
2    --table-name MusicCollection \
3    --item '{"Artist": {"S": "The Beatles"}, "AlbumTitle": {"S": "Abbey Road"}}' \
4    --endpoint-url http://localhost:8000

Querying Data

To retrieve an item by its partition key:

bash
1aws dynamodb get-item \
2    --table-name MusicCollection \
3    --key '{"Artist": {"S": "The Beatles"}}' \
4    --endpoint-url http://localhost:8000

Deleting a Table

To delete the table:

bash
aws dynamodb delete-table \
    --table-name MusicCollection \
    --endpoint-url http://localhost:8000

Troubleshooting

Connectivity Issues

Ensure that DynamoDB Local is running on http://localhost:8000. If not, review your initial setup command and try running it again.

AWS CLI Errors

If you run into errors regarding credentials, double-check your AWS config file. Running DynamoDB locally does not require valid AWS credentials but ensures no syntax errors exist in your configuration.

Summary Table

OperationAWS CLI Command ExampleEndpoint
Create Tableaws dynamodb create-table ...http://localhost:8000
Insert Itemaws dynamodb put-item ...http://localhost:8000
Query Itemaws dynamodb get-item ...http://localhost:8000
Delete Tableaws dynamodb delete-table ...http://localhost:8000
Configurationaws configure (with dummy credentials)N/A

Conclusion

Leveraging AWS CLI with local DynamoDB greatly enhances the development and testing pipeline by simulating AWS services without cost. By following the instructions above, you can set up and interact with DynamoDB locally, performing CRUD operations using AWS CLI. For a complete list of operations and advanced configurations, refer to the AWS CLI and DynamoDB documentation.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.