0

I need to do a Proof of Concept of whether I can run Headless Chrome on AWS or not and I tried to implement the solution from the below link:

Running Selenium and Headless Chrome on AWS Lambda Layers Python

The following is my Python Code which is stored in the Lambda Layer:

from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from sys import platform
import json

def hello(event,context):
    chrome_options = webdriver.ChromeOptions()
    if platform == 'linux':
        try:
            driver_path = '/opt/chromedriver'
            chrome_options.binary_location = '/opt/headless-chromium'
            chrome_options.add_argument = ('--window-size=1920,1080')
            chrome_options.add_argument = ('--disable-gpu')
            chrome_options.add_argument = ('--disable-dev-shm-usage')
            chrome_options.add_argument = ('--headless')
            chrome_options.add_argument = ('--no-sandbox') # required when running as a root-user. Otherwise you would get no sand-box errors
            chrome_options.add_argument = ('--single-process')
            driver = webdriver.Chrome(driver_path,chrome_options=chrome_options)
            response = page_title(driver)
            end_test(driver)
            return json.dumps(response)
        except OSError as e:
            return e
        except WebDriverException as e:
            return e

def page_title(chrome_driver):
    chrome_driver.get('https://www.google.com')
    if (chrome_driver.title) == "Google":
        body = "Page-Title:"+chrome_driver.title
        response = {
                "statusCode": 200,
                "body": body
        }
    else:
        response = {
            "statusCode": 404,
            "body": "Not Found"
        }
    return response

def end_test(chrome_driver):
    chrome_driver.close()
    chrome_driver.quit()

I have created the two Layers (Selenium and Lambda Layer) according to the article and created the directory accordingly and serverless.yaml files and deployed both the layers using the command:

sls deploy --verbose

When I run the following command:

sls invoke --function hello

I get the follwing error:

{
"errorMessage": "WebDriverException(\"unknown error: Chrome failed to start: exited abnormally\\n  (unknown error: DevToolsActivePort file doesn't exist)\\n  (The process started from chrome location /opt/headless-chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)\\n  (Driver info: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux 4.14.88-72.76.amzn1.x86_64 x86_64)\", None, None) is not JSON serializable",
"errorType": "TypeError",
"stackTrace": [
    [
        "/var/lang/lib/python3.6/json/__init__.py",
        238,
        "dumps",
        "**kw).encode(obj)"
    ],
    [
        "/var/lang/lib/python3.6/json/encoder.py",
        199,
        "encode",
        "chunks = self.iterencode(o, _one_shot=True)"
    ],
    [
        "/var/lang/lib/python3.6/json/encoder.py",
        257,
        "iterencode",
        "return _iterencode(o, 0)"
    ],
    [
        "/var/runtime/awslambda/bootstrap.py",
        110,
        "decimal_serializer",
        "raise TypeError(repr(o) + \" is not JSON serializable\")"
    ]
]

}

Error --------------------------------------------------

  Invoked function failed

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information -----------------------------
     OS:                     linux
     Node Version:           11.9.0
     Serverless Version:     1.36.3

I have installed the required chromedriver version so that it can launch the headless chrome using binary, the program is running successfully on my local machine after changing the path of the chromedriver and headless chromium but not able to run the program on AWS.

The goal of doing the POC is to further implement the following article:

UI Testing at Scale With AWS Lambda

Any ideas on how can I fix the issue and is there an effective way of running selenium and headless chrome on AWS.

Let me know if you need any more information in the comments :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.