0

I'm trying to use headless-chromium on AWS Lambda with Golang and sclevine/agouti. I confirmed that chromedriver is executed on Lambda using exec.Command.

Error occurred when taking screenshot, but, I'm not sure headless-chromium is started properly. No error occurred when executing driver.Start() and driver.NewPage. Start message like ChromeDriver 2.43.600233, onlt local connection is allowed was not shown.

Code and outputs are like below.

func main() {
    lambda.Start(handler)
}

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    os.Setenv("PATH", os.Getenv("PATH") + ":/var/task")

    out1, err := exec.Command("/var/task/chromedriver", "-v").Output()
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("exec chromedriver %s\n", string(out1))
    // 2018/11/08 13:12:05 exec chromedriver ChromeDriver 2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d)

    opts := []agouti.Option{
        agouti.ChromeOptions(
            "args", []string{
                "headless",
                "no-default-browser-check",
                "verbose",
                "no-sandbox",
                "no-first-run",
                "disable-default-apps",
                "disable-popup-blocking",
                "disable-translate",
                "disable-background-timer-throttling",
                "disable-renderer-backgrounding",
                "disable-device-discovery-notifications",
            },
        ),
        agouti.Desired(
            agouti.Capabilities{
                "loggingPrefs": map[string]string{
                    "browser": "INFO",
                },
            },
        ),
    }
    opts = append(opts,
        agouti.ChromeOptions(
            "binary", "/var/task/headless-chromium",
        ))
    driver := agouti.NewWebDriver("http://{{.Address}}", []string{"/var/task/chromedriver", "--port={{.Port}}"}, opts...)
    if err := driver.Start(); err != nil {
        log.Fatalf("failed to start driver:%v", err)
    }
    p, err := driver.NewPage()
    if err != nil {
        log.Fatalf("failed to open page:%v", err)
    }
    if err := page.Navigate("https://www.google.com/"); err != nil {
        log.Fatalf("failed to navigate:%v", err)
    }
    if err := page.Screenshot("/tmp/top.jpg"); err != nil {
        log.Fatalf("failed to take ss:%v", err)
        // 2018/11/08 13:12:07 failed to take ss:failed to retrieve                 
        // screenshot: unexpected response:
        // {
        //    "sessionId": "30c4fc0e74ce6c865ced6c8443e873bc",
        //    "status": 6,
        //    "value": {
        //        "message": "invalid session id\n  (Driver info: chromedriver=2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d),platform=Linux 4.14.72-68.55.amzn1.x86_64 x86_64)"
        //    }
        //}
    }
}

I got binaries of headless-shell and chromedriver like below.

curl -SL https://chromedriver.storage.googleapis.com/2.43/chromedriver_linux64.zip > chromedriver.zip
unzip chromedriver.zip -d artifact/
rm chromedriver.zip
curl -SL https://github.com/adieuadieu/serverless-chrome/releases/download/v1.0.0-54/stable-headless-chromium-amazonlinux-2017-03.zip > headless-chromium.zip
unzip headless-chromium.zip -d artifact/
rm headless-chromium.zip

I deployed artifacts using SAM and artifacts were deployed under var/tasks/ on Lambda.

Please help me...

Toshinori Sugita
  • 1,674
  • 1
  • 13
  • 12
  • I checked the downloaded binary (stable build of headless chromium https://github.com/adieuadieu/serverless-chrome/releases/tag/v1.0.0-55 ) on EC2 (Amazon Linux). ./headless-chromium --headless --disable-gpu --window-size=1024,768 --hide-scrollbars --screenshot='yahoo.png' https://www.yahoo.co.jp This command was successful. I got yahoo.png via scp command and was able to open on mac. I think chromium binary itself has no problem. – Toshinori Sugita Nov 08 '18 at 15:12
  • Hi Toshinori, have you checked the .serverless folder and see if your binary file is attached? Do you have any error message in cloudwatch? – ThomasP1988 Nov 12 '18 at 13:12
  • Thank you for your comment! I don't use .serverless, but how can I confirm my binary is files are attached or not? Errors are shown like comment out right under log.Fatalf("failed to take ss:%v", err) in code above. – Toshinori Sugita Nov 14 '18 at 14:31

0 Answers0