DynamoDB Local
AWS Ruby SDK
Ruby programming
AWS development
Local database setup

How do you use DynamoDB Local with the AWS Ruby SDK?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When developing applications that leverage AWS DynamoDB, using the DynamoDB Local version is a critical feature for efficient development and testing. DynamoDB Local is a downloadable version of DynamoDB that you can run on your local machine and is useful for testing and development without incurring costs or requiring an active internet connection. In this article, we will explore how to use DynamoDB Local with the AWS Ruby SDK, complete with examples and tips.

Setting Up DynamoDB Local

Prerequisites

Before you begin, ensure you have met the following prerequisites:

  • Java Runtime Environment (JRE): DynamoDB Local runs on Java. Make sure you have JRE 8 or higher installed.
  • AWS Command Line Interface (CLI): Optionally, you can use the AWS CLI to interact with DynamoDB Local.

Downloading and Running DynamoDB Local

  1. Download DynamoDB Local: You can download DynamoDB Local from the following URL:
 
   https://s3.us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz
  1. Extract the downloaded file:
bash
    tar -xvf dynamodb_local_latest.tar.gz
  1. Start DynamoDB Local: Execute the following command to start DynamoDB Local:
bash
    java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory -sharedDb

This command launches DynamoDB in memory mode with a shared DB, meaning data is not persisted after stopping the server.

Installing the AWS Ruby SDK

To interact with DynamoDB Local using Ruby, you need the AWS SDK for Ruby. Install it using gem:

bash
gem install aws-sdk-dynamodb

Configuring the AWS Ruby SDK

Below is a basic configuration to set up the AWS Ruby SDK to connect to DynamoDB Local:

ruby
1require 'aws-sdk-dynamodb'
2
3# Configuring the DynamoDB client
4dynamodb_client = Aws::DynamoDB::Client.new(
5  region: 'us-west-2',
6  endpoint: 'http://localhost:8000',
7  access_key_id: 'fakeMyKeyId',
8  secret_access_key: 'fakeSecretAccessKey'
9)

This configuration targets the DynamoDB Local instance hosted at http://localhost:8000.

Creating a Table with AWS Ruby SDK

Here's how to create a table in DynamoDB Local using Ruby:

ruby
1begin
2  dynamodb_client.create_table({
3    table_name: 'Movies',
4    key_schema: [
5      { attribute_name: 'year', key_type: 'HASH' },  # Partition key
6      { attribute_name: 'title', key_type: 'RANGE' } # Sort key
7    ],
8    attribute_definitions: [
9      { attribute_name: 'year', attribute_type: 'N' },
10      { attribute_name: 'title', attribute_type: 'S' }
11    ],
12    provisioned_throughput: {
13      read_capacity_units: 10,
14      write_capacity_units: 10
15    }
16  })
17  puts "Table created successfully."
18rescue Aws::DynamoDB::Errors::ServiceError => error
19  puts "Unable to create table: #{error.message}"
20end

Inserting Items into the Table

Here's an example of inserting an item into the "Movies" table:

ruby
1begin
2  dynamodb_client.put_item({
3    table_name: 'Movies',
4    item: {
5      'year' => 2015,
6      'title' => 'The Big New Movie',
7      'info' => {
8        'plot' => 'Nothing happens at all.',
9        'rating' => 0
10      }
11    }
12  })
13  puts "Item inserted successfully."
14rescue Aws::DynamoDB::Errors::ServiceError => error
15  puts "Unable to insert item: #{error.message}"
16end

Querying Data

Fetching data is straightforward:

ruby
1begin
2  result = dynamodb_client.query({
3    table_name: 'Movies',
4    key_condition_expression: "#yr = :yyyy",
5    expression_attribute_names: {
6      "#yr" => "year"
7    },
8    expression_attribute_values: {
9      ":yyyy" => 2015
10    }
11  })
12  
13  result.items.each do |item|
14    puts "#{item['year'].to_i}: #{item['title']} - #{item['info']['plot']}"
15  end
16rescue Aws::DynamoDB::Errors::ServiceError => error
17  puts "Unable to query data: #{error.message}"
18end

Key Points Summary

FeatureDescription
PrerequisitesJava 8+, AWS CLI (optional)
Download URLhttps://s3.us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz
Launch Commandjava -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory -sharedDb
SDK Installationgem install aws-sdk-dynamodb
ConfigurationRequires endpoint, access_key_id, secret_access_key with fake values for local usage
Table Operationscreate_table, put_item, query

Conclusion

Using DynamoDB Local with the AWS Ruby SDK is a powerful way to test your database interactions without incurring real-world costs. By setting up a local environment, you can efficiently debug and iterate over your application logic with high fidelity before deploying to production. With robust table management and querying capabilities provided by AWS SDK for Ruby, you can craft a local development experience that mirrors that of AWS.


Course illustration
Course illustration

All Rights Reserved.