Chromium Binary for AWS Lambda and Google Cloud Functions
npm install chrome-aws-lambda --save-prod
This will ship with appropriate binary for the latest stable release of puppeteer (usually updated within a few days).
You also need to install the corresponding version of puppeteer-core (or puppeteer):
npm install puppeteer-core --save-prod
If you wish to install an older version of Chromium, take a look at Versioning.
This package works with all the currently supported AWS Lambda Node.js runtimes out of the box.
const chromium = require('chrome-aws-lambda');
exports.handler = async (event, context, callback) => {
let result = null;
let browser = null;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true,
});
let page = await browser.newPage();
await page.goto(event.url || 'https://example.com');
result = await page.title();
} catch (error) {
return callback(error);
} finally {
if (browser !== null) {
await browser.close();
}
}
return callback(null, result);
};
const chromium = require('chrome-aws-lambda');
const playwright = require('playwright-core');
(async () => {
const browser = await playwright.chromium.launch({
args: chromium.args,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
// ...
await browser.close();
})();
You should allocate at least 512 MB of RAM to your Lambda, however 1600 MB (or more) is recommended.
Please refer to the Local Development Wiki page for instructions and troubleshooting.
| Method / Property | Returns | Description |
|---|---|---|
font(url) |
{?Promise<string>} |
Provisions a custom font and returns its basename. |
args |
{!Array<string>} |
Provides a list of recommended additional Chromium flags. |
defaultViewport |
{!Object} |
Returns more sensible default viewport settings. |
executablePath |
{?Promise<string>} |
Returns the path the Chromium binary was extracted to. |
headless |
{!boolean} |
Returns true if we are running on AWS Lambda or GCF. |
puppeteer |
{!Object} |
Overloads puppeteer and returns the resolved package. |
The Amazon Linux 2 AWS Lambda runtime is no longer provisioned with any font faces.
Because of this, this package ships with Open Sans, which supports the following scripts:
To provision additional fonts, simply call the font() method with an absolute path or URL:
await chromium.font('/var/task/fonts/NotoColorEmoji.ttf');
// or
await chromium.font('https://raw.githack.com/googlei18n/noto-emoji/master/fonts/NotoColorEmoji.ttf');
Noto Color Emoji(or similar) is needed if you want to render emojis.For URLs, it's recommended that you use a CDN, like raw.githack.com or gitcdn.xyz.
This method should be invoked before launching Chromium.
On non-serverless environments, the
font()method is a no-op to avoid polluting the user space.
Alternatively, it's also possible to provision fonts via AWS Lambda Layers.
Simply create a directory named .fonts and place any font faces you want there:
.fonts
├── NotoColorEmoji.ttf
└── Roboto.ttf
Afterwards, you just need to ZIP the directory and upload it as a AWS Lambda Layer:
zip -9 --filesync --move --recurse-paths .fonts.zip .fonts/
Since version 8.0.0, it's possible to overload puppeteer with the following convenient API:
interface Browser {
defaultPage(...hooks: ((page: Page) => Promise<Page>)[])
newPage(...hooks: ((page: Page) => Promise<Page>)[])
}
interface BrowserContext {
defaultPage(...hooks: ((page: Page) => Promise<Page>)[])
newPage(...hooks: ((page: Page) => Promise<Page>)[])
}
interface Page {
block(patterns: string[])
clear(selector: string)
clickAndWaitForNavigation(selector: string, options?: WaitForOptions)
clickAndWaitForRequest(selector: string, predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForRequest(selector: string, predicate: ((request: HTTPRequest) => boolean | Promise<boolean>), options?: WaitTimeoutOptions)
clickAndWaitForResponse(selector: string, predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForResponse(selector: string, predicate: ((request: HTTPResponse) => boolean | Promise<boolean>), options?: WaitTimeoutOptions)
count(selector: string)
exists(selector: string)
fillFormByLabel(selector: string, data: Record<string, boolean | string | string[]>)
fillFormByName(selector: string, data: Record<string, boolean | string | string[]>)
fillFormBySelector(selector: string, data: Record<string, boolean | string | string[]>)
fillFormByXPath(selector: string, data: Record<string, boolean | string | string[]>)
number(selector: string, decimal?: string, property?: string)
selectByLabel(selector: string, ...values: string[])
string(selector: string, property?: string)
waitForInflightRequests(requests?: number, alpha: number, omega: number, options?: WaitTimeoutOptions)
waitForText(predicate: string, options?: WaitTimeoutOptions)
waitUntilVisible(selector: string, options?: WaitTimeoutOptions)
waitWhileVisible(selector: string, options?: WaitTimeoutOptions)
withTracing(options: TracingOptions, callback: (page: Page) => Promise<any>)
}
interface Frame {
clear(selector: string)
clickAndWaitForNavigation(selector: string, options?: WaitForOptions)
clickAndWaitForRequest(selector: string, predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForRequest(selector: string, predicate: ((request: HTTPRequest) => boolean | Promise<boolean>), options?: WaitTimeoutOptions)
clickAndWaitForResponse(selector: string, predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForResponse(selector: string, predicate: ((request: HTTPResponse) => boolean | Promise<boolean>), options?: WaitTimeoutOptions)
count(selector: string)
exists(selector: string)
fillFormByLabel(selector: string, data: Record<string, boolean | string | string[]>)
fillFormByName(selector: string, data: Record<string, boolean | string | string[]>)
fillFormBySelector(selector: string, data: Record<string, boolean | string | string[]>)
fillFormByXPath(selector: string, data: Record<string, boolean | string | string[]>)
number(selector: string, decimal?: string, property?: string)
selectByLabel(selector: string, ...values: string[])
string(selector: string, property?: string)
waitForText(predicate: string, options?: WaitTimeoutOptions)
waitUntilVisible(selector: string, options?: WaitTimeoutOptions)
waitWhileVisible(selector: string, options?: WaitTimeoutOptions)
}
interface ElementHandle {
clear()
clickAndWaitForNavigation(options?: WaitForOptions)
clickAndWaitForRequest(predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForRequest(predicate: ((request: HTTPRequest) => boolean | Promise<boolean>), options?: WaitTimeoutOptions)
clickAndWaitForResponse(predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForResponse(predicate: ((request: HTTPResponse) => boolean | Promise<boolean>), options?: WaitTimeoutOptions)
fillFormByLabel(data: Record<string, boolean | string | string[]>)
fillFormByName(data: Record<string, boolean | string | string[]>)
fillFormBySelector(data: Record<string, boolean | string | string[]>)
fillFormByXPath(data: Record<string, boolean | string | string[]>)
getInnerHTML()
getInnerText()
number(decimal?: string, property?: string)
selectByLabel(...values: string[])
string(property?: string)
}
To enable this behavior, simply call the puppeteer property exposed by this package.
Refer to the TypeScript typings for general documentation.
When overloaded, you can specify a list of hooks to automatically apply to pages.
For instance, to remove the Headless substring from the user agent:
async function replaceUserAgent(page: Page): Promise<Page> {
let value = await page.browser().userAgent();
if (value.includes('Headless') === true) {
await page.setUserAgent(value.replace('Headless', ''));
}
return page;
}
And then simply pass that page hook to defaultPage() or newPage():
let page = await browser.defaultPage(replaceUserAgent);
Additional bundled page hooks can be found on
/build/hooks.
This package is versioned based on the underlying puppeteer minor version:
puppeteer Version |
chrome-aws-lambda Version |
Chromium Revision |
|---|---|---|
10.1.* |
npm i chrome-aws-lambda@~10.1.0 |
884014 (92.0.4512.0) |
10.0.* |
npm i chrome-aws-lambda@~10.0.0 |
884014 (92.0.4512.0) |
9.1.* |
npm i chrome-aws-lambda@~9.1.0 |
869685 (91.0.4469.0) |
9.0.* |
npm i chrome-aws-lambda@~9.0.0 |
869685 (91.0.4469.0) |
8.0.* |
npm i chrome-aws-lambda@~8.0.2 |
856583 (90.0.4427.0) |
7.0.* |
npm i chrome-aws-lambda@~7.0.0 |
848005 (90.0.4403.0) |
6.0.* |
npm i chrome-aws-lambda@~6.0.0 |
843427 (89.0.4389.0) |
5.5.* |
npm i chrome-aws-lambda@~5.5.0 |
818858 (88.0.4298.0) |
5.4.* |
npm i chrome-aws-lambda@~5.4.0 |
809590 (87.0.4272.0) |
5.3.* |
npm i chrome-aws-lambda@~5.3.1 |
800071 (86.0.4240.0) |
5.2.* |
npm i chrome-aws-lambda@~5.2.1 |
782078 (85.0.4182.0) |
5.1.* |
npm i chrome-aws-lambda@~5.1.0 |
768783 (84.0.4147.0) |
5.0.* |
npm i chrome-aws-lambda@~5.0.0 |
756035 (83.0.4103.0) |
3.1.* |
npm i chrome-aws-lambda@~3.1.1 |
756035 (83.0.4103.0) |
3.0.* |
npm i chrome-aws-lambda@~3.0.4 |
737027 (81.0.4044.0) |
2.1.* |
npm i chrome-aws-lambda@~2.1.1 |
722234 (80.0.3987.0) |
2.0.* |
npm i chrome-aws-lambda@~2.0.2 |
705776 (79.0.3945.0) |
1.20.* |
npm i chrome-aws-lambda@~1.20.4 |
686378 (78.0.3882.0) |
1.19.* |
npm i chrome-aws-lambda@~1.19.0 |
674921 (77.0.3844.0) |
1.18.* |
npm i chrome-aws-lambda@~1.18.1 |
672088 (77.0.3835.0) |
1.18.* |
npm i chrome-aws-lambda@~1.18.0 |
669486 (77.0.3827.0) |
1.17.* |
npm i chrome-aws-lambda@~1.17.1 |
662092 (76.0.3803.0) |
1.16.* |
npm i chrome-aws-lambda@~1.16.1 |
656675 (76.0.3786.0) |
1.15.* |
npm i chrome-aws-lambda@~1.15.1 |
650583 (75.0.3765.0) |
1.14.* |
npm i chrome-aws-lambda@~1.14.0 |
641577 (75.0.3738.0) |
1.13.* |
npm i chrome-aws-lambda@~1.13.0 |
637110 (74.0.3723.0) |
| `1. |
$ claude mcp add chrome-aws-lambda \
-- python -m otcore.mcp_server <graph>