无法在 AWS Lambda 容器中启动 Chromium,但可以在本地运行

Joz*_*ipa 12 docker aws-lambda google-chrome-headless puppeteer

我正在尝试使用 Docker 容器在 AWS Lambda 中设置无头 Chromium 浏览器。我的 Dockerfile 看起来像这样

# Build stage
# ------------
FROM public.ecr.aws/lambda/nodejs:14 AS builder

RUN yum install -y make

# Install dependencies
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true"
WORKDIR /usr/src
COPY package*.json .
COPY tsconfig*.json .
RUN npm ci

# build Typescript
COPY . /usr/src
RUN make compile

# Production stage
# ------------
FROM public.ecr.aws/lambda/nodejs:14

# Install Chrome
COPY docker/google-chrome.repo /etc/yum.repos.d/google-chrome.repo
RUN yum update -y && yum install google-chrome-stable -y

# /var/task is where Lambda looks for files by default
COPY package*.json /var/task/

# Install production dependencies only
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true"
RUN npm install --production

# Copy built files from the previous stage
COPY --from=builder /usr/src/dist /var/task/

CMD ["app.handler"]
Run Code Online (Sandbox Code Playgroud)

google-chrome.repo

[google-chrome]
name=google-chrome - 64-bit
baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
Run Code Online (Sandbox Code Playgroud)

我使用 Node.js 和Puppeteer来控制浏览器。

const browser = await puppeteer.launch({
  dumpio: true,
  headless: true,
  executablePath: config.chromeExecutablePath,
  // all possible options and explanations
  // can be found here https://peter.sh/experiments/chromium-command-line-switches/
  args: [
    '--no-sandbox',
    '--headless',
    '--disable-gpu',
    '--disable-dev-shm-usage',
    '--single-process',
    '--user-data-dir=/tmp/user-data',
    '--data-path=/tmp/data-path',
    '--homedir=/tmp',
    '--disk-cache-dir=/tmp/cache-dir',
  ],
})
Run Code Online (Sandbox Code Playgroud)

在本地运行容器时它工作得很好,但是一旦我部署它并在 Lambda 中运行它,我就会收到这样的错误,整个过程最终会超时。

/usr/bin/google-chrome: line 45: /dev/fd/62: No such file or directory
/usr/bin/google-chrome: line 46: /dev/fd/62: No such file or directory
prctl(PR_SET_NO_NEW_PRIVS) failed
prctl(PR_SET_NO_NEW_PRIVS) failed
Run Code Online (Sandbox Code Playgroud)

知道可能是什么问题吗?