GithubHelp home page GithubHelp logo

george-lim / firefox-lambda Goto Github PK

View Code? Open in Web Editor NEW
7.0 6.0 1.0 14 KB

A Lambda container image that provides a Playwright-patched version of Firefox

Home Page: https://gallery.ecr.aws/george-lim/firefox-lambda

License: MIT License

Dockerfile 84.43% Shell 15.57%
linux firefox lambda docker-image playwright github-container-registry ecr-public xvfb headless-browser

firefox-lambda's Introduction

Firefox Lambda

releases ci license

Firefox Lambda is a Lambda container image that provides a Playwright-patched version of Firefox. The image is built for the Python 3.8 runtime, and allows Firefox to be run both headfully or headlessly.

Usage

Amazon ECR Public:         public.ecr.aws/george-lim/firefox-lambda
GitHub Container Registry: ghcr.io/george-lim/firefox-lambda

Firefox Lambda is published to Amazon ECR Public and GitHub Container Registry.

Supported tags

Tags are provided in the repository's releases.

Installation

In your Lambda function code, modify the executable path of the firefox.launch call to the environment variable FIREFOX_PATH. Then, COPY the code into the Firefox Lambda image as usual - that's it!

To optionally configure the screen size of Xvfb, set the environment variable XVFB_WHD in your Dockerfile. The default value is 1280x720x16.

Features

Playwright requires capabilities from Firefox that are not exposed natively without a build patch. Thus, Firefox needs to be specifically built and patched in Amazon Linux 2 in order to work with Playwright.

Firefox Lambda builds Firefox from the AWS base image for Lambda, without any optional Firefox dependencies. The final production image copies the /opt/firefox/ folder into a Python 3.8 Lambda container image and installs all necessary Firefox and Playwright dependencies.

Additionally, Xvfb is installed and configured in the production image so that Firefox can run headfully.

Examples

Start Playwright inside a Lambda function

These snippets show how to start Playwright inside a Lambda function.

Dockerfile

FROM public.ecr.aws/george-lim/firefox-lambda:1.1.0

# Optional
ENV XVFB_WHD=1280x720x16

COPY app.py $LAMBDA_TASK_ROOT
CMD ["app.handler"]

app.py

import os

from playwright.sync_api import sync_playwright


def run(playwright):
    firefox_path = os.environ.get("FIREFOX_PATH")
    browser = None

    try:
        browser = playwright.firefox.launch(
            executable_path=firefox_path, headless=False
        )

        page = browser.new_page()
        page.goto("https://www.mozilla.org")

        browser.close()
    except Exception:
        if browser:
            browser.close()

        raise


def handler(event, context):
    with sync_playwright() as playwright:
        run(playwright)

CI/CD

Local image building

Building Firefox Lambda during CI/CD takes too long. Thus, image building for CI/CD is done locally, and cached to ghcr.io/george-lim/firefox-lambda:latest-dev. The CD workflow will then build the image from cache, and push production tags to the respective registries.

docker login ghcr.io -u george-lim --password-stdin

docker buildx build \
    --tag ghcr.io/george-lim/firefox-lambda:latest-dev \
    --cache-to=type=registry,ref=ghcr.io/george-lim/firefox-lambda:latest-dev,mode=max \
    .

This will build Firefox Lambda and push the caches to GitHub Container Registry.

You will need to create a Personal Access Token on GitHub with write:packages and read:packages scopes in order to push the caches. Supply the Personal Access Token as the password when logging in.

Secrets

AWS_ACCESS_KEY_ID: AKIAIOSFODNN7EXAMPLE
AWS_SECRET_ACCESS_KEY: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

CR_PAT: "********"

These secrets must exist in the repository for the CD workflow to publish the image.

firefox-lambda's People

Contributors

george-lim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

laromicas

firefox-lambda's Issues

[pid=30][err] *** You are running in headless mode.

Hi, i having trouble running this in aws lambda, when i run it locally from the Dockerfile and tested locally works fine, but when make a ecr image to create a lambda from this and tested, i have the following error ->
Screenshot 2022-04-27 155012

my app.py only change the firefox path to /opt/firefox/firefox
and set headless=True

Run playwright on lambda with node

FROM public.ecr.aws/george-lim/firefox-lambda:1.0.1 as firefox

FROM public.ecr.aws/lambda/nodejs:12

COPY --from=firefox /opt/firefox /opt/firefox

RUN yum install -y
dbus-glib-0.100-7.2.amzn2
gtk3-3.22.30-3.amzn2
libXt-1.1.5-3.amzn2.0.2
&& yum clean all

COPY app.js package*.json ./

RUN npm install

ENV FIREFOX_BINARY_PATH=/opt/firefox/firefox

CMD [ "app.lambdaHandler" ]

I have built docker with above file and deployed lambda with this container.
But I have failed. Following is the lambda logs.

<launched> pid=23
[err] *** You are running in headless mode.
[err] ExceptionHandler::GenerateDump cloned child 34
[err] ExceptionHandler::SendContinueSignalToChild sent continue signal to child
[err] ExceptionHandler::WaitForContinueSignal waiting for continue signal...
=========================== logs ===========================
<launching> /opt/firefox/firefox -no-remote -headless -profile /tmp/playwright_firefoxdev_profile-BYvMgD -juggler-pipe -silent
<launched> pid=23
[err] *** You are running in headless mode.
[err] ExceptionHandler::GenerateDump cloned child 34
[err] ExceptionHandler::SendContinueSignalToChild sent continue signal to child
[err] ExceptionHandler::WaitForContinueSignal waiting for continue signal...
============================================================
Note: use DEBUG=pw:api environment variable and rerun to capture Playwright logs.
    at Connection.sendMessageToServer (/var/task/node_modules/playwright/lib/client/connection.js:69:15)
    at Proxy.<anonymous> (/var/task/node_modules/playwright/lib/client/channelOwner.js:44:61)
    at /var/task/node_modules/playwright/lib/client/browserType.js:61:73
    at BrowserType._wrapApiCall (/var/task/node_modules/playwright/lib/client/channelOwner.js:72:34)
    at BrowserType.launch (/var/task/node_modules/playwright/lib/client/browserType.js:52:21)
    at Runtime.exports.lambdaHandler [as handler] (/var/task/app1.js:6:39)
    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)
END RequestId: ec405a11-a666-42ff-a6e3-a87050e38716
REPORT RequestId: ec405a11-a666-42ff-a6e3-a87050e38716	Duration: 1636.06 ms	Billed Duration: 2771 ms	Memory Size: 512 MB	Max Memory Used: 166 MB	Init Duration: 1134.78 ms	```

What should I do for this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.