-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre-existing lambda layer example
fixes #77
- Loading branch information
Showing
9 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
examples/serverless-with-preexisting-lambda-layer/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
examples/serverless-with-preexisting-lambda-layer/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
examples/serverless-with-preexisting-lambda-layer/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
examples/serverless-with-preexisting-lambda-layer/serverless.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:* |