Skip to content

Instantly share code, notes, and snippets.

@kenoir
Last active June 19, 2018 21:27
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save kenoir/8998532 to your computer and use it in GitHub Desktop.
Save kenoir/8998532 to your computer and use it in GitHub Desktop.
Prettied up some AWS Ruby SDK DynamoDB examples from @Integralist.

AWS query-instance_method docs

export AWS_ACCESS_KEY_ID=‘XXXX’
export AWS_SECRET_ACCESS_KEY=‘XXXX’
# ENV['AWS_ACCESS_KEY_ID']
# ENV['AWS_SECRET_ACCESS_KEY']

require 'aws-sdk'
dynamo_db = AWS::DynamoDB.new

# Create table
foobar = dynamo_db.tables.create(
  'foobar', 10, 5, 
  :hash_key => { 
    :component_id => :string 
  }, 
  :range_key => { 
    :opts_hash => :string }
)

foobar.status # => :creating
foobar.status # => :active (takes ~15 seconds)
foobar.hash_key # => <AWS::DynamoDB::PrimaryKeyElement:0x60cd375d @name="component_id", @type=:string>
foobar.range_key # => <AWS::DynamoDB::PrimaryKeyElement:0x48774b2c @name="opts_hash", @type=:string>

# Get table
dynamo_db.tables['foobar']

# Create Item
item1 = foobar.items.create(
  :component_id => '1', 
  :opts_hash => 'abc',
  :s3_location => 'foo/bar/baz'
) 
# => <AWS::DynamoDB::Item table_name:foobar hash_value:1 range_value:abc>

# Retrieve Items
foobar.items.each { |item| puts item.attributes.to_h }
# => {"s3_location"=>"foo/bar/baz", "component_id"=>"1", "opts_hash"=>"def"}

# Delete Tables
dynamo_db.tables.each {|x| if x.name == foobar then x.delete end }

# Query Tables
client = AWS::DynamoDB::Client::V20120810.new
client.query({ 
  :table_name => 'foobar', 
  :consistent_read => true,
  :select => 'SPECIFIC_ATTRIBUTES',
  :attributes_to_get => ['s3_location'],
  :key_conditions => { 
    'component_id' => { 
      :comparison_operator => 'EQ', 
      :attribute_value_list => [
        { 's' => '1' }
      ],
    },
    'opts_hash' => { 
      :comparison_operator => 'EQ', 
      :attribute_value_list => [
        { 's' => 'def' }
      ]
    } 
  } 
})

# => {:member=>[{"s3_location"=>{:s=>"foo/bar/baz"}}], :count=>1}
@natesire
Copy link

I am using AWS SDK 2. tables is undefined method on the client. Hmm.

@trevorrowe
Copy link

@nathantechie9 The #tables method exists on Aws::DynamoDB::Resource in v2. Also ensure you are running the latest version of the SDK and that you are depending on aws-sdk and not just aws-sdk-core. The core gem does not include the resource interfaces.

@Doug-AWS
Copy link

Doug-AWS commented Apr 5, 2016

Version 2 of the AWS SDK for Ruby Developer Guide has a number of DynamoDB code examples in http://docs.aws.amazon.com/sdk-for-ruby/latest/DeveloperGuide/aws-ruby-sdk-recipes.html#aws-ruby-sdk-dynamo-recipes:

• Getting information about all tables
• Creating a table
• Adding an item to a table
• Getting information about table items
• Getting a table item by key

@run-dmcc
Copy link

run-dmcc commented Nov 3, 2017

How would I delete an item out of a DynamoDB table?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment