navarasu / serverless-ruby-layer

A Serverless Plugin to deploy gems from Gemfile to AWS Layer
MIT License
72 stars 11 forks source link

Ruby 3.2 Gem Path Error for AWS SDK #98

Open jaredSiebert opened 4 months ago

jaredSiebert commented 4 months ago

AWS Lambda with Ruby 3.2 error - by setting the GEM_PATH environment variable, the native aws-sdk gems cannot load

{
  "errorMessage": "cannot load such file -- aws-sdk-dynamodb",
  "errorType": "Init<LoadError>",
  "stackTrace": [
    "<internal:/var/lang/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:136:in `require'",
    "<internal:/var/lang/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:136:in `require'",
    "/var/task/handler.rb:1:in `<top (required)>'",
    "<internal:/var/lang/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:136:in `require'",
    "<internal:/var/lang/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:136:in `require'"
  ]
}

Additional context As outlined in AWS docs for including gems from layers (https://docs.aws.amazon.com/lambda/latest/dg/ruby-package.html#ruby-package-dependencies-layers), if the generated zip file structure changes from ruby/3.2.0/* to ruby/gems/3.2.0/* then there is no need to set the GEM_PATH environment variable which will eliminate the issue of the native aws-sdk gems not being found.

Confirmed this works by compiling the deploy_http_nokogiri sample (https://navarasu.github.io/serverless-ruby-layer/#/use_docker), unzipping gemLayer.zip and adjusting the folder structure, zipping and manually uploading a custom layer to AWS.

jaredSiebert commented 4 months ago

Quick and dirty fix that I did

Starting at line 130

docker(['exec','-w',this.docker_gem_path, docker_id].concat(bundle_args),options,this.cli)
this.aws_gem_path = this.docker_gem_path+'/build/ruby/gems'
docker(['exec', docker_id, 'mkdir', this.aws_gem_path], options, this.cli)
docker(['exec', docker_id, 'mv', this.docker_gem_path+'/build/ruby/3.2.0', this.aws_gem_path], options, this.cli)
docker(['cp', '-L', container_id+':'+this.docker_gem_path+'/build/ruby', this.build_path], options, this.cli)
jaredSiebert commented 3 months ago

Just following up on this - would be nice to not have to monkey patch each of my projects