10

I am trying to set up a headless Chromium browser in AWS Lambda using a docker container. My Dockerfile looks like this

# 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"]

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

I use Node.js and Puppeteer to control the browser.

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',
  ],
})

It works perfectly fine when running the container locally but once I deploy it and run it in Lambda I get errors like this, and the whole thing just timeouts eventually.

/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

Any idea what could be the problem?

Jozef Cipa
  • 2,133
  • 3
  • 15
  • 29
  • See if any solution from this thread helps https://stackoverflow.com/questions/65429877/aws-lambda-container-running-selenium-with-headless-chrome-works-locally-but-not – Tarun Lalwani Apr 09 '21 at 09:27
  • Looks like you don't use a proper Lambda handler. The code of your Lambda needs to follow a certain pattern, to allow the AWS Lambda service to execute your Lambda. The error looks like that does not work. In your Dockerfile you set the `CMD` to `app.handler`. Is your Javascript in a file called `app.js` and does it have a function `handler` that handles a "request"? From the code you provided it does not look like it. – Jens Apr 09 '21 at 11:36
  • @Jens oh yeah, I didn't post the app.js code but yeah, I have an exported `handler` function there. I tried it locally with lambda-runtime-emulator and everything works properly on my machine – Jozef Cipa Apr 09 '21 at 12:55
  • Did you find a solution? How did you solve it? – G.M May 11 '23 at 12:11
  • @G.M I didn't, so I had to spawn a standalone pod in kubernetes and run the pupeteer there – Jozef Cipa May 17 '23 at 12:37

0 Answers0