AWS Lambda 函数内部错误 运行 Sharp:darwin-x64' 二进制文件不能在 'linux-x64' 平台上使用

Error running Sharp inside AWS Lambda function: darwin-x64' binaries cannot be used on the 'linux-x64' platform

尝试在 AWS Lambda 函数内 运行 锐化时,我不断收到以下错误:

darwin-x64' binaries cannot be used on the 'linux-x64' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'

我在 MacBook Pro 上使用无服务器框架部署无服务器应用程序。我该如何解决这个问题?

感谢 stdunbar 指引我正确的方向。

当通过 NPM 以正常方式(即:npm i sharp --save)在 MacOS 上安装 sharp 时,安装程​​序会自动为 OS X 添加二进制文件。但是 AWS lambda 函数 运行 在 Linux 2 台配备 x64 处理器的机器上,这就是我们收到此错误的原因。

要修复你必须先完全卸载 sharp 然后 运行:

npm install --arch=x64 --platform=linux sharp

注:

版本 0.25 不再使用目标标志。这曾经有效:

npm install --arch=x64 --platform=linux --target=10.15.0 sharp

然后使用 sls deploy

从 Serverless Framework 照常部署

旁注:

Sharp 非常快!!!在使用 sharp 之前,我使用了另一个名为 Jimp 的图像大小调整实用程序。它完成了工作,但速度很慢。为了防止超时错误,我不得不将内存大小从 128 增加到 512,并将超时从 5 秒增加到 30 秒,以处理典型的 1 兆字节图像。

以下是使用相同配置将 1.2Mb 图片缩小到 600x400 时两者之间的比较:

Jimp -> used 512Mb of memory and AWS billed me for 14300 ms.

Sharp -> used 132 MB of memory and AWS billed me for 800 ms.

That's more than 14x faster than Jimp!!!

当我在 windows 机器的 CMD 中执行 npm install --arch=x64 --platform=linux sharp 时,我遇到了同样的问题。

通过在管理员模式下打开 CMD 修复了它,我在 运行 npm install --arch=x64 --platform=linux sharp

中没有发现任何错误

我在从 MacOS 或 Windows 构建时发现了一个更简单的解决方案,我使用带有 SAM 的 Makefile 并提供 linux 作为 NPM 安装的目标。

template.yaml

  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: mycode/
      Handler: handler.handler
      Runtime: nodejs12.x
      MemorySize: 512
      Timeout: 5
      Role: !GetAtt EdgeLambdaRole.Arn
    Metadata:
        BuildMethod: makefile

mycode/makefile

build-MyFunction:
    cp *.js $(ARTIFACTS_DIR)
    cp package.json $(ARTIFACTS_DIR)
    cp package-lock.json $(ARTIFACTS_DIR)
    cd $(ARTIFACTS_DIR) && npm install --production --arch=x64 --platform=linux

确保在 makefile 中使用 TABS

如果您在 serverless.yml 文件中使用 serverless along with serverless-webpack, you must use this custom-config

custom:
  # ...
  webpack:
    # ...
    packagerOptions:
      scripts:
        - npm install --arch=x64 --platform=linux sharp

如果你喜欢yarn,通过环境变量设置

注意:我先清理现有的 platform/arch。

# For macOS/Windows, we can install natively
yarn remove sharp
yarn add sharp

# For AWS Lambda, we need to specify this when we package:
yarn remove sharp
npm_config_platform=linux npm_config_arch=x64 yarn add sharp

如果您使用 SAM,您可以在类似 AWS Lambda 的容器中构建您的函数 Linux Docker 容器:

sam build -u

对于现在遇到此问题的任何人 post。我通过将 package.json 文件复制到 AWS Cloud9 IDE 并简单地 运行 npm install 来完成此操作。从那里,只需下载 node_modules/ 文件夹。

如果您像我一样使用 Docker 并遇到这个问题。

很可能您的 node_modules 文件夹正在从您的主机 machine 复制到 docker 容器中。

为什么?因为您的 node_modules 文件夹会下载特定于您的 mac 架构的模块(在我的例子中是 Mac),当您获取这些包并尝试将它们 运行 放入容器中时OS,它会导致问题,因为二进制文件是针对 mac (darwin-x64) 的,它试图 运行 它们在 linux (linux-x64) 里面容器

要修复它,请在与 Dockerfile 相同的目录中创建一个名为 .dockerignore 的文件并相应地更新它们。

.dockerignore

node_modules/

Dockerfile

FROM node:latest
WORKDIR /usr/src/app
COPY package*.json ./
RUN ["npm", "install"]
COPY . .
CMD ["npm", "start"]