AWS Lambda上的谷歌镀 chrome

pvabu6sv  于 2023-02-01  发布在  Go
关注(0)|答案(1)|浏览(148)

有可能运行谷歌 chrome 而不是 chrome 与 puppet 师在AWS λ与容器?
在浏览器中创建新页面时脚本卡住:

const page = await browser.newPage();

来自AWS lambda的日志:

mkdir: cannot create directory ‘/.local’: Read-only file system
touch: cannot touch ‘/.local/share/applications/mimeapps.list’: No such file or directory
/usr/bin/google-chrome-stable: line 45: /dev/fd/62: No such file or directory
/usr/bin/google-chrome-stable: line 46: /dev/fd/62: No such file or directory
[0213/000419.523205:ERROR:bus.cc(397)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0213/000419.528197:ERROR:bus.cc(397)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0213/000419.648505:WARNING:audio_manager_linux.cc(60)] Falling back to ALSA for audio output. PulseAudio is not available or could not be initialized.
DevTools listening on ws://127.0.0.1:46195/devtools/browser/1d348770-1c99-48a5-934c-fae5254fc766
[0213/000419.769218:WARNING:bluez_dbus_manager.cc(248)] Floss manager not present, cannot set Floss enable/disable.
prctl(PR_SET_NO_NEW_PRIVS) failed
prctl(PR_SET_NO_NEW_PRIVS) failed
rjzwgtxy

rjzwgtxy1#

我不使用 puppet 师,但这并不重要。

FROM public.ecr.aws/lambda/provided:al2
RUN yum install unzip atk at-spi2-atk gtk3 cups-libs pango libdrm \
    libXcomposite libXcursor libXdamage libXext libXtst libXt \
    libXrandr libXScrnSaver alsa-lib \
    xorg-x11-server-Xvfb wget shadow-utils  -y

COPY install-chrome.sh /tmp/

RUN /usr/bin/bash /tmp/install-chrome.sh

ENV DBUS_SESSION_BUS_ADDRESS="/dev/null"

我不是100% DBUS_SESSION_BUS_ADDRESS是必要的。我也不是100%确定是否明确命名所有这些软件包是必要的,我偷了一切从十几个不同的地方,可能 chrome rpm将拉在它需要的,但我从来没有使用过任何RHEL为基础的系统,所以我完全一无所知。我知道这是工程。优化是受欢迎的。
以下是脚本:

#!/usr/bin/bash

# Download and install chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
# Without -y it doesn't run because it needs to add dependencies.
yum install -y google-chrome-stable_current_x86_64.rpm
rm google-chrome-stable_current_x86_64.rpm

CHROMEVERSION=`wget -qO- https://chromedriver.storage.googleapis.com/LATEST_RELEASE`
wget --no-verbose -O /tmp/chromedriver_linux64.zip https://chromedriver.storage.googleapis.com/$CHROMEVERSION/chromedriver_linux64.zip
unzip /tmp/chromedriver_linux64.zip -d /opt
rm /tmp/chromedriver_linux64.zip
mv /opt/chromedriver /opt/chromedriver-$CHROMEVERSION
chmod 755 /opt/chromedriver-$CHROMEVERSION
ln -fs /opt/chromedriver-$CHROMEVERSION /usr/local/bin/chromedriver

# Create a user. /usr/sbin is not on $PATH.
/usr/sbin/groupadd --system chrome
/usr/sbin/useradd --system --create-home --gid chrome --groups audio,video chrome

您可以使用docker run --mount type=tmpfs,destination=/tmp --read-only在本地启动它来验证它是否正常工作,这很好地模拟了AWS Lambda的环境。然后您需要运行su chrome -c 'xvfb-run chromedriver --allowed-ips=127.0.0.1'。我使用的是https://github.com/instaclick/php-webdriver/,它是一个非常瘦的PHP客户端,用于W3C和Selenium 2 webdriver。我使用它来测试:

<?php

namespace WebDriver;

require 'vendor/autoload.php';

@mkdir('/tmp/chrome');
chmod('/tmp/chrome', 0777);
$wd_host = 'http://localhost:9515';
$web_driver = new WebDriver($wd_host);
$session = $web_driver->session('chrome', [['goog:chromeOptions' => ['args' => [
  '--no-sandbox',
  '--user-data-dir=/tmp/chrome'
]]]]);
$session->open('https://google.com');

相关问题