2

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.