From e77f679a6b93c0257be4615fbd3dcc29b0a97de9 Mon Sep 17 00:00:00 2001 From: David Beitey Date: Mon, 13 Nov 2023 18:06:04 +1000 Subject: [PATCH] Add AWS SAM LayerVersion example --- README.md | 2 +- examples/aws-sam/.gitignore | 1 + examples/aws-sam/README.md | 19 +++++++++++ .../aws-sam/functions/exampleFunction/app.mjs | 24 ++++++++++++++ .../functions/exampleFunction/package.json | 13 ++++++++ examples/aws-sam/layers/chromium/package.json | 9 +++++ examples/aws-sam/template.yml | 33 +++++++++++++++++++ 7 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 examples/aws-sam/.gitignore create mode 100644 examples/aws-sam/README.md create mode 100644 examples/aws-sam/functions/exampleFunction/app.mjs create mode 100644 examples/aws-sam/functions/exampleFunction/package.json create mode 100644 examples/aws-sam/layers/chromium/package.json create mode 100644 examples/aws-sam/template.yml diff --git a/README.md b/README.md index 2b7a735..af7245f 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ Here are some example projects and help with other services - [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_ +- [AWS SAM](https://github.com/Sparticuz/chromium/tree/master/examples/aws-sam) - [Webpack](https://github.com/Sparticuz/chromium/issues/24#issuecomment-1343196897) - [Netlify](https://github.com/Sparticuz/chromium/issues/24#issuecomment-1414107620) diff --git a/examples/aws-sam/.gitignore b/examples/aws-sam/.gitignore new file mode 100644 index 0000000..0a03531 --- /dev/null +++ b/examples/aws-sam/.gitignore @@ -0,0 +1 @@ +.aws-sam diff --git a/examples/aws-sam/README.md b/examples/aws-sam/README.md new file mode 100644 index 0000000..fd77149 --- /dev/null +++ b/examples/aws-sam/README.md @@ -0,0 +1,19 @@ +# Chromium as a Layer for AWS SAM + +1. Install AWS SAM CLI: https://github.com/aws/aws-sam-cli/ + +1. Ensure Docker is installed and running: https://www.docker.com/ + +1. Build the project: + + ```sh + sam build + ``` + +1. Invoke the AWS Lambda Function locally with: + + ```sh + sam local invoke ExampleFunction + ``` + + This example connects to https://www.example.com and outputs the page's title as the function result. See the source code in [`app.mjs`](app.mjs) for more details. diff --git a/examples/aws-sam/functions/exampleFunction/app.mjs b/examples/aws-sam/functions/exampleFunction/app.mjs new file mode 100644 index 0000000..de94ba7 --- /dev/null +++ b/examples/aws-sam/functions/exampleFunction/app.mjs @@ -0,0 +1,24 @@ +import chromium from '@sparticuz/chromium'; +import puppeteer from 'puppeteer-core'; + +export const lambdaHandler = async (event, context) => { + const browser = await puppeteer.launch({ + args: chromium.args, + defaultViewport: chromium.defaultViewport, + executablePath: await chromium.executablePath(), + headless: chromium.headless, + }); + + const page = await browser.newPage(); + + await page.goto("https://www.example.com", { waitUntil: "networkidle0" }); + + const browserVersion = await browser.version(); + const pageTitle = await page.title(); + + await page.close(); + + await browser.close(); + + return { result: 'success', browserVersion, pageTitle }; +} diff --git a/examples/aws-sam/functions/exampleFunction/package.json b/examples/aws-sam/functions/exampleFunction/package.json new file mode 100644 index 0000000..8157b06 --- /dev/null +++ b/examples/aws-sam/functions/exampleFunction/package.json @@ -0,0 +1,13 @@ +{ + "name": "ExampleFunction", + "private": true, + "version": "0.1.0", + "description": "AWS Lambda Function that loads Chromium", + "main": "app.mjs", + "devDependencies": { + "@sparticuz/chromium": "^118.0.0" + }, + "dependencies": { + "puppeteer-core": "^21.4.0" + } +} diff --git a/examples/aws-sam/layers/chromium/package.json b/examples/aws-sam/layers/chromium/package.json new file mode 100644 index 0000000..942a69e --- /dev/null +++ b/examples/aws-sam/layers/chromium/package.json @@ -0,0 +1,9 @@ +{ + "name": "ChromiumLayer", + "private": true, + "version": "1.0.0", + "description": "Chromium layer for AWS Lambda", + "dependencies": { + "@sparticuz/chromium": "^118.0.0" + } +} diff --git a/examples/aws-sam/template.yml b/examples/aws-sam/template.yml new file mode 100644 index 0000000..b173b4b --- /dev/null +++ b/examples/aws-sam/template.yml @@ -0,0 +1,33 @@ +AWSTemplateFormatVersion: "2010-09-09" +Transform: AWS::Serverless-2016-10-31 +Description: Example configuration for AWS SAM and Chromium + +Resources: + ChromiumLayer: + Type: AWS::Serverless::LayerVersion + Properties: + Description: Chromium with Node.js integration for AWS Lambda + ContentUri: layers/chromium + CompatibleRuntimes: + - &nodejsRuntime nodejs18.x + # Chromium doesn't currently have ARM support; see https://github.com/Sparticuz/chromium#can-i-use-arm-or-graviton-instances + CompatibleArchitectures: + - &chromiumArch x86_64 + RetentionPolicy: Delete + Metadata: + BuildMethod: *nodejsRuntime + BuildArchitecture: *chromiumArch + + ExampleFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: functions/exampleFunction + Handler: app.lambdaHandler + Runtime: *nodejsRuntime + Architectures: + - *chromiumArch + Layers: + - !Ref ChromiumLayer + # Adjust as necessary + Timeout: 30 + MemorySize: 1024