diff --git a/README.md b/README.md index 8fde24e..316ffc5 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ Here are some example projects and help with other services - [Production Dependency](https://github.com/Sparticuz/chromium/tree/master/examples/production-dependency) - [Serverless Framework with Lambda Layer](https://github.com/Sparticuz/chromium/tree/master/examples/serverless-with-lambda-layer) +- [Serverless Framework with Pre-existing Lambda Layer](https://github.com/Sparticuz/chromium/tree/master/examples/serverless-with-preexisting-lambda-layer) - [Chromium-min](https://github.com/Sparticuz/chromium/tree/master/examples/remote-min-binary) - AWS SAM _TODO_ - [Webpack](https://github.com/Sparticuz/chromium/issues/24#issuecomment-1343196897) diff --git a/examples/serverless-with-preexisting-lambda-layer/README.md b/examples/serverless-with-preexisting-lambda-layer/README.md new file mode 100644 index 0000000..d6f780d --- /dev/null +++ b/examples/serverless-with-preexisting-lambda-layer/README.md @@ -0,0 +1,40 @@ +## Upload Lambda layer to AWS + +1. Download the layer zip from Github Releases + ![Step 1](docs/step1.png) +2. Create a S3 bucket, or use a pre-existing bucket, upload the zip, and copy the URL + ![Step 2](docs/step2.png) +3. Create a new layer or use a pre-existing layer. (If using a pre-existing layer, Create a new verion) + ![Step 3](docs/step3.png) +4. Use the S3 file to load into to AWS Lambda Layers + ![Step 4](docs/step4.png) +5. Add the layer to your serverless function + +```yaml +service: sls-with-preexisting-layer + +provider: + name: aws + runtime: nodejs18.x + stage: dev + region: us-east-1 + timeout: 300 + +functions: + chromium-test: + handler: index.handler + layers: + - arn:aws:lambda:us-east-1:************:layer:chromium:* +``` + +# BONUS + +These steps can easily be automated using the following code: + +```shell +$ chromiumVersion="112.0.0" +$ bucketName="chromiumUploadBucket" +$ wget "https://github.com/Sparticuz/chromium/releases/download/v${chromiumVersion}/chromium-v${chromiumVersion}-layer.zip" +$ aws s3 cp "chromium-v${chromiumVersion}-layer.zip" "s3://${bucketName}/chromiumLayers/chromium-v${chromiumVersion}-layer.zip" +$ aws lambda publish-layer-version --layer-name chromium --description "Chromium v${chromiumVersion}" --content "S3Bucket=${bucketName},S3Key=chromiumLayers/chromium-v${chromiumVersion}-layer.zip" --compatible-runtimes nodejs --compatible-architectures x86_64 +``` diff --git a/examples/serverless-with-preexisting-lambda-layer/docs/step1.png b/examples/serverless-with-preexisting-lambda-layer/docs/step1.png new file mode 100644 index 0000000..f52711d Binary files /dev/null and b/examples/serverless-with-preexisting-lambda-layer/docs/step1.png differ diff --git a/examples/serverless-with-preexisting-lambda-layer/docs/step2.png b/examples/serverless-with-preexisting-lambda-layer/docs/step2.png new file mode 100644 index 0000000..cfa9925 Binary files /dev/null and b/examples/serverless-with-preexisting-lambda-layer/docs/step2.png differ diff --git a/examples/serverless-with-preexisting-lambda-layer/docs/step3.png b/examples/serverless-with-preexisting-lambda-layer/docs/step3.png new file mode 100644 index 0000000..e2101b3 Binary files /dev/null and b/examples/serverless-with-preexisting-lambda-layer/docs/step3.png differ diff --git a/examples/serverless-with-preexisting-lambda-layer/docs/step4.png b/examples/serverless-with-preexisting-lambda-layer/docs/step4.png new file mode 100644 index 0000000..28827c9 Binary files /dev/null and b/examples/serverless-with-preexisting-lambda-layer/docs/step4.png differ diff --git a/examples/serverless-with-preexisting-lambda-layer/index.js b/examples/serverless-with-preexisting-lambda-layer/index.js new file mode 100644 index 0000000..039cdec --- /dev/null +++ b/examples/serverless-with-preexisting-lambda-layer/index.js @@ -0,0 +1,29 @@ +const puppeteer = require("puppeteer-core"); +const chromium = require("@sparticuz/chromium"); + +module.exports = { + handler: async () => { + try { + const browser = await puppeteer.launch({ + args: chromium.args, + defaultViewport: chromium.defaultViewport, + executablePath: await chromium.executablePath(), + headless: chromium.headless, + ignoreHTTPSErrors: true, + }); + + const page = await browser.newPage(); + + await page.goto("https://www.example.com", { waitUntil: "networkidle0" }); + + console.log("Chromium:", await browser.version()); + console.log("Page Title:", await page.title()); + + await page.close(); + + await browser.close(); + } catch (error) { + throw new Error(error.message); + } + }, +}; diff --git a/examples/serverless-with-preexisting-lambda-layer/package.json b/examples/serverless-with-preexisting-lambda-layer/package.json new file mode 100644 index 0000000..b3d109f --- /dev/null +++ b/examples/serverless-with-preexisting-lambda-layer/package.json @@ -0,0 +1,21 @@ +{ + "name": "serverless-with-lambda-layer", + "version": "0.0.0", + "description": "This package demonstrates using @sparticuz/chromium as a devDependency with a layer that contains the binaries", + "license": "ISC", + "author": { + "name": "Kyle McNally" + }, + "main": "index.js", + "scripts": { + "deploy": "sls deploy", + "test": "sls invoke --function chromium-test --log" + }, + "dependencies": { + "puppeteer-core": "19.6.3" + }, + "devDependencies": { + "@sparticuz/chromium": "110.0.0", + "serverless": "^3.27.0" + } +} diff --git a/examples/serverless-with-preexisting-lambda-layer/serverless.yml b/examples/serverless-with-preexisting-lambda-layer/serverless.yml new file mode 100644 index 0000000..503f54a --- /dev/null +++ b/examples/serverless-with-preexisting-lambda-layer/serverless.yml @@ -0,0 +1,14 @@ +service: sls-with-layer + +provider: + name: aws + runtime: nodejs18.x + stage: dev + region: us-east-1 + timeout: 300 + +functions: + chromium-test: + handler: index.handler + layers: + - arn:aws:lambda:us-east-1:************:layer:chromium:*