How to Deploy Sharp as an AWS Lambda Function Layer

blog cover image

When it comes to image processing and manipulation, Sharp is very much so the NodeJS default. It comes with a lot of built-in functions for things like resizing, cropping, and rotating images--all of which are helpful when wanting to work with AI models since many of them have limitations on the size of the images they can handle.

While powerful, Sharp is also fairly simple to use. In fact, if you install the Sharp library in a local NodeJS project, you can begin transforming images in just a few lines of code:

# assumes a NodeJS version of 20 or higher
mkdir sharp-testing && cd sharp-testing
touch index.mjs
npm init -y && npm install sharp
//file: index.mjs
//assume a file called cover.png exists in the same directory as index.mjs
import sharp from 'sharp'

const image = sharp('./cover.png')

//Convert to JPEG but keep the original aspect ratio
await image.resize(500, 500, { fit: 'inside' }).toFile('output.jpg')

console.log('done')
node index.mjs

This is great for local development, but what if you want to use Sharp in an AWS Lambda function?

Well, that's where things get a bit tricky. That's because Sharp is a wrapper around the C++ library libvips, so when you install Sharp, it builds a native binary for your specific platform--locally, that platform is Mac, Windows, or Linux. But when you deploy it to a Lambda function, that Linux environment is specific to AWS (x86_64 or arm64).

Because of that nuance, we'll deploy a copy of the Sharp binary suitable for Lambda functions to our AWS account. That will allow us to install Sharp as a dev dependency locally, while being able to use that Layer in any Lambda functions we deploy.

Getting a Build of the Sharp Binary

While it's definitely possible to build the Sharp binary yourself, it's a bit of a pain as it requires either docker or setting up a Linux VM. Thankfully, there are plenty of people who have already done that, and open source their builds. For this tutorial, we'll use the Sharp for AWS Lambda Layers project by pH200.

๐Ÿ—’๏ธ The reason I'm recommending this project is because the latest version of Sharp (as of this writing) is 0.33.5, which is also the version that is supported by this pH200's repo.

With that, all we have to do is download the latest release from the pH200/sharp-layer GitHub repo to our local machine.

Sharp releases

๐Ÿ—’๏ธ Feel free to download the release-all.zip file, the release-x64.zip file, or the release-arm64.zip file. Just note that will correspond to the Lambda function architecture you're targeting. For this tutorial, I'll userelease-arm64.zip simply save it to my Desktop.

Deploying the Layer

Now that we have the Sharp binary, we can deploy it as a Lambda function layer. Open up your terminal, change into the directory where you saved the release-arm64.zip file (Desktop in my case), and run the following command:

aws lambda publish-layer-version \
    --layer-name sharp \
    --description "Sharp layer" \
    --license-info "Apache License 2.0" \
    --zip-file fileb://release-arm64.zip \
    --compatible-runtimes nodejs20.x nodejs22.x \
    --compatible-architectures arm64

This command will deploy the Sharp binary as a Lambda function layer.

Upon successful deployment, you should see something like the following:

{
    "LayerVersionArn": "arn:aws:lambda:us-east-1:123456789012:layer:sharp:1",
    "LayerName": "sharp",
    "Description": "Sharp layer",
    "CreatedDate": "2025-04-03T12:00:00Z"
}

๐Ÿ—’๏ธ Note that the LayerVersionArn is the ARN of the layer, which is what is needed to use the layer in a Lambda function.

Conclusion

And that's it๐ŸŽ‰ We've now deployed the Sharp binary as a Lambda function layer. You can now use it in any Lambda function you deploy.