Frontend With HasanAbout Me

Running Playwright as a Docker Container in AWS Lambda.

Playwright

Playwright has quickly become a go-to tool for end-to-end testing and browser automation. The Playwright team includes developers from Google's Puppeteer project and anyone who has worked with both can clearly see that it takes inspiration from Puppeteer but instead of copying it they improved upon it.

Understanding Serverless and AWS Lambdas

Serverless is definitely a buzzword in the tech industry, but there are places where it shines. Autoscaling can be hard to get right, but with serverless, all that complexity is handled by the cloud provider. All one has to do is write your specific code in the language of your choice and deploy it on the cloud where it will be run on demand.

Like with most of the internet, AWS leads the way in serverless with its Lambda service.

The Challenge with Playwright on Serverless Environments

So how do we go about running Playwright on AWS Lambda? If it was as simple as running npm install I would not be writing this article. The problem comes from the fact that Lambdas runtime environment is a very bare bones linux distribution, it is like to this have minimal cold-starts and fast execution times. But this also means that it lacks many of the libraries that Playwright and Chrome needs to run.

There are couple of solutions to this problem, one is to provide all the libraries that AWS Linux distribution is missing and provide a custom implementation of browser that can run in a serverless environment. Similar to packages such as this: chrome-aws-lambda. Or run on your own container directly in AWS Lambda.

Migration to Dockerized Playwright

We at sematext had been using Puppeteer with the above mentioned package for some time now. But the new features and improvements in Playwright and similarity in syntax with Puppeteer really convinced us to make the switch.

We had been burned by using third party chromium packages and things could easily break with one security patch from AWS. So we decided to have our own containers running only official Playwright images. This guarantees a stable runtime environment and kept us immune from changes or problems in the third party packages.

Setting Up Docker for AWS Lambdas

Thankfully the good people at Microsoft provide a Playwright Docker image

# Use the Playwright base image
FROM mcr.microsoft.com/playwright:v1.44.1-focal-amd64

RUN apt-get update && apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev \
autoconf \
libtool

ENV NPM_CONFIG_CACHE=/tmp/.npm

# To cache the npm install step
RUN npm install -g aws-lambda-ric

COPY package.json ./

# Install the application dependencies
RUN npm install

# Copy the application code to the container
COPY . .

RUN npm run lint

RUN npm run build

ENTRYPOINT ["/bin/npx", "aws-lambda-ric"]

# Set the command to run your application
CMD ["dist/index.handler"]

Deploying to AWS Lambda

  1. Build the Docker image.
docker build -t your-image-name .
docker tag your-image-name:latest your-dockerhub-username/your-image-name:latest
  1. Push Image to Docker Hub
docker login
docker push your-dockerhub-username/your-image-name:latest
  1. Deploy to AWS Lambda

Use AWS CLI or the AWS console to create a Lambda function with your container image. Ensure the image is in the same region as your Lambda function.

Testing and Optimization

Test your setup through the AWS Lambda console, configure your function, and trigger it. Advanced users can leverage AWS CLI for streamlined deployments.

By following these steps, you can effectively run Playwright within AWS Lambdas, harnessing the power of serverless architecture. For further optimizations, refer to the Playwright documentation.