Ruby on Rails with local and cloud AWS DynamoDB

AWS allows to get downloadable version of DynamoDB and run it locally. This article presents how to install DynamoDB locally and connect Ruby on Rails app to it.

  1. Introduction
  2. Install Java
  3. Get downloadable DynamoDB
  4. Setup DynamoDB via shell
  5. Setup Rails project
  6. Connect Rails app to local DynamoDB
  7. Setup access to cloud DynamoDB

 

1. Introduction

Downloadable version of DynamoDB lets us to develop app without connecting to AWS Services, what can be very convenient solution for development process. You have to notice that downloadable version may be different than DynamoDB running in the cloud. Here you can find more information about it.

I’m using following configuration:

Ruby 2.3.1
Rails 5.0.1
Ubuntu 16.04 LTS (Xenial)

[03.02.2017] NOTE: There is available gem called Dynamoid which can be useful. However, it hasn’t been officially released for Rails 5 due to following problem. You can try to use code from master branch – please see more.

 

2. Install Java

Downloadable DynamoDB needs Java to run. We will install JRE, but you can also use JDK.

If you get error: “E: dpkg was interrupted, you must manually run ‘sudo dpkg –configure -a’ to correct the problem.” Run following command and repeat JRE installation from previous step.

 

3. Get downloadable DynamoDB

More details about downloadble version of DynamoDB you can find here.

Download DynamoDB:

NOTE: The url may be changed. Visit this page to be sure that URL to downloadable DynamoDB is correct.

Extract downloaded file:

Run DynamoDB:

Now your local DynamoDB should be running and listening on port 8000. You can stop it by CTRL+C command.

To continue and create Rails App, open new terminal.

 

4. Setup DynamoDB via shell

When DynamoDB is running locally, you should have access to its shell under: http://localhost:8000/shell

We use this shell to create new table. Alternatively, you can also create this table from code.

Open in the browser: http://localhost:8000/shell and click </> in right top corner. From the list choose: CreateTable. You should see following code:

Przechwytywanie

Source code should be copied to editor. You can edit this code on your own. Below I put another example:

To add new item you can also use template like in table creation. Below I put another very simple example:

You can run above code few times with different values. Remember that primary key (user_id in my case) is required and if you run many times the Put query with the same value of primary key, each next query will overwrite previous one.

To look up your table, you can use: Query, Scan or GetItem (if you want to view specific item).

An example for scan which returns all items in table:

 

5. Setup Rails project

Because Active Record is not compatible with DynamoDB, you can skip it during Rails project creation:

Add to Gemfile:

 

6. Connect Rails app to local DynamoDB

Firstly, configure access to DynamoDB.

1. Store AWS settings

Until you work with DynamoDB locally, you don’t have to use AWS settings. If you want to work with not-local version of DynamoDB, you should set AWS settings – now or you can skip this and set it later.

Store your AWS settings in secrets.yml as environment variables:

And set environment variableson your machine. If you are working on Ubuntu you can add it to files like ~/.bash_profile or ~/.profile or ~/.bashrc:

After that, open new session (new terminal) or run:

It will reload the file and export variables.

 

2. Setup connection to local DynamoDB:

Create: config/initializers/aws.rb:

Create file: lib/aws.rb:

Add to config/environment.rb:

 

3. Use my_table1 in Rails app

Let’s generate some controller (because we are not using Active Record, the DynamoDB and Rails model names don’t have to be the same – but it’s better practice):

Add routes to config/routes.rb:

Add 2 following actions to app/controller/my_table1_controller.rb:

 

Create app/views/my_table1/new.html.erb

It’s a simple form with two fields only. You can open it in web browser under: /my_table1/new .

 

Create app/views/my_table1/index.html.erb:

It’s not-styled list of all items saved in DynamoDB’s my_table1. You can open it in web browser under: /my_table1 .

 

 

7. Setup access to cloud DynamoDB

Firstly, you have to create new DynamoDB table. You can to that using AWS console (in web browser) – it should by straightforward. You have to set only Table name (my_table1) and Primary Key (user_id). If you use different than locally, you have to adjust code from this example.

You’ll use AWS settings from secrets.yml file (as it was described in 6.1). If you haven’t done this yet, please do it now.

And we are almost ready to test it. Notice that we make if-statement in Aws.rb where we check Rails app environment:

If Rails environment is development, app connects to local DynamoDB. There two things what you can to do:

  1. Run Rails server in test or production environment:
  2. Negate if statement in lib/aws.rb:

 

Now, you should to be able to add and list items from my_table1 in AWS cloud like for local DynamoDB.

 

 

 

Tomasz Antas

Ruby on Rails developer and Web designer.
The area of his interest includes Popular Science, Internet of Things, Wearables, AI and Virtual/Augmented Reality.

Latest posts by Tomasz Antas (see all)

Thanks for reading!