joshuaflanagan / serverless-ruby-package

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

Package problems with mysql gem #15

Closed brunoadacosta closed 4 years ago

brunoadacosta commented 4 years ago

I'm having trouble using the mysql2 gem in a project, the image does not contain the mysql headers and the compilation gives an error.

Is there any way I can use a custom docker image? or do we have another way of doing it?

joshuaflanagan commented 4 years ago

I figured I would eventually need to make the docker image configurable, but was waiting for a need. It would just be a change to this line: https://github.com/joshuaflanagan/serverless-ruby-package/blob/db3f14ecfb572259218be0745ec6677410ea32c7/index.js#L108

However, that won't really solve your problem. The mysql2 gem requires the mysql client to be installed on the server where it runs. So even if we used a custom docker image for the gem packaging, once you actually deploy it to Lambda, I don't think the Lambda server will have the mysql client installed that the gem needs. You may need to build your own Lambda Layer. A quick search for Lambda + MySQL advice all seems to suggest other people use languages that have mysql packages that do not require the client to be installed.

brunoadacosta commented 4 years ago

Lambda has a layer that adds the mysql lib, but there is a way around it by adding libmysqlclient.so.18 inside lib / Article http://blog.blackninjadojo.com/aws/serverless/2019/02/18/build-package-and-deploy-an-aws-lambda-using-the-ruby-runtime.html

If I generate the package by hand compiling the gems with the image with the lib mysql-devel and climbing the lambda works

brunoadacosta commented 4 years ago

I trying to use that custom hooks plugin

  - serverless-hooks-plugin
custom:
  hooks:
    package:initialize:
      - docker run -v `pwd`:`pwd` -w `pwd` lambci/lambda:build-ruby2.5 yum -y install mysql-devel ; bundle install --deployment --path vendor/bundle
    package:finalize:
      - rm -rf vendor
      - rm -rf .bundle

but it doesn't work well

brunoadacosta commented 4 years ago

I've tryed

plugins:
  - serverless-ruby-package
  - serverless-hooks-plugin
custom:
  rubyPackage:
    alwaysCrossCompileExtensions: false
  hooks:
    package:initialize:
      - docker run -v `pwd`:`pwd` -w `pwd` lambci/lambda:build-ruby2.5 yum -y install mysql-devel ; bundle install --standalone --path vendor/bundle
    package:finalize:
      - rm -rf vendor
      - rm -rf .bundle

and returns error on lambda

{
  "errorMessage": "Error loading the 'mysql2' Active Record adapter. Missing a gem it depends on? Could not find 'mysql2' (>= 0.4.4, < 0.6.0) among 254 total gem(s)\nChecked in 'GEM_PATH=/var/task/vendor/bundle/ruby/2.5.0:/opt/ruby/gems/2.5.0:/var/runtime', execute `gem env` for more information",
  "errorType": "Function<LoadError>",
brunoadacosta commented 4 years ago

If i execute $ docker run -v "$PWD":/var/task -it lambci/lambda:build-ruby2.5 /bin/bash -c "yum -y install mysql-devel ; bundle install --standalone ; bash"

and inside container $ zip -r package.zip lib/ handler.rb vendor/

it works on lambda

brunoadacosta commented 4 years ago

need to do new release, my PR works for you?

joshuaflanagan commented 4 years ago

I still need to do some testing before doing a release, but I merged in your PR #17. I just noticed you created a 3rd PR #18 - can you explain why there are 3 PRs?

brunoadacosta commented 4 years ago

My last PR solves one problem with config variable, sorry for mistake

joshuaflanagan commented 4 years ago

I've released v1.3.0 which allows setting a custom dockerImage. Thanks @brunoadacosta !