Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DerWanderer opened this issue Apr 28, 2024 · 3 comments

Comments

@DerWanderer
Copy link

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:in require'",
"/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:in require'",
"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:in require'"
]
}

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
Copy link
Owner

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
Copy link
Owner

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

@DerWanderer
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants