如何通过自定义安装命令在我的AWS Lambda上使用CDK安装sharp?

huangapple go评论69阅读模式
英文:

How do I install sharp on my AWS Lambda via the CDK with a custom install command?

问题

I have an error message asking me to install a specific version of sharp for my AWS lambda function: npm install --platform=linux --arch=x64 sharp.

Whilst I can do this locally, I believe the CDK just runs npm i sharp, which causes issues.

Current CDK definition:

this.sharpUser = new NodejsFunction(this, `sharpUser-${context.environment}`, {
  runtime: Runtime.NODEJS_18_X,
  handler: "handler",
  entry: join(__dirname, "./sharpUser.ts"),
  bundling: {
    nodeModules: ["sharp", "@aws-sdk/client-s3"],
    externalModules: ["aws-sdk"],
  },
});

Error:

  Something went wrong installing the "sharp" module
  
  Cannot find module '../build/Release/sharp-linux-x64.node'
  Require stack:
  - /var/task/node_modules/sharp/lib/sharp.js
  - /var/task/node_modules/sharp/lib/constructor.js
  - /var/task/node_modules/sharp/lib/index.js
  - /var/task/index.js
  - /var/runtime/index.mjs
  
  Possible solutions:
  - Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
  - Install for the current linux-x64 runtime: "npm install --platform=linux --arch=x64 sharp"
  - Consult the installation documentation: https://sharp.pixelplumbing.com/install".
英文:

I have an error message asking me to install a specific version of sharp for my AWS lambda function: npm install --platform=linux --arch=x64 sharp.

Whilst I can do this locally, I believe the CDK just runs npm i sharp, which causes issues.

Current CDK definition:

this.sharpUser = new NodejsFunction(this, `sharpUser-${context.environment}`, {
  runtime: Runtime.NODEJS_18_X,
  handler: "handler",
  entry: join(__dirname, "./sharpUser.ts"),
  bundling: {
    nodeModules: ["sharp", "@aws-sdk/client-s3"],
    externalModules: ["aws-sdk"],
  },
});

Error:

  Something went wrong installing the \"sharp\" module
  
  Cannot find module '../build/Release/sharp-linux-x64.node'
  Require stack:
  - /var/task/node_modules/sharp/lib/sharp.js
  - /var/task/node_modules/sharp/lib/constructor.js
  - /var/task/node_modules/sharp/lib/index.js
  - /var/task/index.js
  - /var/runtime/index.mjs
  
  Possible solutions:
  - Install with verbose logging and look for errors: \"npm install --ignore-scripts=false --foreground-scripts --verbose sharp\"
  - Install for the current linux-x64 runtime: \"npm install --platform=linux --arch=x64 sharp\"
  - Consult the installation documentation: https://sharp.pixelplumbing.com/install",

答案1

得分: 1

这是我使它工作的方法:

我的 CDK 配置用于我的 Node.js 函数如下:

bundling: {
    nodeModules: ["sharp"],
    forceDockerBundling: true, // 强制 Docker 打包 sharp
},
memorySize: 2048,
timeout: Duration.minutes(2)

然后我进入了我的 src 文件夹,这里存放着 Lambda 代码。使用 npm 初始化了一个新项目:

npm init -y
npm install sharp

然后像正常导入 sharp 库并使用它:

import sharp from "sharp";

如果使用 TypeScript,你可能需要设置 ts 配置文件中的 esModuleInterop: true

虽然不是最干净的解决方案,但对我有用 如何通过自定义安装命令在我的AWS Lambda上使用CDK安装sharp?

如果不行,你可以查找 AWS 的 image-resizer-service 并使用它。

英文:

this is how i got it working:

my cdk config for my nodejs function as follow:

 bundling: {
      nodeModules: ["sharp"],
      forceDockerBundling: true, // force docker bundling for sharp
 },
memorySize: 2048,
timeout: Duration.minutes(2)

then i went into my src folder , where the lambda code is stored. Initialized a new project using npm:

npm init -y
npm install sharp

then import sharp lib as normal and use it:

import sharp from "sharp";

if using typescript you may need set this ts config file "esModuleInterop": true

not the cleanest solution, but it worked for me 如何通过自定义安装命令在我的AWS Lambda上使用CDK安装sharp?

if not u can look up image-resizer-service by AWS and use that.

huangapple
  • 本文由 发表于 2023年6月6日 01:00:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定