问题描述
试图让Lambda连接到RDS数据库,但无法加载mysql2 gem.尝试了原始指令,但这不能解决问题.
Trying to have a Lambda connect to an RDS database but can't get the mysql2 gem to load. Tried the pristine instruction but that didn't resolve the issue.
我在供应商目录中有内置的mysql2 gem.是否使用捆绑安装--deployment .
I've got the built mysql2 gem in the vendor directory. Did this using bundle install --deployment.
大概是个问题,因为mysql2使用了已编译的扩展名.虽然不确定如何对AWS Lambda进行排序.有想法吗?
Presumably this is a problem because of the compiled extensions used by mysql2. Not sure how I can sort this for AWS Lambda though. Thoughts?
以下是日志输出:
START RequestId: 62f35c49-039f-11e9-be04-1fd1111df42b Version: $LATEST
Ignoring mysql2-0.5.2 because its extensions are not built. Try: gem pristine mysql2 --version 0.5.2
Init error when loading handler lambda_function.lambda_handler
{
"errorMessage": "cannot load such file -- mysql2",
"errorType": "Init<LoadError>",
"stackTrace": [
"/var/lang/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'",
"/var/lang/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'",
"/var/task/lambda_function.rb:3:in `<top (required)>'",
"/var/lang/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'",
"/var/lang/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'"
]
}
END RequestId: 62f35c49-039f-11e9-be04-1fd1111df42b
REPORT RequestId: 62f35c49-039f-11e9-be04-1fd1111df42b Duration: 1439.17 ms Billed Duration: 1500 ms Memory Size: 128 MB Max Memory Used: 17 MB
Unknown application error occurred
Init<LoadError>
这是我的Gemfile:
Here's my Gemfile:
source 'https://rubygems.org'
gem 'mysql2', '~> 0.5.2'
gem 'sequel', '~> 5.15.0'
Gemfile.lock
Gemfile.lock
GEM
remote: https://rubygems.org/
specs:
mysql2 (0.5.2)
sequel (5.15.0)
PLATFORMS
ruby
DEPENDENCIES
mysql2 (~> 0.5.2)
sequel (~> 5.15.0)
BUNDLED WITH
1.17.2
这是我的lambda_function.rb文件的顶部
Here's the top of my lambda_function.rb file
require 'json'
require 'logger'
require 'mysql2'
require 'sequel'
推荐答案
先决条件:* Docker
Prerequesties:* Docker
创建这样的docker文件
Create a docker file like this
FROM lambci/lambda:build-ruby2.5
RUN yum -y install mysql-devel
RUN gem update bundler
CMD "/bin/bash"
构建Docker
docker build -t lambda-ruby2.5-mysqldep .
Docker使用以下命令在您的源代码文件夹中运行
Docker run in your source code folder with the below command
docker run --rm -it -v $PWD:/var/task -w /var/task lambda-ruby2.5-mysqldep
这会让您陷入重击在bash内
It will get you into the bashInside the bash
- 将mysql文件夹从/usr/lib64/mysql移至/usr/mysql
- mkdir -p/var/task/lib
- cp -a/usr/mysql/.so./var/task/lib/
- 捆绑配置--local build.mysql --with-mysql-config =/usr/local/mysql/bin/mysql_config
- 捆绑安装(带部署和不带部署)
- move the mysql folder from /usr/lib64/mysql to /usr/mysql
- mkdir -p /var/task/lib
- cp -a /usr/mysql/.so. /var/task/lib/
- bundle config --local build.mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config
- bundle install with and without deployment
然后退出重击.然后创建一个lambda部署程序包,并将其上传到AWS.它对我有用
Then exit the bash. And create a lambda deployment package and upload it to AWS. It worked for me
这篇关于无法在AWS Lambda上加载文件mysql2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!