Skip to content

rdarneal/aws-lambda-python-selenium-cdk-template

Repository files navigation


AWS Lambda Python - Docker Selenium CDK Template App

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
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

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.

(back to top)

Built With

(back to top)

Getting Started

To get a local copy up and running follow these simple example steps.

Prerequisites

This template requires a few system prequisites:

  • Node.js and node package manager. (npm, in this exapmle installed via node version manager nvm)
  • 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 variables ACCOUNT_ID (your AWS account ID) and REGION (your AWS region). These are read by app.py

Installation & Usage

  1. Clone the repo to your local directory
$ git clone https://github.com/rdarneal/aws-lambda-python-selenium-cdk-template.git
  1. Navigate to the new directory
$ cd aws-lambda-python-selenium-cdk-template
  1. Create a new virtual environemnt called .venv with python at the top level
$ python -m venv .venv
  1. Activate the virtual environment
$ source .venv/bin/activate # Use this line for Linux/MacOS
source .venv/Scripates/activate.bat  # Use this line for Windows
  1. Install the requirements file in the top level directory (required for CDK use in python)
$ pip install -r requirements.txt
  1. Login via the AWS CLI (for non-IAM examples check out AWS documentation) using a profile
$ aws sso login --profile your-profile
  1. Deploy the app using the CDK
$ cdk deploy --profile your-profile
  1. Find the name of your newly deployed function (this will take the base name you define in app.py and function name from scraper.py)
$ aws lambda list-functions --profile your-profile
  1. 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.

Testing locally with docker prior to deploying

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.

  1. Navigate to the scraper directory to build the Dockerfile
$ cd lambda/scraper
  1. Build the docker image locally
$ docker build -t my-image:test .
  1. Since the root image is an AWS/Lambda/Python image, you can run it simply with:
$ docker run -p 9000:8080 my-image:test
  1. 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.

  1. 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.

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Project Link: https://github.com/rdarneal/aws-lambda-python-selenium-cdk-template

(back to top)

Acknowledgments

(back to top)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published