The prebuilt sharp node module for AWS Lambda layer.
- Built and tested automatically using GitHub Actions
- Automatically releases sharp updates with GitHub Actions
- Separated builds for
arm64andx64 - Minified and bundled with
esbuild - Minimum
6.98 MBzip file to optimize cold start time
Please check out Optimizing Node.js dependencies in AWS Lambda for details. A bundled and minified lambda function can be up to 70% faster for cold starts. The package size is also crucial for cold start performance.
Download latest release-arm64.zip or release-x64.zip
importsharpfrom'sharp'Check out aws: Creating and sharing Lambda layers for more details.
This package can be used with sst. Check out docs.sst.dev: Lambda Layers and sst.dev: Resize Images for examples.
function: {handler: '{handler}',runtime: 'nodejs18.x',architecture: 'arm_64',nodejs: {esbuild: {external: ['sharp'],},},layers: [newlambda.LayerVersion(stack,'SharpLayer',{code: lambda.Code.fromAsset('layers/sharp'),compatibleArchitectures: [lambda.Architecture.ARM_64]}),]}Providing a zip file locally actually works, even though it's not mentioned in the documentation.
## LambdaImageFunction:
Type: AWS::Serverless::FunctionProperties:
CodeUri: image-lambda/Handler: app.handlerRuntime: nodejs18.xArchitectures:
- arm64Timeout: 30MemorySize: 1024Layers:
- !RefSharpLayerMetadata:
BuildMethod: esbuildBuildProperties:
# Check these two issues for problems related to esm and esbuild# https://github.com/evanw/esbuild/issues/1921# https://github.com/evanw/esbuild/pull/2067#issuecomment-1503688128# Switch to cjs when esm doesn't workFormat: esmOutExtension:
- .js=.mjsEntryPoints:
- app.tsExternal:
- '@aws-sdk/*'# @aws-sdk 3.x is installed globally for nodejs18.x
- sharp # use layer## Lambda layerSharpLayer:
Type: AWS::Serverless::LayerVersionProperties:
LayerName: sharpContentUri: layers/sharp/release-arm64.zip # zipCompatibleArchitectures:
- arm64CompatibleRuntimes:
- nodejs18.x
- nodejs16.xFork this repo -> Actions -> Run build.yml
Umkus/lambda-layer-sharp - another maintained sharp lambda layer