How to use a Gem in aws with Ruby?

Viewed 275

I want to use gem httparty (as an example)

I added a Gemfile

source 'https://rubygems.org'

gem 'httparty'

enter image description here

My lambda code has:

...
require 'httparty'

and I call it with

response = Net::HTTParty.get('example.com', '/index.html')

but the initial require is getting

{
  "errorMessage": "cannot load such file -- httparty",
  "errorType": "Init<LoadError>",
  "stackTrace": [
    "/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'",
    "/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'",
    "/var/task/lambda_function.rb:3:in `<top (required)>'",
    "/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'",
    "/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'"
  ]
}
1 Answers

Need to bundle non-aws gems into local vendor folder and then zip up those dependencies to be included, i.e.

bundle install --path vendor/bundle  # <-- Note using path

zip -r function.zip lambda_function.rb vendor/ # <-- vendor/ for the dependencies

aws lambda update-function-code --function-name myFunction \
--zip-file fileb://function.zip --region 'us-east-2' # <= your region
Related