6
votes

Amazon's documentation provides examples in Java, .NET, and PHP of how to use DynamoDB Local. How do you do the same thing with the AWS Ruby SDK?

My guess is that you pass in some parameters during initialization, but I can't figure out what they are.

dynamo_db = AWS::DynamoDB.new(
  :access_key_id => '...',
  :secret_access_key => '...')
3

3 Answers

17
votes

Are you using v1 or v2 of the SDK? You'll need to find that out; from the short snippet above, it looks like v2. I've included both answers, just in case.

v1 answer:

AWS.config(use_ssl: false, dynamo_db: { api_verison: '2012-08-10', endpoint: 'localhost', port: '8080' })
dynamo_db = AWS::DynamoDB::Client.new

v2 answer:

require 'aws-sdk-core'
dynamo_db = Aws::DynamoDB::Client.new(endpoint: 'http://localhost:8080')

Change the port number as needed of course.

6
votes

Now aws-sdk version 2.7 throws an error as Aws::Errors::MissingCredentialsError: unable to sign request without credentials set when keys are absent. So below code works for me

dynamo_db = Aws::DynamoDB::Client.new(
  region: "your-region",
  access_key_id: "anykey-or-xxx",
  secret_access_key: "anykey-or-xxx",
  endpoint: "http://localhost:8080"
)
2
votes

I've written a simple gist that shows how to start, create, update and query a local dynamodb instance.

https://gist.github.com/SundeepK/4ffff773f92e3a430481

Heres a run down of some simple code:

Below is a simple command to run dynamoDb in memory

#Assuming you have downloading dynamoDBLocal and extracted into a dir called dynamodbLocal
java -Djava.library.path=./dynamodbLocal/DynamoDBLocal_lib -jar ./dynamodbLocal/DynamoDBLocal.jar -inMemory -port 9010

Below is a simple ruby script

require 'aws-sdk-core'

dynamo_db = Aws::DynamoDB::Client.new(region: "eu-west-1", endpoint: 'http://localhost:9010')
    dynamo_db.create_table({
    table_name: 'TestDB',
    attribute_definitions: [{
        attribute_name: 'SomeKey',
        attribute_type: 'S'
    },
    {
        attribute_name: 'epochMillis',
        attribute_type: 'N'
    }
    ],
    key_schema: [{
        attribute_name: 'SomeKey',
        key_type: 'HASH'
    },
    {
        attribute_name: 'epochMillis',
        key_type: 'RANGE'
    }
    ],
    provisioned_throughput: {
        read_capacity_units: 5,
        write_capacity_units: 5
    }
    })
dynamo_db.put_item( table_name: "TestDB",
    item: { 
    "SomeKey" => "somevalue1", 
    "epochMillis" => 1
})
puts dynamo_db.get_item({
    table_name: "TestDB",
    key: { 
    "SomeKey" => "somevalue",
    "epochMillis" => 1
}}).item

The above will create a table with a range key and also add/query for the same data that was added. Not you must already have version 2 of the aws gem installed.