简体   繁体   English

如何将使用 sharp 库的 macOS 无服务器框架应用程序部署到 AWS?

[英]How can I deploy a Serverless Framework application the macOS that uses the sharp library to AWS?

I'm developing a Serverless Framework application that is using the Node runtime and is deployed to AWS.我正在开发一个使用 Node 运行时并部署到 AWS 的无服务器框架应用程序。 One of my AWS Lambda functions uses the sharp library.我的 AWS Lambda 函数之一使用了 sharp 库。

When I run the AWS Lambda function, the following error occurs:当我运行 AWS Lambda function 时,出现以下错误:

'darwin-x64' binaries cannot be used on the 'linux-x64' platform. “darwin-x64”二进制文件不能在“linux-x64”平台上使用。 Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'.请删除“node_modules/sharp/vendor”目录并运行“npm install”。

I believe this error is occurring because when I run the sls deploy command on my local computer, the application is packaged on macOS and then moved to AWS.我认为发生此错误是因为当我在本地计算机上运行sls deploy命令时,应用程序在 macOS 上打包,然后移动到 AWS。 I think the application needs to be packaged on an operating system using linux-x64 .我认为应用程序需要在使用linux-x64的操作系统上打包。

How can I deploy my Serverless Framework from my computer and still be able to use the sharp library?如何从我的计算机部署我的无服务器框架并且仍然能够使用 sharp 库?

You can install the Linux compatible package using the following:您可以使用以下命令安装 Linux 兼容包:

rm -rf node_modules/sharp
npm install --arch=x64 --platform=linux --target=10.15.0 sharp

Note that this also specifies a target NodeJS version, ensure its the same version of node you're using in your Lambda.请注意,这也指定了目标 NodeJS 版本,确保它与您在 Lambda 中使用的节点版本相同。 This is straight out of the docs (see here .)这是直接从文档中提取出来的(请参阅此处。)

However that didn't solve my problems.然而这并没有解决我的问题。 My serverless configuration (using serverless-bundle plugin) meant that my modules were being installed again in a separate folder, wiping out the platform-specific modules I just manually installed.我的无服务器配置(使用serverless-bundle插件)意味着我的模块被再次安装在一个单独的文件夹中,清除了我刚刚手动安装的特定于平台的模块。

Two choices here:这里有两个选择:

  1. use serverless-plugin-scripts to hook into the deploy events to run the above patch;使用serverless-plugin-scripts挂钩部署事件以运行上述补丁; or或者
  2. run serverless in docker using a Linux container with a matching node version.使用具有匹配节点版本的 Linux 容器在 docker 中运行无服务器。

For my specific edge case I had to go with Docker.对于我的特定边缘情况,我不得不使用 Docker。 The build scripts will effect every function you're deploying -- adding ~30mb of Sharp code -- and Lambda@Edge has limitations on source code size.构建脚本将影响您正在部署的每个功能——添加约 30mb 的 Sharp 代码——并且 Lambda@Edge 对源代码大小有限制。

The description of https://www.npmjs.com/package/sharp suggests it is linux compatible. https://www.npmjs.com/package/sharp的描述表明它与 linux 兼容。

I am unfamiliar with how to (or if you can) force nodes native package resolution to a separate OS.我不熟悉如何(或者如果可以的话)将节点本机包解析强制转换为单独的操作系统。 Try building your lambda zip inside a docker image https://hub.docker.com/_/alpine/尝试在 docker 映像中构建 lambda zip https://hub.docker.com/_/alpine/

If you have not already, I would suggest following the Installing the AWS SAM CLI on macOS guide to insure you have the correct local environment for developing Serverless on macOS.如果您还没有,我建议您遵循在 macOS 上安装 AWS SAM CLI指南以确保您拥有在 macOS 上开发无服务器的正确本地环境。

This process is designed for the macOS, and includes built-in support for Docker, so that you can build and deploy packages that are compatible with Lambda directly from your local computer.此过程专为 macOS 设计,包括对 Docker 的内置支持,因此您可以直接从本地计算机构建和部署与 Lambda 兼容的包。

For AWS lambda deployment with Sharp module, the following worked for me when using serverless , esbuild and serverless-esbuild .对于使用Sharp模块的 AWS lambda 部署,以下在使用serverlessesbuildserverless-esbuild时对我有用。 Changed the serverless.yml file with the below configuration.使用以下配置更改了 serverless.yml 文件。 It is basically telling esbuild to download sharp again with the following --arch=x64 --platform=linux considering your lambda uses x64 arch.考虑到您的 lambda 使用 x64 arch,它基本上是告诉 esbuild 使用以下--arch=x64 --platform=linux再次下载 sharp。 Check serverless-esbuild packager and packagerOptions options for more understanding.检查serverless-esbuild packagerpackagerOptions选项以获得更多理解。

esbuild:
    # keep existing configurations
    external:
      - sharp
    packagerOptions:
      scripts:
        - npm install --arch=x64 --platform=linux sharp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何像无服务器框架一样使用 AWS SAM 模板文件分离? - How can i use AWS SAM Template File separation like Serverless Framework use? 为什么它说应用程序无法在 AWS 上部署 - 无服务器 - Why it says application failed to deploy on AWS - Serverless AWS 无服务器应用程序模型 (SAM) — 如何更改 StageName? - AWS Serverless Application Model (SAM) -- How do I change StageName? 如何在要部署到 AWS 弹性豆茎中的 Flask 应用程序中使用我的 firebase 凭证? - How can I use my firebase credentials in a Flask application that I want to deploy in a AWS elastic beanstalk? 无服务器框架无法连接到 AWS 中现有的 REST API - Serverless framework can't connect to existing REST API in AWS 无服务器:无法使用部署命令在 AWS 上加载 lambda 函数 - Serverless: can't load lambda function on the AWS using deploy comand 如何从 AWS Lambda 函数 + 无服务器框架的 URL 中删除阶段? - How to remove stage from URLs for AWS Lambda functions + Serverless framework? 如何使用 Serverless Framework 在多个区域部署堆栈? - how to deploy a stack in multiple regions using the Serverless Framework? 如何在 AWS Lambda 上部署此代码并生成 csv? - How can I deploy this code on AWS Lambda and generate a csv? 无服务器框架命令不起作用:“sls deploy -v” - serverless framework command not working: "sls deploy -v"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM