0

I’m using the Ruby SDK aws-sdk-dynamodb with Ruby 2.5 for an AWS Lambda function that saves an item to an AWS DynamoDB table.

I can successfully save an item with this code:

    def save!
      hash = {
        table_name: ‘my-table’,
        item: {
          message_sid: '123456',
          created_at: Time.now.to_s
        }
      }
      dynamo = Aws::DynamoDB::Client.new(region: ‘us-east-1’)
      dynamo.put_item(hash)
      puts 'item successfully saved'
      true
    rescue => error
      puts "Unable to save item: #{error}: #{error.message}"
      false
    end

I get an error “no such member :message_sid” when I use this code:

    def save!
      dynamoDB = Aws::DynamoDB::Resource.new(region: ‘us-east-1’)
      table = dynamoDB.table(‘my-table’)
      hash = { message_sid: '123456', created_at: Time.now.to_s }
      table.put_item(hash)
      puts 'item successfully saved'
      true
    rescue => error
      puts "Unable to save item: #{error}: #{error.message}"
      false
    end

I haven’t found any DynamoDB documentation for the error “no such member”. Why does the second example fail?

NoSQLKnowHow
  • 4,449
  • 23
  • 35
Daniel Kehoe
  • 10,952
  • 6
  • 63
  • 82

1 Answers1

1

Admittedly the error message is not very helpful but a close reading of the example documentation reveals that DynamoDB expects the key item: when the method put_item is called on a table object. So this code will work:

    def save!
      dynamoDB = Aws::DynamoDB::Resource.new(region: ‘us-east-1’)
      table = dynamoDB.table(‘my-table’)
      hash = { message_sid: '123456', created_at: Time.now.to_s }
      table.put_item(item: hash)
      puts 'item successfully saved'
      true
    rescue => error
      puts "Unable to save item: #{error}: #{error.message}"
      false
    end

Specifically you should use table.put_item(item: hash) not table.put_item(hash).

Daniel Kehoe
  • 10,952
  • 6
  • 63
  • 82