Running Puppeteer on AWS ECS Fargate or EC2
When you deploy a Puppeteer project to AWS ECS, you need to make sure that you initialize Puppeteer with the correct options. This is because in ECS it runs in a headless environment, and Puppeteer needs to be configured to run in headless mode. Because the application is running in a container, Puppeteer also needs to run without the sandbox.
Required Puppeteer Options
When you initialize Puppeteer, you need to launch it with the following options:
const browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox", "--disable-setuid-sandbox"],
})
The headless: true
option tells Puppeteer that it is running on a server, and should not launch a browser window.
The two arguments passed to Puppeteer tell it to run without a sandbox - for more, see the Puppeteer documentation. This configuration should only be used when you trust the content you are rendering through the headless browser.
Considerations for running Puppeteer
We suggest considering an architecture where you run Puppeteer in a separate, worker service. This isolates Puppeteer and the web browser runtime to a single service, and allows you to scale the number of workers independently of the rest of your application. This also reduces any security risks with running Puppeteer in the same service as your application.