Local DynamoDB Installation and AWS CLI Commands for DynamoDB
Just to give some brief background information; you might already know that Amazon DynamoDB is a fully managed key-value and document database, a NoSQL data platform for developers from Amazon Web Services, AWS. Amazon DynamoDB is an online service provided by AWS and has a price policy for your usage. If you are a developer building applications using DynamoDB as your data store or database, you will require internet connection to consume Amazon DynamoDB service and pay for the usage during your developments based on your service consumption. To encourage developers and overcome these problems, AWS provides a local download of DynamoDB database with an easy DynamoDB setup on your local computer. Using local DynamoDB instance, as a developer you will not require an internet connection and will not pay anything for your local DynamoDB setup.
Programmers can interact with local DynamoDB using AWS CLI commands for operations like create table, list tables and delete table, etc. Just like AWS CLI tool, developers can also use Python codes to access local DynamoDB for data operations.
This tutorial will follow below steps to cover the requirements of a software developer to work with Amazon DynamoDB locally using AWS CLI tool commands:
Install Local DynamoDB on your machine (Windows PC)
Configure AWS CLI profile for local DynamoDB access
Execute sample AWS CLI commands to create table, list tables and delete table
How to Install Local DynamoDB
If you want to develop your applications using local DynamoDB installation, one of the methods you can use is deploying DynamoDB locally on your computer via downloading DynamoDB Local as executable .jar file
Developers can find most recent download links for various platforms including Windows, macOS or Linux at Amazon DynamoDB Developer Guide portal.
You can download executable jar file of the DynamoDB Local in zip format and extract it on a suitable folder where you want to install and run it.
I downloaded the local DynamoDB setup zip file in folder "C:\My\DynamoDB" and extracted the compressed installation files actually including the DynamoDBLocal.jar file and others as seen in below screenshot.
Then to start DynamoDB locally on your computer, open a command prompt window, navigate to the directory where you extracted DynamoDBLocal.jar, and enter the following command:
As seen in following screenshot, if everything is OK you will have a similar response informing the developers about the initializing of local DynamoDB platform.
Create AWS Profile for Local DynamoDB Access
After we have enabled local DynamoDB on our computer, it is time to setup the required profile for necessary credentials to access local DynamoDB installation using AWS CLI commands or using Python, etc programmatically.
For setting up the permissions to access locally installed NoSQL platform Amazon DynamoDB, we will configure AWS credentials using CLI command AWS Configure with dummy values for AWS Access Key ID and AWS Secret Access Key, a valid region and an output format.
Here is how I did in my example case.
I gave localdynamodb as the profile name where I store dummy values for AWS access key id and secret access key.
You can use any value for local DynamoDB installation, it does not matter.
You can use one of valid AWS region codes like eu-central-1 for Frankfurt but at the end you will be accessing you locally installed DynamoDB platform via endpoint URL parameter in later steps.
List Local DynamoDB Tables using AWS CLI Tool
In order to test that you can successfully access to your locally installed Amazon DynamoDB instance, we can execute below sample AWS CLI code to list all the existing DynamoDB tables in our local NoSQL data platform. Since we have not created any tables yet and by default no previously configured DynamoDB table is shipped with fresh setup, we expect the output to be an empty list.
Please note that we added an endpoint URL parameter value in our AWS CLI dynamodb list-tables command. This end point URL pointing to our locally installed DynamoDB database. By default local DynamoB is accessible via 8000 port.
The second modification different than a normal list-tables dynamodb command is the profile parameter value. I have passed the previously configured named profile as the parameter value which will enable me to required permissions on the local DynamoDB instance.
As seen in following screenshot of the above AWS CLI command "aws dynamodb list-tables" the output is empty meaning no tables are created yet on our local DynamoDB environment.
Create New DynamoDB Table using AWS CLI Command
Working with a local DynamoDB is not different than working on cloud DynamoDB service. The only thing that the application developers should pay attention is the service endpoint URL and the AWS profile that they will use. Just as an example, I will use the sample AWS CLI command given in official documentation to create a sample DynamoDB table named Music and modify the command for a local DynamoDB table by adding related arguments.
Just notice that the endpoint-url and profile parameter values are customized for local DynamoDB installation access.
And below screen is showing the output of a successful DynamoDB table creation.
If you list the tables in your local DynamoDB platform, this time you will see Music table is listed in the output
Additionally developers can use Python code to create DynamoDB table, too. For the referenced tutorial, while creating a local DynamoDB table I realized that I have to install the boto3 Python library and create default profile for AWS CLI to access local DynamoDB instance. All remaining code can be used without any modification to create DynamoDB table using Python locally on a developer computer.
Delete DynamoDB Table using AWS CLI
I have given sample CLI commands to create and list DynamoDB tables, now I can share the AWS CLI command to delete a DynamoDB table with developers. If you want to remove a DynamoDB table, following command can be used. Since this table was created in previous steps locally, I again used related CLI command arguments to target the local DynamoDB database for delete operation.
The output will display the description of the deleted DynamoDB table.
When you try to list the existing DynamoDB tables, the Music table will not be available in the list now.
I hope application developers using Amazon DynamoDB as data storage platform will benefit from this local DynamoDB tutorial.
As I mention before, using a local DynamoDB will increase the productivity of the software developers and switching from development environment to production environment will be fast by just modifying the service endpoint URL and the AWS CLI profile used to access AWS service.