Skip to content

Commit

Permalink
Merge pull request #178 from davidjb/aws-sam-example
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparticuz authored Nov 13, 2023
2 parents 8a39891 + e77f679 commit 8fdbb07
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
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

0 comments on commit 8fdbb07

Please sign in to comment.