英文:
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 的 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
if not u can look up image-resizer-service
by AWS and use that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论