Imagine you're traveling by plane to a different city, and the deadline for your DynamoDB-powered project is really tight. Obviously, during flight, there's no internet, but you just had one of these "Aha!" moments. You want to rush into the implementation, but there's one problem:
Since DynamoDB is fully managed NoSQL database, you cannot access it when you're offline. Bummer.
This didn't happened to me just once. Until recently, when I discovered I can run DynamoDB offline and locally on my computer, without AWS Cloud. In some scenarios, it makes developing and testing applications without accessing DynamoDB easier. In this guide, I'll show you how you can do that too. There are a few ways to do this; let's run through them below:
Different ways to setup DynamoDB locally
This is my favorite way because it does not require Java installed. In this setup, DynamoDB is running in an isolated container on the port of your choice. Another good thing about this solution is that it works the same across all the operating systems and can be easily added to your Docker Compose setup.
Make sure you have Docker installed. If you don't have Docker yet, you can get it here.
Open terminal and type:
And that's pretty much it.
Your DynamoDB local instance is now running on port 8000. If you want to connect to this container using SDK or CLI, don't forget to change the
endpoint parameter in the configuration. Otherwise, you'll keep trying to connect to the AWS network.
In this setup, we're running Java binary in our system without any containerization. It's a bit more complicated
First, make sure you have Java Runtime Environment (JRE) version 6.x or newer already installed. It's required to run DynamoDB locally.
Second, download DynamoDB
Then, extract the archive and copy its contents to a location of your choice.
Lastly, open terminal in the location where you've extracted files in step 3 and enter the following command:
DynamoDB is now running on port
8000. If you want to change it, use
Similar to the Docker setup, you need to change the
endpoint parameter in the configuration.
Running out of memory
If you're going to use DynamoDB really heavily, it's possible that the allocated amount of memory for your JVM might not be enough. In a case like that, use a combination of
Xmx flags to adjust the amount of RAM. For example:
This will start your local DynamoDB instance with 256 MB of memory and allow the process to use up to 2048 MB of memory.
With Serverless Framework
Using DynamoDB with Serverless Framework is a pretty popular combination. DDB in a case like this one is perfect for running offline integration tests or just running local replica of the AWS environment for development purposes. Here's how:
- Run following command:
serverless.ymlfile and add the following entry to the plugins section:
- Now, run the following command to let the plugin setup everything for us, including downloading DynamoDB Local:
If you don't have DynamoDB tables defined in your
serverless.ymlfile, do it now.
Finally, start DynamoDB Local and migrate by running this command:
For more information, checkout the plugin docs.
LocalStack is a project aiming to mock most of the AWS resources locally. Once again, in my opinion, using Localstack is much easier with Docker. Running DynamoDB local with it look like this:
This will run a container exposing three ports:
8080running Localstack's status dashboard
Similar to the previous approaches, if we want to use our offline services, we need to set the
endpoint parameter of SDK or CLI to
http://localhost:4569. If you don't want to set the
endpoint setting explicitly when using CLI with each command, alternatively, you can use awscli-local.
Querying and visualizing offline data
Even though DynamoDB offline comes with an interactive shell that allows you to query and modify data inside the database using a web-based console, I found it a bit clunky. Fortunately, Dynobase works with DynamoDB local just like with the AWS managed one.
© 2020 Dynobase