[英]Chrome --headless for AWS Lambda?
Chrome with headless mode already available for linux: https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md 具有无头模式的Chrome已经可用于linux: https : //chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
It works only with Canary right now, but it coming official in Chrome 57. 它现在只适用于Canary,但它正在Chrome 57中正式发布。
Any chances to run Google Chrome on aws lambda? 有没有机会在aws lambda上运行Google Chrome?
Yes; 是; it's possible.
这是可能的。
Compiling a non-debug build of Headless Chrome yields a binary that's ~125 MB, and just under 44 MB when gzipped. 编译Headless Chrome的非调试版本会生成~125 MB的二进制文件,并且在gzip压缩时不到44 MB。 This means it fits within the 250 MB uncompressed and 50 MB size limitation for the function's deployment package.
这意味着它适合功能部署包的250 MB未压缩和50 MB大小限制 。
What's (currently) required is to force Chrome to compile without using shared memory at /dev/shm. (目前)需要强制Chrome编译而不使用/ dev / shm中的共享内存。 Theres a thread on the topic on the headless-dev google group here .
即使世界上的话题线程无头-dev的谷歌组在这里 。
Here are steps I've used to build a binary of headless Chrome that will work on AWS Lambda. 以下是我用于构建可在AWS Lambda上运行的无头Chrome二进制文件的步骤。 They're based on this and this .
他们是基于这个和这个 。
sudo printf "LANG=en_US.utf-8\nLC_ALL=en_US.utf-8" >> /etc/environment
sudo yum install -y git redhat-lsb python bzip2 tar pkgconfig atk-devel alsa-lib-devel bison binutils brlapi-devel bluez-libs-devel bzip2-devel cairo-devel cups-devel dbus-devel dbus-glib-devel expat-devel fontconfig-devel freetype-devel gcc-c++ GConf2-devel glib2-devel glibc.i686 gperf glib2-devel gtk2-devel gtk3-devel java-1.*.0-openjdk-devel libatomic libcap-devel libffi-devel libgcc.i686 libgnome-keyring-devel libjpeg-devel libstdc++.i686 libX11-devel libXScrnSaver-devel libXtst-devel libxkbcommon-x11-devel ncurses-compat-libs nspr-devel nss-devel pam-devel pango-devel pciutils-devel pulseaudio-libs-devel zlib.i686 httpd mod_ssl php php-cli python-psutil wdiff --enablerepo=epel
Yum will complain about some packages not existing. 百胜会抱怨一些不存在的包裹。 Whatever.
随你。 I haven't looked into them.
我没有调查过他们。 Didn't seem to stop me from building headless_shell, though.
但是,似乎没有阻止我构建headless_shell。 Ignore whiney little Yum and move on.
忽略那个小小的百胜,然后继续前进。 Next:
下一个:
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
echo "export PATH=$PATH:$HOME/depot_tools" >> ~/.bash_profile
source ~/.bash_profile
mkdir Chromium && cd Chromium
fetch --no-history chromium
cd src
At this point we need to make a very small change to the Chrome code. 此时,我们需要对Chrome代码进行非常小的更改。 By default on Linux, Chrome assumes there to be a tmpfs at
/dev/shm
. 默认情况下,在Linux上,Chrome假设在
/dev/shm
存在tmpfs。 There is no tmpfs available to a Lambda function. Lambda函数没有可用的tmpfs。 :-(
:-(
The file we have to change is src/base/files/file_util_posix.cc
. 我们要更改的文件是
src/base/files/file_util_posix.cc
。 Modify GetShmemTempDir()
such that it always returns the OSs temp dir ( /tmp
). 修改
GetShmemTempDir()
,使其始终返回OS temp dir( /tmp
)。 A simple way to do this is to just remove the entire #if defined(OS_LINUX)
block in the GetShmemTempDir()
function. 一种简单的方法是只删除
GetShmemTempDir()
函数中的整个#if defined(OS_LINUX)
块。 A less drastic change is to hardcode use_dev_shm
to false
: 一个不太激烈的变化是将
use_dev_shm
硬编码为false
:
bool GetShmemTempDir(bool executable, FilePath* path) {
#if defined(OS_LINUX)
bool use_dev_shm = true;
if (executable) {
static const bool s_dev_shm_executable = DetermineDevShmExecutable();
use_dev_shm = s_dev_shm_executable;
}
// cuz lambda
use_dev_shm = false; // <-- add this. Yes it's pretty hack-y
if (use_dev_shm) {
*path = FilePath("/dev/shm");
return true;
}
#endif
return GetTempDir(path);
}
With that change, it's time to compile. 有了这个改变,就该编译了。 Picking things back up in the
src
directory, set some compile arguments and then (the last command) start the build process. 在
src
目录中备份东西,设置一些编译参数,然后(最后一个命令)启动构建过程。
mkdir -p out/Headless
echo 'import("//build/args/headless.gn")' > out/Headless/args.gn
echo 'is_debug = false' >> out/Headless/args.gn
echo 'symbol_level = 0' >> out/Headless/args.gn
echo 'is_component_build = false' >> out/Headless/args.gn
echo 'remove_webcore_debug_symbols = true' >> out/Headless/args.gn
echo 'enable_nacl = false' >> out/Headless/args.gn
gn gen out/Headless
ninja -C out/Headless headless_shell
Finally we make a tarball of the relevant file(s) we'll need to run in Lambda. 最后,我们制作了一个我们需要在Lambda中运行的相关文件的tarball。
mkdir out/headless-chrome && cd out
cp Headless/headless_shell Headless/libosmesa.so headless-chrome/
tar -zcvf chrome-headless-lambda-linux-x64.tar.gz headless-chrome/
Within Lambda, run headless_shell
with the remote debugger interface enabled by executing: 在Lambda中,通过执行以下命令运行
headless_shell
并启用远程调试器接口:
/path/to/headless_shell --disable-gpu --no-sandbox --remote-debugging-port=9222 --user-data-dir=/tmp/user-data --single-process --data-path=/tmp/data-path --homedir=/tmp --disk-cache-dir=/tmp/cache-dir
Since /tmp is the only writeable place in a Lambda function, there are a bunch of flags just telling Chrome where to dump it's data. 由于/ tmp是Lambda函数中唯一可写的位置,因此有一堆标志只是告诉Chrome在哪里转储它的数据。 They're not necessary but it keeps Chrome happy.
它们不是必需的,但它让Chrome很开心。 Note also that it's been mentioned that with the
--disable-gpu
flag, we don't need libosmesa.so
, the omission of which would shave off about 4 MB from our package zip. 还要注意,有人提到使用
--disable-gpu
标志,我们不需要libosmesa.so
,遗漏的东西会从我们的包zip中libosmesa.so
大约4 MB。
I've started this project with the aim of making it easier to get started. 我开始这个项目的目的是让它更容易上手。 It comes with a pre-built headless Chrome binary which you can get here .
它配备了预制的无头Chrome二进制文件,您可以在此处获取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.