Use this code as a starting point for deploying your own AWS Lambda functions that utilize Selenium. Useful for web-automation, serverless web-scraping.
Instructions below are provided for running this application via AWS SSO (IAM Identity Center), using the AWS Cloud Development Kit (CDK) and Python.
Table of Contents
This project was created as I was searching for a method to deploy a serverless web-automation bot on AWS Lambda. After finding multiple useful repositories, I merged them together to create a template for CDK web deployment.
To get a local copy up and running follow these simple example steps.
This template requires a few system prequisites:
- Node.js and node package manager. (
npm
, in this exapmle installed via node version managernvm
) - An AWS account, with IAM Identity Center configured.
- AWS Cloud Development Kit CLI (run using
cdk
) - AWS Command Line Interface v2 (
aws
) - Docker engine (
docker
) - Python (
python
)
Configuration prequisites:
- Setup your aws CLI to run using SSO (previously IAM Identity Center)
- Ensure you can run
aws sso login --profile <your-profile>
to successfully login to AWS. - Create a local
.env
file in the project folder, for the variablesACCOUNT_ID
(your AWS account ID) andREGION
(your AWS region). These are read byapp.py
- Clone the repo to your local directory
$ git clone https://github.com/rdarneal/aws-lambda-python-selenium-cdk-template.git
- Navigate to the new directory
$ cd aws-lambda-python-selenium-cdk-template
- Create a new virtual environemnt called
.venv
with python at the top level
$ python -m venv .venv
- Activate the virtual environment
$ source .venv/bin/activate # Use this line for Linux/MacOS
source .venv/Scripates/activate.bat # Use this line for Windows
- Install the requirements file in the top level directory (required for CDK use in python)
$ pip install -r requirements.txt
- Login via the AWS CLI (for non-IAM examples check out AWS documentation) using a profile
$ aws sso login --profile your-profile
- Deploy the app using the CDK
$ cdk deploy --profile your-profile
- Find the name of your newly deployed function (this will take the base name you define in
app.py
and function name fromscraper.py
)
$ aws lambda list-functions --profile your-profile
- Invoke the function via AWS CLI, output the response as
response.json
, if successful, you should see a STATUS:200 response in your command line. Check the json output file for the return values.
$ aws lambda invoke --profile your-profile --function-name your-function-name-copied-from-step-8 response.json
The application stack should now be available in your AWS account if you visit AWS Lambda.
If you would like to check the application locally, you can separately build a test of the docker container image and run / test it locally before deploying.
- Navigate to the scraper directory to build the Dockerfile
$ cd lambda/scraper
- Build the docker image locally
$ docker build -t my-image:test .
- Since the root image is an AWS/Lambda/Python image, you can run it simply with:
$ docker run -p 9000:8080 my-image:test
- Open a new shell (or pass -d flag in the previous command to run in background) and send a cURL request to the local container port you exposed:
$ curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"this is optional"}'
You should receive a response that matches the return of the lambda_function.handler
function.
- Shut down the container
$ docker kill my-image:test
This is useful to do before pushing the container to AWS, as it will allow you to confirm your code is functioning as expected.
Distributed under the MIT License. See LICENSE.txt
for more information.
Project Link: https://github.com/rdarneal/aws-lambda-python-selenium-cdk-template