Ben Downey
Ben Downey

Reputation: 2665

AWS Lambda Unable to Find Ruby Dependency

I'm working through the tutorial in announcing-ruby-support-for-aws-lambda and I'm having trouble getting Lambda to locate Ruby dependencies.

I've got the tutorial's code just copy/pasted in there. Nothing fancy so far.

require 'aws-record'

class DemoTable
  include Aws::Record
  set_table_name ENV[‘DDB_TABLE’]
  string_attr :id, hash_key: true
  string_attr :body
end

def put_item(event:,context:)
  body = event["body"]
  item = DemoTable.new(id: SecureRandom.uuid, body: body)
  item.save! # raise an exception if save fails
  item.to_h
end

I've got a Gemfile that contains aws-record and I've run both bundle install and bundle install --deployment.

If I'm in the Lambda console, looking at the Function Code section, I can see the project has the vendor directory and the aws-record gem is present.

I've used the sam CLI to package and deploy the code and it seems like everything worked.

But when I create and run the test, I receive the following error.

{
  "errorMessage": "cannot load such file -- aws-record",
  "errorType": "Init<LoadError>",
  "stackTrace": [
    "/var/lang/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'",
    "/var/lang/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'",
    "/var/task/hello_ruby_record.rb:1:in `<top (required)>'",
    "/var/lang/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'",
    "/var/lang/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'"
  ]
}

Everything seems to simple at this stage of the app, so I'm mystified about what I'm missing. Does anyone have any suggestions about how to troubleshoot this?

Upvotes: 2

Views: 2113

Answers (2)

Dennis
Dennis

Reputation: 799

You do not need to match your local Ruby version to the AWS Ruby version. Instead you can utilise Docker to vendor the 2.5.0 gems in this way:

cd /path/to/Gemfile/ && \
docker run -v `pwd`:`pwd` -w `pwd` -i -t lambci/lambda:build-ruby2.5 bundle install --deployment

Upvotes: 0

Ben Downey
Ben Downey

Reputation: 2665

The issue was a mismatch between Ruby versions. I was a few versions behind AWS Lambda's version (2.5.0). Once I changed updated my local version to the one Labmda uses, the loading error went away.

This issue is a duplicate of AWS Lambda: Ruby function failing to load gem and How can I get my AWS Lambda to access gems stored in vendor/bundle? but at this time, neither has accepted answers, so I'm leaving this posted here for future devs in a similar situation.

Upvotes: 4

Related Questions