Skip to content

Commit

Permalink
Add pre-existing lambda layer example
Browse files Browse the repository at this point in the history
fixes #77
  • Loading branch information
Sparticuz committed Mar 27, 2023
1 parent a7320dd commit 503db13
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions examples/serverless-with-preexisting-lambda-layer/README.md
Original file line number Diff line number Diff line change
@@ -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
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions examples/serverless-with-preexisting-lambda-layer/index.js
Original file line number Diff line number Diff line change
@@ -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);
}
},
};
21 changes: 21 additions & 0 deletions examples/serverless-with-preexisting-lambda-layer/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
14 changes: 14 additions & 0 deletions examples/serverless-with-preexisting-lambda-layer/serverless.yml
Original file line number Diff line number Diff line change
@@ -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:*

0 comments on commit 503db13

Please sign in to comment.