Last active
January 3, 2020 15:20
-
-
Save umihico/b0ef4ac13c9abc0cf97029cfef09a530 to your computer and use it in GitHub Desktop.
create selenium layer including binary WORKS python 3.7. NOT 3.8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from selenium import webdriver | |
def lambda_handler(event, context): | |
chrome=Chrome() | |
chrome.get('https://www.google.com/') | |
print(chrome.title) | |
chrome.quit() | |
return { | |
'statusCode': 200, | |
'body': json.dumps('Hello from Lambda!') | |
} | |
def Chrome(): | |
options = webdriver.ChromeOptions() | |
options.binary_location = "/opt/python/bin/headless-chromium" | |
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("--single-process") | |
options.add_argument("--ignore-certificate-errors") | |
options.add_argument("--homedir=/tmp") | |
chrome = webdriver.Chrome("/opt/python/bin/chromedriver", chrome_options=options) | |
return chrome | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir -p python/bin/ | |
curl -SL https://github.com/adieuadieu/serverless-chrome/releases/download/v1.0.0-37/stable-headless-chromium-amazonlinux-2017-03.zip > headless-chromium.zip | |
unzip headless-chromium.zip -d python/bin/ | |
curl -SL https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip > chromedriver.zip | |
unzip chromedriver.zip -d python/bin/ | |
docker run --rm -v $(pwd):/var/task -w /var/task lambci/lambda:build-python3.7 pip install selenium -t ./python | |
zip -r layer.zip python | |
aws lambda publish-layer-version --layer-name selenium --zip-file fileb://layer.zip --compatible-runtimes python3.7 | |
rm -rf layer.zip python chromedriver.zip headless-chromium.zip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment