AWS Lambda容器映像中使用Puppeteer
在Lambda中运行Puppeteer可以做网页截图、网页录屏等操作。但是在Lambda中跑起Puppeteer并不容易,选对浏览器,设置对浏览器启动参数,才能在Lambda中跑起Puppeteer

AWS Lambda是AWS推出的无服务器计算服务,用户无需管理服务器,可以更专注自己业务。Lambda支持Nodejs、Python、Golang、Java等多种语言。Lambda是按量付费,支持最多同时运行2000个Lambda函数。非常适用一些瞬时并发较高切对服务器性能要求较高的服务。我们在项目中有使用Lambda部署视频转码服务、网页截图、网页录屏等等10多个服务。

在Lambda中运行Puppeteer可以做网页截图、网页录屏等操作。但是在Lambda中跑起Puppeteer并不容易,选对浏览器,设置对浏览器启动参数,才能在Lambda中跑起Puppeteer。

浏览器

由于chromium是不带视频解码器的,如果使用chrominum打开带有视频的网页,会出现黑屏的情况。所以在Lambda中不建议使用chromiun浏览器。

官方的chrome也不行,虽然使用docker启动容器测试都能正常工作,但是发布到Lambda上后,发现浏览器会Hang主,Lambda环境跟Docker环境还是有一定差距的。

推荐使用在这里下载浏览器:Chromium

这里直接附上我的安装的三方编译的chrome的Lambda,大家可以用它做基础镜像。

# Define custom function directory
ARG FUNCTION_DIR="/function"

# Grab a fresh slim copy of the image to reduce the final size
FROM node:14-buster AS builder 

# Include global arg in this stage of the build
ARG FUNCTION_DIR

ENV NODE_PATH=/${FUNCTION_DIR}/node_modules

WORKDIR ${FUNCTION_DIR}

RUN sed -i 's#http://deb.debian.org#http://mirrors.aliyun.com#g' /etc/apt/sources.list

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

RUN npm i aws-lambda-ric

# Grab a fresh slim copy of the image to reduce the final size
FROM node:14-buster-slim

# Include global arg in this stage of the build
ARG FUNCTION_DIR
ARG SOFTWARE_DIR="/tmp/software/"

RUN sed -i 's#http://deb.debian.org#http://mirrors.aliyun.com#g' /etc/apt/sources.list
RUN apt-get update && \
    apt-get install -y --no-install-recommends build-essential libexpat1-dev libjpeg-dev libpng-dev \
    libwebp-dev libgif-dev libexif-dev ca-certificates fonts-liberation \
    libappindicator3-1 libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 \
    libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 \
    libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 \ 
    libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget xdg-utils libxshmfence-dev \
    fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-indic unifont ssh git && \
    rm -r /var/lib/apt/lists/* && apt-get clean

RUN mkdir -p ${SOFTWARE_DIR}

RUN cd ${SOFTWARE_DIR} && \
    wget https://github.com/macchrome/linchrome/releases/download/v104.0.5112.101-r1012729-portable-ungoogled-Lin64/ungoogled-chromium_104.0.5112.101_1.vaapi_linux.tar.xz && \
    tar -xvf ungoogled-chromium_104.0.5112.101_1.vaapi_linux.tar.xz && \ 
    mv ungoogled-chromium_104.0.5112.101_1.vaapi_linux /opt/google/

RUN rm -rf ${SOFTWARE_DIR} 

ENV NODE_PATH=/${FUNCTION_DIR}/node_modules

# Copy in the built dependencies
COPY --from=builder ${FUNCTION_DIR} ${FUNCTION_DIR}

Puppeteer

环境搭好了并不意味着一定成功,如果puppeteer启动参数配置错误也可能导致浏览器无法启动。

const browser = await puppeteer.launch({
      args: [
        '--autoplay-policy=user-gesture-required',
        '--disable-background-networking',
        '--disable-background-timer-throttling',
        '--disable-backgrounding-occluded-windows',
        '--disable-breakpad',
        '--disable-client-side-phishing-detection',
        '--disable-component-update',
        '--disable-default-apps',
        '--disable-dev-shm-usage',
        '--disable-domain-reliability',
        '--disable-extensions',
        '--disable-features=AudioServiceOutOfProcess,IsolateOrigins,site-per-process',
        '--disable-hang-monitor',
        '--disable-ipc-flooding-protection',
        '--disable-offer-store-unmasked-wallet-cards',
        '--disable-popup-blocking',
        '--disable-print-preview',
        '--disable-prompt-on-repost',
        '--disable-renderer-backgrounding',
        '--disable-setuid-sandbox',
        '--disable-speech-api',
        '--disable-sync',
        '--disable-web-security',
        '--disk-cache-size=33554432',
        '--hide-scrollbars',
        '--ignore-gpu-blocklist',
        '--metrics-recording-only',
        '--disable-audio-output',
        '--no-default-browser-check',
        '--no-first-run',
        '--no-pings',
        '--no-sandbox',
        '--no-zygote',
        '--password-store=basic',
     //   '--enable-webgl',
        '--disable-gpu',
        '--use-mock-keychain',
        '--window-size=1920,1080',
        '--single-process'
      ],
      executablePath: '/opt/google/chrome',
      defaultViewport: puppeteer.defaultViewport,
      headless: true,
      ignoreHTTPSErrors: true,
      userDataDir: '/tmp',
    })

浏览器、Puppeteer参数都正确的话,在Lambda中运行大概率是没有问题的。


最后修改于 2022-09-21

此篇文章的评论功能已经停用。