-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from davidjb/aws-sam-example
- Loading branch information
Showing
7 changed files
with
100 additions
and
1 deletion.
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
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 @@ | ||
.aws-sam |
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,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. |
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,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 }; | ||
} |
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,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" | ||
} | ||
} |
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,9 @@ | ||
{ | ||
"name": "ChromiumLayer", | ||
"private": true, | ||
"version": "1.0.0", | ||
"description": "Chromium layer for AWS Lambda", | ||
"dependencies": { | ||
"@sparticuz/chromium": "^118.0.0" | ||
} | ||
} |
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,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 |