我正在尝试在几个 AWS lambda@edge 上使用 Sharp。这个想法是在请求时调整图像大小并缓存图像( see this )。
我也在使用serverless与 serverless-webpack部署 lambda。
我可以部署 lambda,如果我在 AWS 控制台中测试它们,一切都会顺利。
但是,这些是 lamda@edge,它们将用作 cloudwatch 请求/响应触发器。因此,最大 lambda 大小为 1Mb。
我的问题是我似乎无法接近这个大小,我能达到的最好大小是 11.6Mb。而且,正如第一个链接中所示,这似乎是可能的。
这是无服务器配置,产生 34.7Mb lambda:
custom:
webpack:
includeModules:
forceExclude:
- aws-sdk
packagerOptions:
scripts:
- rm -rf node_modules/sharp && docker run -v "$PWD":/var/task lambci/lambda:build-nodejs10.x npm install sharp
package:
exclude:
- .env
- .git/**
- .gitlab-ci.yml
- tests*
excludeDevDependencies: true
individually: true
这样我就得到了 11.6Mb:
custom:
webpack:
includeModules:
forceExclude:
- aws-sdk
packagerOptions:
scripts:
- npm rebuild sharp --target=10.15.0 --target_arch=x64 --target_platform=linux
package:
exclude:
- .env
- .git/**
- .gitlab-ci.yml
- tests*
excludeDevDependencies: true
individually: true
我也尝试过 package.exclude,但没有运气:
- node_modules/**
- '!node_modules/sharp/**'
这是我的 webpack 配置:
const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const entries = {};
Object.keys(slsw.lib.entries).forEach(key => (entries[key] = ['./source-map-install.js', slsw.lib.entries[key]]));
module.exports = {
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
devtool: 'source-map',
resolve: {
extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'],
},
// externals: ['sharp'], #tried that too
externals: [nodeExternals()],
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
},
target: 'node',
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.ts?$/, loader: 'ts-loader', options: { happyPackMode: true } },
],
},
};
在本地运行时,我可以看到它正在打包的内容...node_modules 文件夹似乎有尖锐的内容及其依赖项。但最大的文件夹是锋利的。
我怀疑我正在将不需要的东西打包在锋利的文件夹中......但我似乎不明白是什么。
有什么帮助吗?
谢谢
更新: 仔细阅读,似乎我需要尖锐(源响应)大小限制的函数是 5Mb。 我只需要找到一种只为该功能打包 Sharp 的方法。 Webpack 似乎将它放在两者中,即使我在其他功能(查看器请求)上不需要它。 有什么帮助吗?
最佳答案
我最终在 custom.webpack.packagerOptions.scripts
中运行了一个脚本,该脚本将忽略不需要的 Sharp 内容。
这是我使用的脚本:
custom:
webpack:
includeModules:
forceExclude:
- aws-sdk
packagerOptions: # uncomment this block if invoking locally
scripts:
- if [ -f "src/handlers/myfunction.js" ]; then rm -rf node_modules/sharp && docker run -v "$PWD":/var/task lambci/lambda:build-nodejs10.x npm install sharp; else rm -rf node_modules; fi
关于serverless-framework - 在 lambda@edge 上使用 Sharp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60096160/