Closed brunoadacosta closed 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.
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
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
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>",
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
need to do new release, my PR works for you?
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?
My last PR solves one problem with config variable, sorry for mistake
I've released v1.3.0 which allows setting a custom dockerImage. Thanks @brunoadacosta !
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?