Git Product home page Git Product logo

sharp-layer's Introduction

sharp for AWS Lambda Layers

GitHub release Build action

About

The prebuilt sharp node module for AWS Lambda layer.

Features

  • Built and tested automatically using GitHub Actions
  • Automatically releases sharp updates with GitHub Actions
  • Separated builds for arm64 and x64
  • Minified and bundled with esbuild
  • Minimum 6.98 MB zip file to optimize cold start time

Why use a bundled Lambda function? / Why separate build for arm64?

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

Releases

Download latest release-arm64.zip or release-x64.zip

Usage

import sharp from '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.

Setting arm64 for sst functions

function: {
  handler: '{handler}',
  runtime: 'nodejs18.x',
  architecture: 'arm_64',
  nodejs: {
    esbuild: {
      external: ['sharp'],
    },
  },
  layers: [
    new lambda.LayerVersion(stack, 'SharpLayer', {
      code: lambda.Code.fromAsset('layers/sharp'),
      compatibleArchitectures: [
        lambda.Architecture.ARM_64
      ]
    }),
  ]
}

Setting up a lambda layer for AWS SAM

Providing a zip file locally actually works, even though it's not mentioned in the documentation.

  ## Lambda
  ImageFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: image-lambda/
      Handler: app.handler
      Runtime: nodejs18.x
      Architectures:
        - arm64
      Timeout: 30
      MemorySize: 1024
      Layers:
        - !Ref SharpLayer
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        # 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 work
        Format: esm
        OutExtension:
          - .js=.mjs
        EntryPoints:
          - app.ts
        External:
          - '@aws-sdk/*' # @aws-sdk 3.x is installed globally for nodejs18.x
          - sharp # use layer
  ## Lambda layer
  SharpLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: sharp
      ContentUri: layers/sharp/release-arm64.zip # zip
      CompatibleArchitectures:
        - arm64
      CompatibleRuntimes:
        - nodejs18.x
        - nodejs16.x

Build

Fork this repo -> Actions -> Run build.yml

References

Umkus/lambda-layer-sharp - another maintained sharp lambda layer

aws: Creating and sharing Lambda layers

aws: Working with layers

aws: Building layers

sharp: Installation - AWS Lambda

sharp-layer's People

Contributors

ph200 avatar docoder avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.