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
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 *
to get everything into a zip file and uploaded to 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'"
]
}
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?
Open the Functions page of the Lambda console. Choose a function. Choose Configuration, then choose Environment variables. Under Environment variables, choose Edit.
You can now develop AWS Lambda functions using Ruby 2.7. This is the latest release of Ruby and supports new features like pattern matching, argument forwarding and numbered arguments. Lambda functions written in Ruby 2.7 run on Amazon Linux 2, the latest generation of Amazon Linux.
You can't change the configuration (which includes environment variables) or function code in a published Lambda function version. To change the environment variables of a published Lambda function version, you must first change the current, unpublished function version ($LATEST). Then, publish a new function version.
I think you should not change the GEM_PATH or having to set $LOAD_PATH
in every lambda functions.
The "best" way is to do this little hack when you create the layer archive:
bundle install --path vendor/bundle
cd vendor/bundle
mkdir ruby/gems
mv ruby/2.5.0 ruby/gems/
zip -r layer.zip ruby/gems/2.5.0/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With