Chrome AWS Lambda with Puppeteer on Amplify
In this article
This guide shows you how to run headless Chrome (via Puppeteer) inside an AWS Lambda function and deploy it through AWS Amplify. This lets you do server-side web scraping, generate PDFs, take screenshots, or render JavaScript-heavy pages without maintaining your own servers. You'll need an AWS account, the Amplify CLI installed, and Node.js on your local machine.
What You'll Need
- AWS account with permissions to create Lambda functions and Amplify apps
- AWS Amplify CLI installed (
npm install -g @aws-amplify/cli) - Node.js 14 or later on your local machine
- Basic familiarity with the command line
Step-by-Step
- Initialize your Amplify project – In your project directory, run
amplify init. Follow the prompts to create a new Amplify app. Choose your default editor and AWS profile. - Add a Lambda function – Run
amplify add function. Choose a name (e.g.,puppeteerFunction), select the runtime Node.js, and pick the Hello World template. When asked about advanced settings, choose Yes to configure the function later. - Install Puppeteer and Chromium – In your function's source directory (likely
amplify/backend/function/puppeteerFunction/src), runnpm install puppeteer-coreandnpm install @sparticuz/chromium. These packages work well in Lambda's environment. - Write the Lambda handler – Open
index.jsin the function's src folder. Replace the default code with a simple Puppeteer script that launches Chromium, navigates to a URL, and returns the page title. Use@sparticuz/chromiumto get the binary path and set the executable path in Puppeteer. - Set environment variables – In the function's
parameters.jsonor through the Amplify console, add an environment variable likeCHROME_PATHpointing to the Chromium binary path returned by@sparticuz/chromium. Alternatively, hardcode it in the code usingchromium.executablePath(). - Deploy the function – Run
amplify pushto deploy the function to AWS. This will create the Lambda function and any required IAM roles. - Test the function locally (optional) – Use
amplify function invoketo test the function with a sample event. This helps catch errors before deploying. - Add an API endpoint (optional) – If you want to trigger the function via HTTP, run
amplify add api, choose REST, and connect it to your Lambda function. This creates an API Gateway endpoint. - Deploy the API – Run
amplify pushagain to deploy the API. Note the endpoint URL provided. - Test the deployed function – Use a tool like curl or Postman to send a request to the API endpoint. The function should return the expected output.
Common Mistakes
- Using the regular
puppeteerpackage instead ofpuppeteer-core. The full Puppeteer package downloads its own Chromium, which is too large for Lambda's 250 MB limit. - Not setting the correct executable path. Puppeteer needs to know where Chromium is installed; using
@sparticuz/chromiumprovides that path dynamically. - Ignoring Lambda's memory and timeout limits. Chromium needs at least 512 MB of memory and a timeout of at least 10 seconds. Set these in the function's configuration.
- Forgetting to include the
chromiumbinary in the deployment. The@sparticuz/chromiumpackage handles this, but only if you install it in the function's src directory.
Tips
- Set the Lambda timeout to 30 seconds and memory to 1024 MB for reliable performance.
- Use
chromium.argsfrom@sparticuz/chromiumto pass recommended flags like--no-sandboxand--disable-setuid-sandbox. - Keep your function code in the Amplify function directory so
amplify pushbundles it correctly. - If you need to scrape many pages, consider using a queue or step functions to avoid timeouts.
Troubleshooting
- Function times out – Increase the timeout in the Lambda console (up to 15 minutes) and ensure your code exits cleanly. Also check that you're not waiting for unnecessary network requests.
- Error: "spawn chromium ENOENT" – This means Puppeteer can't find the Chromium binary. Make sure you're using
@sparticuz/chromiumand setting the executable path correctly. - Error: "Failed to launch the browser process" – Often due to missing shared libraries. The
@sparticuz/chromiumpackage includes a prebuilt binary that works in Lambda, so this usually happens if you're using a different version or not using the package properly. - Function returns empty response – Check the CloudWatch logs for your Lambda function. Common causes: not awaiting the browser launch, or the page navigation failing silently.
- Deployment fails with "Lambda function not found" – Make sure you've run
amplify pushafter adding the function, and that the function name matches.
Frequently Asked Questions
Can I use the regular Puppeteer package instead of puppeteer-core?
No, the full Puppeteer package downloads Chromium during installation, which exceeds Lambda's size limit. Use puppeteer-core with @sparticuz/chromium to keep the deployment small.
How do I pass parameters to the Lambda function, like a URL to scrape?
Use the event object. In your API Gateway setup, the event contains the query string or body. In your handler, read event.queryStringParameters.url or parse event.body.
Can I take screenshots with this setup?
Yes, you can use Puppeteer's page.screenshot() method and return the image as a base64-encoded string in the response.
How do I increase the memory and timeout for my Lambda function?
In the Amplify console, go to your function's configuration, or edit the amplify/backend/function/ file, and set memorySize and timeout values.
Is it possible to use this with a React app deployed on Amplify?
Yes, the Lambda function is separate from your frontend. You can call it from your React app using fetch or axios.
What are the costs of running this on Lambda?
AWS Lambda charges based on requests and compute time. Running Chromium uses more memory and time, so costs are higher than simple functions, but still low for moderate usage.