2

How do I install Chrome in AWS Lambda? I know I might need a specific file from an EC2 instance but I can't figure out how to retrieve it.

not duplicate as as a micro instance ec2 won't be the same enviroment as a lambda

Meerfall the dewott
  • 209
  • 1
  • 4
  • 19
  • 1
    Possible duplicate of [Running Chrome on an AWS micro instance](https://stackoverflow.com/questions/16468855/running-chrome-on-an-aws-micro-instance) – Abhinav Suman Nov 15 '19 at 12:27
  • @animusmind not duplicate as as a micro instance ec2 won't be the same enviroment as a lambda – Meerfall the dewott Nov 15 '19 at 13:17
  • 1
    Chrome typically means "Google Chrome", the cross-platform browser. It doesn't make sense to install a browser in an environment like AWS Lambda. Do you mean something else, like headless Chrome? Also, you've tagged this question with 'java'. How does java relate to your question? – jarmod Nov 15 '19 at 14:32
  • @jarmod People have done it for scimmers. To use headless chrome you would need to install it (or at least that what a couple of sources say). I tagged it with Java because that the language I used to write the lambda function in – Meerfall the dewott Nov 15 '19 at 15:51
  • So, what is it that you want to do exactly? I still don't understand. – jarmod Nov 15 '19 at 16:01
  • @jarmod I need to be able to install google chrome into a lambda's environment so I can run headless chrome correctly – Meerfall the dewott Nov 15 '19 at 16:05

3 Answers3

1

You could create a Lambda Layer with a compiled Google Chrome. There are docker containers emulating Lambda execution environment, e.g. "lambci/lambda:java8". Could be tricky, since you need to build the browser from source code yourself, while there may be many dependencies missing.

Have you tried looking at other people's solutions? For example, this repo seems to have an already compiled Lambda Layer with Google Chrome Google Chrome for AWS Lambda as a layer

Phil
  • 53
  • 5
  • Lambda layer is Javascript based, I need a java based solution if possible otherwise I would have to rework huge portions of it. – Meerfall the dewott Nov 18 '19 at 17:03
  • 1
    Absolutely not true. A Lambda Layer is an archive that is extracted into "/opt". Please have a look here: [Including Library Dependencies in a Layer](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path). My idea was to compile it as a binary and call through ProcessBuilder – Phil Nov 18 '19 at 17:19
0

You should drop the requirement for Java in this case. It's not your friend.

Switch to Node.js, if only for this Lambda, and use alixaxel/chrome-aws-lambda and Puppeteer.

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • I'm seriously considering this however the entire project is based on Java and could result in rewriting huge portions of the code to fix it. I need a Java based solution – Meerfall the dewott Nov 15 '19 at 16:21
  • Maybe find a way to use both. Not sure what your workflow is but if it's scrape then process the results, may do the scraping and web page manipulation in Node.js, deliver the results to S3, and trigger your subsequent Java Lambda to process it. If the workflow is driven by your Java code (rather than post-scraping) then build a Node.js scraper that you can send API commands to via API Gateway, and control it from your Java code. – jarmod Nov 15 '19 at 16:30
0

As AWS Lambda recently announced (2020) the Container Image Support, you can bring your own runtime for your Java code.

A sample Docker Image might look like the following:

FROM public.ecr.aws/lambda/java:11

RUN yum install -y wget unzip libX11

RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm && \
    yum install -y google-chrome-stable_current_x86_64.rpm

RUN CHROME_DRIVER_VERSION=`curl -sS https://chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
    wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip && \
    unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

COPY target/dependency ${LAMBDA_TASK_ROOT}/lib/
COPY target/classes ${LAMBDA_TASK_ROOT}

CMD ["de.rieckpil.test.InvokeWebDriver::handleRequest"]

Once you push this Docker Image to your ECR, you can create a Lambda function that uses your image.

However, I'm still unable to launch Chrome 89 (already tried a gazillion of ChromeOptions arguments combinations) ...

/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

Maybe someone in the future has more success with this and can use this as a template :D

rieckpil
  • 10,470
  • 3
  • 32
  • 56
  • Did you find a fix? I'm facing the same issue 'touch: cannot touch '/home/sbx_user1051/.local/share/applications/mimeapps.list': No such file or directory'. https://stackoverflow.com/questions/76998462/google-chrome-tries-to-create-mimeapps-list-on-ubuntu-docker-image-hosted-on-aws – Basit Aug 29 '23 at 08:30