5
4

More than 3 years have passed since last update.

AWS Lambda × Selenium × Chromeでハマったところ

Last updated at Posted at 2020-03-08

起きたこと

既存のAWS Lambdaの処理にServerless Frameworkを適用する対応で、Lambda関数実行の際に以下エラーが発生し、長時間ハマった。

  • Seleniumのインスタンス作成時に「error while loading shared libraries: libglib-2.0.so.0: cannot open shared object file: No such file or directory」というエラー
  • 上記解消後、また、Seleniumのインスタンス作成時に「Message: chrome not reachable」というエラー

環境

  • AWS Lambda
  • python 3.8
  • serverless-chrome 1.0.0-55 (chromium 69.0.3497.81)
  • ChromeDriver 2.44
  • Selenium

対応

error while loading shared libraries

エラー詳細
error while loading shared libraries: libglib-2.0.so.0: cannot open shared object file: No such file or directory
対応

公式によると、python3.8をランタイムとして選ぶと、OSがAmazon Linux2になる。
Amazon Linux2はAmazon Linuxとはデフォルトのshared librariesが異なることが原因と考え、実際にOSをAmazon Linuxになるように修正したところエラーが解消した。

shared librariesを追加するよう頑張ったり、もしかしたら最新のChromeを使うようにすればこのエラー起きないのではと思いつつも、serverless-chrome(最新バージョンでも、chromium 69.0.3497.81まで)を使いたかったので、OSがAmazon Linuxになるpython3.7をランタイムとして選択することにした。

chrome not reachable

エラー詳細
[ERROR] WebDriverException: Message: chrome not reachable
(Driver info: chromedriver=2.44.609551 (5d576e9a44fe4c5b6a07e568f1ebc753f1214634),platform=Linux 4.14.138-99.102.amzn2.x86_64 x86_64)
Traceback (most recent call last):
  File "/var/task/test.py", line 33, in lambda_function
    driver = webdriver.Chrome('/opt/chromedriver', chrome_options=options)
  File "/opt/python/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/opt/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/opt/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/opt/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/opt/python/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
対応

ChromeDriver2.44はChromeのバージョン69〜71まで対応と記載があったが、念の為、同じChromeのバージョンに対応していると記載があるChromeDriver2.43に変更してみたらエラーが解消した。
ChromeDriver 2.44のリリースノートを確認し、特段問題を感じなかったため、ChromeDriver2.43を利用することで対応した。

まとめ

今回の処理のセキュリティの重要性さと対応の急務さを天秤にかけて、バージョンを下げるという手段をとりました。
最新のChromeを使えば上記も起きないのではないかと思っています。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4