1

I have struggled to get Headless Chrome running on AWS Lambda for days. It works fine on EC2 but when I try it on Lambda, I just get "Message: 'chromedriver' executable may have wrong permissions.

The modules are zipped with the chromedriver and headless-chromium executables in the root directory of the zip file. The total zipped file I upload to S3 is 52mb but extracted it is below the 250mb limit so I don't think that is the issue.

Python Zip Folder Structure Image

from selenium import webdriver


def lambda_handler(event, context):
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")
    options.add_argument("--window-size=1280x1696")
    options.add_argument("--disable-application-cache")
    options.add_argument("--disable-infobars")
    options.add_argument("--no-sandbox")
    options.add_argument("--hide-scrollbars")
    options.add_argument("--enable-logging")
    options.add_argument("--log-level=0")
    options.add_argument("--v=99")
    options.add_argument("--single-process")
    options.add_argument("--ignore-certificate-errors")
    options.add_argument("--homedir=/tmp")
    options.binary_location = "/var/task/headless-chromium"

    driver = webdriver.Chrome("/var/task/chromedriver", chrome_options=options)
    driver.get("https://www.google.co.uk")
    title = driver.title
    driver.close()

    return title


if __name__ == "__main__":
    title = lambda_handler(None, None)
    print("title:", title)

A few posts on the web have reported compatibility issues that may have caused problems so I have the specific executable versions for Chrome and ChromeDriver from the web, where others seem to on previous success EC2 and other means.

DOWNLOAD SOURCES FOR HEADLESS CHROME AND CHROMEDRIVER (stable) https://github.com/adieuadieu/serverless-chrome/releases/tag/v1.0.0-37 (https://sites.google.com/a/chromium.org/chromedriver/downloads) Download unavailable so retrieved from the source below https://chromedriver.storage.googleapis.com/index.html?path=2.37/

Can anyone help me crack this?

sugarcup
  • 11
  • 4
  • Change the permissions of the both executables (chromedriver and headless-chromium) to 777 and upload them. The issue is resolved. – Sai Sri Krishna Kotha Nov 23 '18 at 11:40
  • have you solve this issue? I am using windows to run my lambda locally with SAM build and its working well. My permissions seems right too. Really appreciated – SMDC Jul 12 '19 at 05:54
  • @SMDC you should deploy files from Linux, WSL would work as well if you have Win 10. – Nerman Jun 26 '20 at 13:09

1 Answers1

3

I found a solution for this problem few minutes ago. When use chromedriver in Lambda Function (I think) it need permission can write. but when chrome driver file is in 'task' folder or 'opt' folder, user can only have read permission.

Only folder can change permission in Lambda Function is 'tmp' folder.

So I move the chrome driver file to 'tmp' folder. and it works.

like this.

os.system("cp ./chromedriver /tmp/chromedriver")
os.system("cp ./headless-chromium /tmp/headless-chromium")
os.chmod("/tmp/chromedriver", 0o777)
os.chmod("/tmp/headless-chromium", 0o777)
chrome_options.binary_location = "/tmp/headless-chromium"
driver = webdriver.Chrome(executable_path=r"/tmp/chromedriver",chrome_options=chrome_options)
Gandang
  • 31
  • 2