Merge pull request #178 from davidjb/aws-sam-example
This commit is contained in:
commit
8fdbb07701
|
|
@ -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 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)
|
- [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)
|
- [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)
|
- [Webpack](https://github.com/Sparticuz/chromium/issues/24#issuecomment-1343196897)
|
||||||
- [Netlify](https://github.com/Sparticuz/chromium/issues/24#issuecomment-1414107620)
|
- [Netlify](https://github.com/Sparticuz/chromium/issues/24#issuecomment-1414107620)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
.aws-sam
|
||||||
|
|
@ -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.
|
||||||
|
|
@ -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 };
|
||||||
|
}
|
||||||
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"name": "ChromiumLayer",
|
||||||
|
"private": true,
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Chromium layer for AWS Lambda",
|
||||||
|
"dependencies": {
|
||||||
|
"@sparticuz/chromium": "^118.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue