[英]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:这里有两个选择:
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 部署,以下在使用serverless 、 esbuild和serverless-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 packager和packagerOptions选项以获得更多理解。
esbuild:
# keep existing configurations
external:
- sharp
packagerOptions:
scripts:
- npm install --arch=x64 --platform=linux sharp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.