无法在AWS lambda中使用自定义层(Ruby 3.2.0)

ecfdbz9o  于 2023-10-18  发布在  Ruby
关注(0)|答案(1)|浏览(98)

我在运行lambda(运行时:Ruby 3.2)使用自定义层(pg gem)。
我按照本文档的Ruby文件结构创建了自定义层(.zip):

pg.zip
└ ruby/gems/3.2.0/
               | build_info
               | cache
               | doc
               | extensions
               | gems
               | └ pg-1.5.3
               └ specifications
                 └ pg-1.5.3.gemspec

这个自定义层已经添加到lambda中。
我测试了一下,发现了一个错误:

{
  "errorMessage": "cannot load such file -- pg",
  "errorType": "Init<LoadError>",
  "stackTrace": [
    "/var/lang/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb:59:in `require'",
    "/var/lang/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb:59:in `require'",
    "/var/task/lambda_function.rb:2:in `<top (required)>'",
    "/var/lang/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb:59:in `require'",
    "/var/lang/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb:59:in `require'"
  ]
}

我在安装了[[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection)的Mac(x86_64)中构建了这个包。
Ruby脚本在本地运行时运行良好。

问题:

1.我的pg.zip(自定义图层)的文件结构是否正确?
1.或者,在我当前的环境中捆绑它时,这可能是不匹配的?

vptzau2j

vptzau2j1#

我在ruby 3.2运行时使用lambda层时也遇到了类似的错误!
致您的qns:
1.通常是正确的!在我们的例子中,我们将有一个额外的lib文件夹来存储本地扩展。
1.是的,因为pg依赖于原生扩展,所以你必须在与运行时环境相同的环境中构建gem。
以下是我解决它的步骤:
1.使用lambda ruby 3.2原生扩展构建gem pg

  • Dockerfile*
FROM public.ecr.aws/lambda/ruby:3.2

RUN yum install -y amazon-linux-extras \
    && amazon-linux-extras enable postgresql14 \
    && yum group install "Development Tools" -y

RUN yum install -y postgresql postgresql-devel

ADD Gemfile Gemfile.lock ${LAMBDA_TASK_ROOT}

ENV GEM_HOME=${LAMBDA_TASK_ROOT}

RUN bundle config set without 'development test'
RUN bundle config set path 'vendor/bundle'

RUN bundle install
  • 宝石文件 *
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'pg'

1.然后构建Dockerfile并将必要的扩展复制到层文件夹(即$DIST_DIR文件夹)。示例脚本:

#!/bin/sh -e

rm -rf $DIST_DIR
mkdir -p "$DIST_DIR/lib"
mkdir -p "$DIST_DIR/ruby/gems"

cd "$SOURCE_DIR" >/dev/null || exit

docker build -t ruby-builder -f Dockerfile .
CONTAINER=$(docker run -d ruby-builder)

# See https://northsail.io/articles/aws-lambda-ruby-2-7-pg-gem-libldap-error
# for what to libpq binaries to copy for Amazon Linux 2
docker cp \
  $CONTAINER:/usr/lib64/libpq.so.5.14 \
  $DIST_DIR/lib/libpq.so.5

docker cp \
  $CONTAINER:/usr/lib64/libldap_r-2.4.so.2.10.7 \
  $DIST_DIR/lib/libldap_r-2.4.so.2

docker cp \
  $CONTAINER:/usr/lib64/liblber-2.4.so.2.10.7 \
  $DIST_DIR/lib/liblber-2.4.so.2

docker cp \
  $CONTAINER:/usr/lib64/libsasl2.so.3.0.0 \
  $DIST_DIR/lib/libsasl2.so.3

docker cp \
  $CONTAINER:/usr/lib64/libssl3.so \
  $DIST_DIR/lib/

docker cp \
  $CONTAINER:/usr/lib64/libsmime3.so \
  $DIST_DIR/lib/

docker cp \
  $CONTAINER:/usr/lib64/libnss3.so \
  $DIST_DIR/lib/

docker cp \
  $CONTAINER:/usr/lib64/libnssutil3.so \
  $DIST_DIR/lib/

docker cp \
  $CONTAINER:/var/task/vendor/bundle/ruby/3.2.0 \
  $DIST_DIR/ruby/gems/3.2.0

docker stop $CONTAINER
docker rm $CONTAINER

docker cp命令用于在Amazon Linux 2上运行的lambda环境中运行时复制missing native extension files needed by pg gem
生成的图层内容应如下所示:

├── lib
│   ├── liblber-2.4.so.2
│   ├── libldap_r-2.4.so.2
│   ├── libnss3.so
│   ├── libnssutil3.so
│   ├── libpq.so.5
│   ├── libsasl2.so.3
│   ├── libsmime3.so
│   └── libssl3.so
└── ruby
    └── gems
        └── 3.2.0
            ├── bin
            │   ├── bundle
            │   └── bundler
            ├── build_info
            ├── cache
            │   ├── bundler-2.4.6.gem
            │   └── pg-1.5.3.gem
            ├── doc
            ├── extensions
            │   └── aarch64-linux
            ├── gems
            │   ├── bundler-2.4.6
            │   └── pg-1.5.3
            ├── plugins
            └── specifications
                ├── bundler-2.4.6.gemspec
                └── pg-1.5.3.gemspec

1..zip $DIST_DIR使用您拥有的相同文档创建ruby3.2 lambda层:https://docs.aws.amazon.com/lambda/latest/dg/creating-deleting-layers.html

相关问题