From 4d29a7610232c9a842fe7c8dde72ed9816cdd331 Mon Sep 17 00:00:00 2001 From: Filip Date: Sun, 10 Aug 2025 11:04:31 +0200 Subject: [PATCH 1/3] Update README.md --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/README.md b/README.md index 8bba42b..38e6718 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,55 @@ CMD [ "main.handler" ] Available tags are listed [here](https://hub.docker.com/r/umihico/aws-lambda-selenium-python/tags) +## Advanced usage + +To get more than a minimal example, change the code and then run the container + +``` +docker run --name demo -p 9000:8080 umihico/aws-lambda-selenium-python:latest +``` + +### Dynamic url + +Change [this line](https://github.com/umihico/docker-selenium-lambda/blob/main/main.py#L25) to `chrome.get(event.get("url"))` and invoke the url set by [amazon/aws-lambda-python](https://hub.docker.com/r/amazon/aws-lambda-python#usage), any JSON in -d is passed to the function's event argument + +``` +curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"url": "https://example.com"}' +``` + +You can also have a fallback like `chrome.get(event.get("url", "https://example.com"))`, in which case you can invoke the url like this + +``` +curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}' +``` + +With sls, you can pass the url argument to a Lambda deployed on AWS + +``` +sls invoke --function demo --data '{"url": "https://github.com"}' +``` + +### Local execution + +Add the following to the end of main.py + +``` +if __name__ == "__main__" and os.getenv("AWS_LAMBDA_FUNCTION_NAME") is None: + print(handler()) +``` + +Now you can run the function outside of Lambda + +``` +docker exec demo python main.py +``` + +You can add a dynamic url to this setup with something like + +``` +event = {"target": sys.argv[1]} if len(sys.argv) > 1 else {} +``` + ## Side Project Are you interested in **Node.js** or **Playwright**? Please check out [docker-playwright-lambda](https://github.com/umihico/docker-playwright-lambda) From 5e2e1735d20d5a1c86f65ce49517954075bda24e Mon Sep 17 00:00:00 2001 From: Filip Date: Sun, 10 Aug 2025 16:02:15 +0200 Subject: [PATCH 2/3] Improve docs --- README.md | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 38e6718..a846fa8 100644 --- a/README.md +++ b/README.md @@ -37,29 +37,32 @@ Available tags are listed [here](https://hub.docker.com/r/umihico/aws-lambda-sel ## Advanced usage -To get more than a minimal example, change the code and then run the container - -``` -docker run --name demo -p 9000:8080 umihico/aws-lambda-selenium-python:latest -``` +To get more than a minimal example, change the code, rebuild the image, and then run the container ### Dynamic url -Change [this line](https://github.com/umihico/docker-selenium-lambda/blob/main/main.py#L25) to `chrome.get(event.get("url"))` and invoke the url set by [amazon/aws-lambda-python](https://hub.docker.com/r/amazon/aws-lambda-python#usage), any JSON in -d is passed to the function's event argument +Change [this line](https://github.com/umihico/docker-selenium-lambda/blob/7a19acc/main.py#L25) to `chrome.get(event.get("url"))`, prepare the container +```bash +docker build --platform linux/amd64 -t aws-lambda-selenium-python:latest . +docker run -p 9000:8080 aws-lambda-selenium-python:latest ``` + +Then invoke the url set by [amazon/aws-lambda-python](https://hub.docker.com/r/amazon/aws-lambda-python#usage), any JSON in -d is passed to the function's event argument + +```bash curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"url": "https://example.com"}' ``` You can also have a fallback like `chrome.get(event.get("url", "https://example.com"))`, in which case you can invoke the url like this -``` +```bash curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}' ``` With sls, you can pass the url argument to a Lambda deployed on AWS -``` +```bash sls invoke --function demo --data '{"url": "https://github.com"}' ``` @@ -67,20 +70,21 @@ sls invoke --function demo --data '{"url": "https://github.com"}' Add the following to the end of main.py -``` +```python if __name__ == "__main__" and os.getenv("AWS_LAMBDA_FUNCTION_NAME") is None: print(handler()) ``` Now you can run the function outside of Lambda -``` -docker exec demo python main.py +```bash +docker build --platform linux/amd64 -t aws-lambda-selenium-python:latest . +docker run --entrypoint python aws-lambda-selenium-python:latest main.py ``` You can add a dynamic url to this setup with something like -``` +```python event = {"target": sys.argv[1]} if len(sys.argv) > 1 else {} ``` From 47061478fa758844a728adaec23386d9d17188cd Mon Sep 17 00:00:00 2001 From: Filip Date: Mon, 11 Aug 2025 16:38:25 +0200 Subject: [PATCH 3/3] Fix event --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a846fa8..aabdff2 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ docker run --entrypoint python aws-lambda-selenium-python:latest main.py You can add a dynamic url to this setup with something like ```python -event = {"target": sys.argv[1]} if len(sys.argv) > 1 else {} +event = {"url": sys.argv[1]} if len(sys.argv) > 1 else {} ``` ## Side Project