├── .dockerignore ├── .gitignore ├── Dockerfile ├── package.json ├── webpack.config.js ├── README.md └── .github └── workflows └── docker-workflow.yml /.dockerignore: -------------------------------------------------------------------------------- 1 | ** 2 | !package-lock.json 3 | !package.json 4 | !webpack.config.js 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | coverage 3 | node_modules/ 4 | dist/ 5 | *.iml 6 | .DS_Store 7 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM lambci/lambda:build-nodejs12.x 2 | 3 | WORKDIR /build 4 | 5 | COPY * ./ 6 | 7 | RUN npm --no-optional --no-audit --progress=false install 8 | 9 | RUN node ./node_modules/webpack/bin/webpack.js 10 | 11 | RUN node -e "console.log(require('sharp'))" 12 | 13 | RUN mkdir /dist && \ 14 | echo "cp /build/dist/sharp-layer.zip /dist/sharp-layer.zip" > /entrypoint.sh && \ 15 | chmod +x /entrypoint.sh 16 | 17 | ENTRYPOINT "/entrypoint.sh" -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lambda-layer-sharp", 3 | "version": "1.1.0", 4 | "repository": "git@github.com:Umkus/lambda-layer-sharp.git", 5 | "license": "Apache License 2.0", 6 | "module": "lambda-layer-sharp", 7 | "devDependencies": { 8 | "webpack-node-externals": "^3.0.0", 9 | "copy-webpack-plugin": "^6.4.1", 10 | "webpack": "^5.65.0", 11 | "webpack-cli": "^4.9.1", 12 | "zip-webpack-plugin": "^4.0.1" 13 | }, 14 | "dependencies": { 15 | "sharp": "^0.30.0" 16 | }, 17 | "engines": { 18 | "node": ">=14" 19 | }, 20 | "author": "Mykhailo Gorianskyi " 21 | } 22 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const CopyPlugin = require('copy-webpack-plugin'); 3 | const ZipPlugin = require('zip-webpack-plugin'); 4 | 5 | const distPath = 'nodejs/node_modules/sharp'; 6 | 7 | module.exports = { 8 | name: 'layer', 9 | mode: 'production', 10 | stats: 'minimal', 11 | target: 'node', 12 | watch: false, 13 | entry: { 14 | [`${distPath}/index`]: './node_modules/sharp/lib/index', 15 | }, 16 | plugins: [ 17 | new CopyPlugin({ 18 | patterns: [ 19 | { 20 | from: 'node_modules/sharp/build', 21 | to: `${distPath}/build`, 22 | }, 23 | { 24 | from: 'node_modules/sharp/LICENSE', 25 | to: distPath, 26 | }, 27 | { 28 | from: `node_modules/sharp/vendor`, 29 | to: `${distPath}/vendor`, 30 | }, 31 | ], 32 | }), 33 | new ZipPlugin({ 34 | filename: 'sharp-layer.zip', 35 | }) 36 | ], 37 | optimization: { 38 | // minimize will be enabled in Github Actions 39 | minimize: process.env.NODE_ENV === 'production', 40 | }, 41 | output: { 42 | filename: '[name].js', 43 | path: path.resolve(__dirname, 'dist'), 44 | libraryTarget: 'commonjs2', 45 | }, 46 | externals: { 47 | './sharp-linux-x64.node': './build/Release/sharp-linux-x64.node', 48 | }, 49 | }; 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AWS Sharp layer 2 | This AWS lambda layer contains a pre-built [sharp](https://www.npmjs.com/package/sharp) npm library. 3 | It is optimized for the most frugal space usage possible. 4 | 5 | # Getting 6 | A pre-built layer zip file is available on the [Releases page](../../releases), alongside the size of the layer. 7 | 8 | # Building 9 | 10 | ## Dependencies 11 | * Docker 12 | 13 | ## Steps 14 | 1. Clone the repo: 15 | ```shell script 16 | git clone git@github.com:Umkus/lambda-layer-sharp.git 17 | cd lambda-layer-sharp/ 18 | ``` 19 | 1. Install dependencies: 20 | ```shell script 21 | docker run -v "$PWD":/var/task lambci/lambda:build-nodejs12.x npm --no-optional --no-audit --progress=false install 22 | ``` 23 | 1. Build the layer: 24 | ```shell script 25 | docker run -v "$PWD":/var/task lambci/lambda:build-nodejs12.x node ./node_modules/webpack/bin/webpack.js 26 | ``` 27 | 1. Perform a smoke-test: 28 | ```shell script 29 | docker run -w /var/task/dist/nodejs -v "$PWD":/var/task lambci/lambda:build-nodejs12.x node -e "console.log(require('sharp'))" 30 | ``` 31 | 1. Import created layer into your AWS account: 32 | ```shell script 33 | aws lambda publish-layer-version --layer-name sharp --description "Sharp layer" --license-info "Apache License 2.0" --zip-file fileb://dist/sharp-layer.zip --compatible-runtimes nodejs12.x 34 | ``` 35 | -------------------------------------------------------------------------------- /.github/workflows/docker-workflow.yml: -------------------------------------------------------------------------------- 1 | name: Build Layer ZIP 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | schedule: 11 | - cron: '0 0 * * *' 12 | 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - name: Checkout 18 | uses: actions/checkout@v2 19 | with: 20 | fetch-depth: 0 21 | - name: Read version 22 | id: package_lock_json 23 | run: | 24 | content=$(cat ./package-lock.json) 25 | content="${content//'%'/'%25'}" 26 | content="${content//$'\n'/'%0A'}" 27 | content="${content//$'\r'/'%0D'}" 28 | echo "::set-output name=packageLockJson::$content" 29 | - name: Variables 30 | id: vars 31 | run: | 32 | sharp_version="${{ fromJSON(steps.package_lock_json.outputs.packageLockJson).dependencies.sharp.version }}" 33 | echo "::set-output name=sharp_version::$sharp_version" 34 | 35 | release_exists="true" 36 | git show-ref --tags --quiet --verify -- "refs/tags/$sharp_version" || release_exists="false" 37 | echo "::set-output name=release_exists::$release_exists" 38 | - name: Build 39 | id: docker_build 40 | uses: docker/build-push-action@v2 41 | with: 42 | context: . 43 | file: ./Dockerfile 44 | tags: amazon-linux-sharp-layer:dev 45 | - name: Copy artifacts 46 | run: docker run -v "${{ github.workspace }}/dist":/dist amazon-linux-sharp-layer:dev 47 | - name: Create release 48 | uses: svenstaro/upload-release-action@v2 49 | with: 50 | repo_token: ${{ secrets.GITHUB_TOKEN }} 51 | tag: ${{ steps.vars.outputs.sharp_version }} 52 | file: dist/sharp-layer.zip 53 | overwrite: true 54 | --------------------------------------------------------------------------------