├── .github └── workflows │ └── build.yml ├── LICENSE ├── PREV_VERSION ├── README.md └── test.mjs /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build lambda layer for sharp 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | schedule: 11 | - cron: '0 4 * * *' 12 | workflow_dispatch: 13 | 14 | jobs: 15 | build: 16 | runs-on: ubuntu-latest 17 | permissions: 18 | contents: write 19 | steps: 20 | - name: Setup node 21 | uses: actions/setup-node@v4 22 | with: 23 | node-version: '^20.10.0' 24 | - name: Checkout 25 | uses: actions/checkout@v3 26 | - name: Install esbuild 27 | run: npm i -g esbuild@0.18.20 28 | 29 | - name: Install sharp - arm64 30 | run: npm i --save-exact --os=linux --cpu=arm64 --libc=glibc sharp 31 | - name: Remove musl-based binary # @npm/cli#6914 32 | run: rm -rf node_modules/@img/*-linuxmusl* 33 | - name: Get sharp version 34 | id: version 35 | uses: notiz-dev/github-action-json-property@release 36 | with: 37 | path: 'package.json' 38 | prop_path: 'dependencies.sharp' 39 | - name: esbuild - arm64 40 | run: esbuild --bundle ./node_modules/sharp/ --outfile=index.js --minify --format=cjs --platform=node 41 | - name: Zip - arm64 42 | run: | 43 | mkdir -p nodejs/node_modules/sharp/lib 44 | mv index.js nodejs/node_modules/sharp/lib/ 45 | mv node_modules/sharp/package.json nodejs/node_modules/sharp/ 46 | mv node_modules/sharp/LICENSE nodejs/node_modules/sharp/ 47 | mv node_modules/sharp/lib/index.d.ts nodejs/node_modules/sharp/lib/ 48 | mv node_modules/@img nodejs/node_modules/ 49 | zip -r release-arm64 nodejs 50 | - name: Clean arm64 51 | run: rm -rf nodejs node_modules # keep package.json for keeping the version 52 | 53 | - name: Install sharp - x64 54 | run: npm i --save-exact --os=linux --cpu=x64 --libc=glibc sharp 55 | - name: Remove musl-based binary # @npm/cli#6914 56 | run: rm -rf node_modules/@img/*-linuxmusl* 57 | - name: esbuild - x64 58 | run: esbuild --bundle ./node_modules/sharp/ --outfile=index.js --minify --format=cjs --platform=node 59 | - name: Zip - x64 60 | run: | 61 | mkdir -p nodejs/node_modules/sharp/lib 62 | mv index.js nodejs/node_modules/sharp/lib/ 63 | mv node_modules/sharp/package.json nodejs/node_modules/sharp/ 64 | mv node_modules/sharp/LICENSE nodejs/node_modules/sharp/ 65 | mv node_modules/sharp/lib/index.d.ts nodejs/node_modules/sharp/lib/ 66 | mv node_modules/@img nodejs/node_modules/ 67 | zip -r release-x64 nodejs 68 | - name: Clean x64 69 | run: rm -rf nodejs node_modules # keep package.json for keeping the version 70 | 71 | - name: Install sharp - all 72 | run: | 73 | npm i --os=linux --cpu=x64 --libc=glibc sharp 74 | npm i --os=linux --cpu=arm64 --libc=glibc sharp 75 | - name: Remove musl-based binary # @npm/cli#6914 76 | run: rm -rf node_modules/@img/*-linuxmusl* 77 | - name: esbuild - all 78 | run: esbuild --bundle ./node_modules/sharp/ --outfile=index.js --minify --format=cjs --platform=node 79 | - name: Zip - all 80 | run: | 81 | mkdir -p nodejs/node_modules/sharp/lib 82 | mv index.js nodejs/node_modules/sharp/lib/ 83 | mv node_modules/sharp/package.json nodejs/node_modules/sharp/ 84 | mv node_modules/sharp/LICENSE nodejs/node_modules/sharp/ 85 | mv node_modules/sharp/lib/index.d.ts nodejs/node_modules/sharp/lib/ 86 | mv node_modules/@img nodejs/node_modules/ 87 | zip -r release-all nodejs 88 | 89 | - name: Test 90 | run: | 91 | cp test.mjs nodejs/test.mjs 92 | node nodejs/test.mjs 93 | if [ ! -f text_bw.png ]; then exit 1 ; fi 94 | 95 | - name: Current version 96 | id: version_current 97 | run: echo "sharpver=$(cat PREV_VERSION)" >> $GITHUB_ENV 98 | - name: Update PREV_VERSION 99 | if: ${{ env.sharpver != steps.version.outputs.prop && github.event_name != 'pull_request' }} 100 | run: echo ${{steps.version.outputs.prop}} > PREV_VERSION 101 | - name: Create commit 102 | uses: stefanzweifel/git-auto-commit-action@v4 103 | if: ${{ env.sharpver != steps.version.outputs.prop && github.event_name != 'pull_request' }} 104 | with: 105 | commit_message: ${{steps.version.outputs.prop}} 106 | file_pattern: PREV_VERSION 107 | - name: Create release 108 | uses: softprops/action-gh-release@v1 109 | if: ${{ env.sharpver != steps.version.outputs.prop && github.event_name != 'pull_request' }} 110 | with: 111 | files: "release*" 112 | body: ${{steps.version.outputs.prop}} 113 | tag_name: ${{steps.version.outputs.prop}} 114 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "[]" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright [yyyy] [name of copyright owner] 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | -------------------------------------------------------------------------------- /PREV_VERSION: -------------------------------------------------------------------------------- 1 | 0.34.2 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sharp for AWS Lambda Layers 2 | 3 | [![GitHub release](https://img.shields.io/github/tag/pH200/sharp-layer.svg)](https://github.com/pH200/sharp-layer/tags) 4 | [![Build action](https://github.com/pH200/sharp-layer/actions/workflows/build.yml/badge.svg)](https://github.com/pH200/sharp-layer/actions/workflows/build.yml) 5 | 6 | ## About 7 | 8 | The prebuilt [sharp](https://www.npmjs.com/package/sharp) node module for AWS Lambda layer. 9 | 10 | ### Features 11 | 12 | - Built and tested automatically using GitHub Actions 13 | - Automatically releases [sharp](https://www.npmjs.com/package/sharp) updates with GitHub Actions 14 | - Separated builds for `arm64` and `x64` 15 | - Minified and bundled with `esbuild` 16 | - Minimum `6.98 MB` zip file to optimize cold start time 17 | 18 | ### Why use a bundled Lambda function? / Why separate build for arm64? 19 | 20 | Please check out [Optimizing Node.js dependencies in AWS Lambda](https://aws.amazon.com/blogs/compute/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. 21 | 22 | ## Download 23 | 24 | [**Releases**](https://github.com/pH200/sharp-layer/releases) 25 | 26 | Download latest [release-arm64.zip](https://github.com/pH200/sharp-layer/releases/latest/download/release-arm64.zip) or [release-x64.zip](https://github.com/pH200/sharp-layer/releases/latest/download/release-x64.zip) 27 | 28 | ## Usage 29 | 30 | ```js 31 | import sharp from 'sharp' 32 | ``` 33 | 34 | Check out [aws: Creating and sharing Lambda layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html) for more details. 35 | 36 | This package can be used with [sst](https://sst.dev). Check out [docs.sst.dev: Lambda Layers](https://docs.sst.dev/advanced/lambda-layers) and [sst.dev: Resize Images](https://sst.dev/examples/how-to-automatically-resize-images-with-serverless.html) for examples. 37 | 38 | ### Setting arm64 for sst functions 39 | 40 | ```js 41 | function: { 42 | handler: '{handler}', 43 | runtime: 'nodejs18.x', 44 | architecture: 'arm_64', 45 | nodejs: { 46 | esbuild: { 47 | external: ['sharp'], 48 | }, 49 | }, 50 | layers: [ 51 | new lambda.LayerVersion(stack, 'SharpLayer', { 52 | code: lambda.Code.fromAsset('layers/sharp'), 53 | compatibleArchitectures: [ 54 | lambda.Architecture.ARM_64 55 | ] 56 | }), 57 | ] 58 | } 59 | ``` 60 | 61 | ### Setting up a lambda layer for AWS SAM 62 | 63 | Providing **a zip file** locally actually works, even though it's **not** mentioned in the documentation. 64 | 65 | ```yml 66 | ## Lambda 67 | ImageFunction: 68 | Type: AWS::Serverless::Function 69 | Properties: 70 | CodeUri: image-lambda/ 71 | Handler: app.handler 72 | Runtime: nodejs18.x 73 | Architectures: 74 | - arm64 75 | Timeout: 30 76 | MemorySize: 1024 77 | Layers: 78 | - !Ref SharpLayer 79 | Metadata: 80 | BuildMethod: esbuild 81 | BuildProperties: 82 | # Check these two issues for problems related to esm and esbuild 83 | # https://github.com/evanw/esbuild/issues/1921 84 | # https://github.com/evanw/esbuild/pull/2067#issuecomment-1503688128 85 | # Switch to cjs when esm doesn't work 86 | Format: esm 87 | OutExtension: 88 | - .js=.mjs 89 | EntryPoints: 90 | - app.ts 91 | External: 92 | - '@aws-sdk/*' # @aws-sdk 3.x is installed globally for nodejs18.x 93 | - sharp # use layer 94 | ## Lambda layer 95 | SharpLayer: 96 | Type: AWS::Serverless::LayerVersion 97 | Properties: 98 | LayerName: sharp 99 | ContentUri: layers/sharp/release-arm64.zip # zip 100 | CompatibleArchitectures: 101 | - arm64 102 | CompatibleRuntimes: 103 | - nodejs18.x 104 | - nodejs16.x 105 | ``` 106 | 107 | ## Build 108 | 109 | Fork this repo -> Actions -> Run build.yml 110 | 111 | ## References 112 | 113 | [Umkus/lambda-layer-sharp](https://github.com/Umkus/lambda-layer-sharp) - another maintained sharp lambda layer 114 | 115 | [aws: Creating and sharing Lambda layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html) 116 | 117 | [aws: Working with layers](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-layers.html) 118 | 119 | [aws: Building layers](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-layers.html) 120 | 121 | [sharp: Installation - AWS Lambda](https://sharp.pixelplumbing.com/install#aws-lambda) 122 | -------------------------------------------------------------------------------- /test.mjs: -------------------------------------------------------------------------------- 1 | import sharp from 'sharp' 2 | // Generate an image from text 3 | await sharp({ 4 | text: { 5 | text: 'Hello, world!', 6 | width: 400, // max width 7 | height: 300 // max height 8 | } 9 | }).toFile('text_bw.png') 10 | --------------------------------------------------------------------------------