AWS Lambda:Ruby函数无法加载gem [英] AWS Lambda: Ruby function failing to load gem

查看:96
本文介绍了AWS Lambda:Ruby函数无法加载gem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ruby Lambda函数,该函数取决于外部(即非AWS)的RubyGem.我有一个Gemfile,一个Gemfile.lock和一个vendor/bundle目录.从本地角度看,一切看起来都很好.

I have a Ruby Lambda function which depends on an external (ie non-AWS) RubyGem. I have a Gemfile, a Gemfile.lock and a vendor/bundle directory. Everything looks fine from a local perspective.

我尝试使用bundle install --path vendor/bundlebundle install --deployment安装gem,并且在压缩文件时特别包含了vendor目录:zip -r function.zip myfunction.rb vendor

I've tried using bundle install --path vendor/bundle and bundle install --deployment to install the gems, and am specifically including the vendor directory when zipping up the files: zip -r function.zip myfunction.rb vendor

尽管如此,当我在Lambda控制台中测试该功能时,它会失败,并显示错误消息,例如无法找到库.

Despite this, when I test the function in the Lambda console, it's failing with errors complaining about not being able to find the libraries, e.g.

{
  "errorMessage": "cannot load such file -- mysql2",
  "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:3: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'"
  ]
}

大概我在上载函数之前将宝石捆绑在一起确实做错了什么;但我不知道是什么.有人有什么想法吗?

Presumably I'm doing something really obviously wrong in bundling up the gems before uploading the function; but I can't figure out what. Does anyone have any ideas?

推荐答案

我遇到了同样的问题.根本原因是在本地和Lambda函数中使用了不同的ruby版本.当前,AWS Lambda使用的是Ruby 2.5,而您的gems位于'/vendor/bundle/ruby​​/'.因此,您的lambda函数无法找到您上传的gem库.要使用您的Ruby版本覆盖gem路径,请在包含处理程序功能的文件顶部添加以下提到的代码:

I faced the same issue. The underlying reason is different ruby versions being used in local and Lambda function. Currently AWS Lambda is using Ruby 2.5 and your gems are located at '/vendor/bundle/ruby/'. Thus your lambda function is unable to locate the gem libraries uploaded by you. To override the gem path with your Ruby version, add the below mentioned code at the top of your file containing the handler function:

  my_gem_path = Dir["./vendor/bundle/ruby/<your-ruby-version>/gems/**/lib"]
  $LOAD_PATH.unshift(*my_gem_path)

或者,只需尝试升级本地Ruby版本并将其与Lambda函数viz 2.5当前使用的确切版本匹配.

Or, just try upgrading and matching your local Ruby version with the exact version used by Lambda function viz 2.5 currently.

这篇关于AWS Lambda:Ruby函数无法加载gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆