如何获得我的AWS Lambda来访问存储在供应商/捆绑软件中的gem? [英] How can I get my AWS Lambda to access gems stored in vendor/bundle?
问题描述
我正在用Ruby编写Lambda函数,最终将通过Webhook在Slack中向我发送一些通知.所以我的lambda_function
文件是
I'm writing a Lambda function in Ruby which will eventually send me some notifications in Slack via Webhook. So what I have for my lambda_function
file is
require 'json'
require 'webhook'
def lambda_handler(event:, context:)
# TODO implement
{ statusCode: 200, body: JSON.generate('Hello from Lambda!') }
Webhook.post('https://mywebhookurl', {message: 'test'})
end
内联代码编辑器中的目录结构如下:
And the directory structure in my inline code editor looks like this:
Gemfile
Gemfile.lock
lambda_function.rb
vendor/
bundle/
ruby
2.3.0
gems/webhook
也在2.3.0
浴池下,还有其他几个文件夹,包括build_info
,cache
,doc
等.为了将此代码传递到AWS Lambda上,我正在运行
also under the 2.3.0
bath are several other folders including build_info
, cache
, doc
etc. In order to get this code onto AWS Lambda, I'm running
zip -r myLambda.zip *
将所有内容打包成zip文件并上传到Lambda.
zip -r myLambda.zip *
to get everything into a zip file and uploaded to Lambda.
但是,当我最终去对lambda进行基本测试时,出现以下错误:
However, when I finally go to run a basic test on the lambda, I get the following error:
{
"errorMessage": "cannot load such file -- webhook",
"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/lambda_function.rb:2: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'"
]
}
由于以下教程精确地显示了如何设置我所拥有的东西,但是不起作用.有谁能成功从AWS Lambda的gemfile中提取gem?
There shouldn't be any more to this as the following tutorial shows exactly how to set up what I have, but doesn't work. Does anyone have success pulling gems from their gemfile in AWS Lambda?
推荐答案
在构建带Ruby的AWS Lambda层时遇到了同样的问题.一种快速简便的方法是将所有gem路径添加到AWS Lambda中的Ruby的$ LOAD_PATH中. IE浏览器:
I ran into this same problem when building AWS Lambda Layers w/ Ruby. A quick and easy way to get this to work is to add all of the gem paths to Ruby's $LOAD_PATH in your AWS Lambda. IE:
load_paths = Dir["/opt/ruby/gems/2.5.0/**/lib"]
$LOAD_PATH.unshift(*load_paths)
require 'webhook'
在您的情况下,将"/opt/ruby/gems/2.5.0/**/lib"
替换为"./vendor/bundle/ruby/2.3.0/gems/**/lib"
.
Replace "/opt/ruby/gems/2.5.0/**/lib"
with "./vendor/bundle/ruby/2.3.0/gems/**/lib"
in your case.
当您执行require 'webhook'
时,它将遍历所有路径并遇到"./vendor/bundle/ruby/2.3.0/gems/webhook-1.0.0/lib/webhook.rb"
并将其添加到您的AWS Lambda中. require
不需要文件扩展名.
When you do require 'webhook'
it's going to go and look through all of the paths and come across "./vendor/bundle/ruby/2.3.0/gems/webhook-1.0.0/lib/webhook.rb"
and add it into your AWS Lambda. require
doesn't require a file extension.
当我们通过捆绑器运行rails时,对我们来说是很多魔术",包括确保$ LOAD_PATH指向gems.由于AWS Lambda不使用捆绑程序,因此我们需要自己做一些魔术".
When we run rails through bundler it does a bunch of 'magic' for us including making sure that our $LOAD_PATH is pointing at the gems. Since AWS Lambdas don't use bundler, we need to do some of this 'magic' ourselves.
这篇关于如何获得我的AWS Lambda来访问存储在供应商/捆绑软件中的gem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!