AWS Lambda:Ruby函数无法加载gem

编程入门 行业动态 更新时间:2024-10-27 14:20:09
本文介绍了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/bundle和bundle 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

本文发布于:2023-11-28 09:22:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641830.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   加载   Lambda   AWS   gem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!