Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AWS SAM LayerVersion example #178

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions examples/aws-sam/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.aws-sam
19 changes: 19 additions & 0 deletions examples/aws-sam/README.md
Original file line number Diff line number Diff line change
@@ -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.
24 changes: 24 additions & 0 deletions examples/aws-sam/functions/exampleFunction/app.mjs
Original file line number Diff line number Diff line change
@@ -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 };
}
13 changes: 13 additions & 0 deletions examples/aws-sam/functions/exampleFunction/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 9 additions & 0 deletions examples/aws-sam/layers/chromium/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "ChromiumLayer",
"private": true,
"version": "1.0.0",
"description": "Chromium layer for AWS Lambda",
"dependencies": {
"@sparticuz/chromium": "^118.0.0"
}
}
33 changes: 33 additions & 0 deletions examples/aws-sam/template.yml
Original file line number Diff line number Diff line change
@@ -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