joshuaflanagan / serverless-ruby-package

serverless plugin to package ruby gems
39 stars 11 forks source link

Mysql2 gem native build succeeds in ruby 3.2 Docker image but results in error on execution #58

Open DerWanderer opened 6 months ago

DerWanderer commented 6 months ago

I am testing a very basic service (based on https://github.com/serverless/examples/tree/v3/aws-ruby-simple-http-endpoint) using the serverless-ruby-package. It's been working perfectly as I carefully add more of my requirements to it. Execution started failing as soon as I added the gem 'mysql2' to the Gemfile. Here is the error in Cloudwatch:

{ "errorMessage": "libmariadb.so.3: cannot open shared object file: No such file or directory - /var/task/vendor/bundle/ruby/3.2.0/gems/mysql2-0.5.6/lib/mysql2/mysql2.so", "errorType": "Init", "stackTrace": [ "<internal:/var/lang/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in require'", "<internal:/var/lang/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:inrequire'", "/var/task/vendor/bundle/ruby/3.2.0/gems/mysql2-0.5.6/lib/mysql2.rb:36:in <top (required)>'", "<internal:/var/lang/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:inrequire'", "<internal:/var/lang/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in require'", "/var/task/handler.rb:2: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:inrequire'" ] }

The mysql2.so file was built within the image and is present in the Docker image, in the vendor/ files, and is visible in the AWS Lambda console "Code" tab. Rebuilding the gems with Ruby 3.3 and deploying with the 3.3 Docker image also resulted in the same problem.

Serverless.yml excerpt:

service: er-ops 
frameworkVersion: "3"

provider:
  name: aws
  runtime: ruby3.2
plugins:
  - serverless-ruby-package

functions:
  call_count:
    handler: handler.call_count
    environment:
      RDS_DB_NAME: ${ssm:/database/name}
      RDS_HOSTNAME: ${ssm:/database/host}
      RDS_HOSTNAME_READER:  ${ssm:/database/host_reader}
      RDS_PASSWORD: ${ssm:/database/password}
      RDS_USERNAME: ${ssm:/database/username}
    events:
      - httpApi:
          path: /call-count
          method: get

package:
  include:
    - handler.rb

Environment:

Docker Image (command "docker image ls"): amazon/aws-lambda-ruby 3.2 3f8c5310dbf8 11 days ago 683MB Ruby: RVM using Ruby 3.2.3

joshuaflanagan commented 6 months ago

I believe the mysql2 gem has additional requirements. See the Installation instructions it its readme:

You may need to install a package such as libmariadb-dev, libmysqlclient-dev, mysql-devel, or other appropriate package for your system

Have you confirmed that the target system has the appropriate mysql client?

joshuaflanagan commented 6 months ago

See https://github.com/customink/mysql2-lambda?tab=readme-ov-file, which is archived, but points to https://github.com/rails-lambda/lamby-cookiecutter/pull/21 for building your own ruby lambda runtime with the mysql client installed.

DerWanderer commented 6 months ago

Aha, thank you @joshuaflanagan. That actually rings a bell from years ago. I'm sure that the AWS Lambda image for Ruby 3.2 will not have the mysql client pre-installed. I will look into this and report back.