User Agents

Web browsers and everyday web tech, explained

Chrome AWS Lambda with Puppeteer on Amplify

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.
  2. 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.
  3. Install Puppeteer and Chromium – In your function's source directory (likely amplify/backend/function/puppeteerFunction/src), run npm install puppeteer-core and npm install @sparticuz/chromium. These packages work well in Lambda's environment.
  4. Write the Lambda handler – Open index.js in 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/chromium to get the binary path and set the executable path in Puppeteer.
  5. Set environment variables – In the function's parameters.json or through the Amplify console, add an environment variable like CHROME_PATH pointing to the Chromium binary path returned by @sparticuz/chromium. Alternatively, hardcode it in the code using chromium.executablePath().
  6. Deploy the function – Run amplify push to deploy the function to AWS. This will create the Lambda function and any required IAM roles.
  7. Test the function locally (optional) – Use amplify function invoke to test the function with a sample event. This helps catch errors before deploying.
  8. 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.
  9. Deploy the API – Run amplify push again to deploy the API. Note the endpoint URL provided.
  10. 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 puppeteer package instead of puppeteer-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/chromium provides 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 chromium binary in the deployment. The @sparticuz/chromium package 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.args from @sparticuz/chromium to pass recommended flags like --no-sandbox and --disable-setuid-sandbox.
  • Keep your function code in the Amplify function directory so amplify push bundles 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/chromium and setting the executable path correctly.
  • Error: "Failed to launch the browser process" – Often due to missing shared libraries. The @sparticuz/chromium package 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 push after 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//function-parameters.json 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.