• Stars
    star
    256
  • Rank 159,219 (Top 4 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

headless chrome + selenium webdriver in AWS Lambda using the serverless application model

lambdium

headless chrome + selenium webdriver in AWS Lambda

Lambdium uses Selenium Webdriver with Headless Chromium to run Webdriver scripts written in JavaScript on AWS Lambda. Since this project was created, AWS now offers this as a completely managed service called Device Farm Desktop Browser Testing.

You can use this AWS Lambda function by itself, bundled with your own application as a standalone AWS Lambda layer, or with other AWS services to:

  • Run many concurrent selenium scripts at the same time without worrying about the infrastructure
  • Run execute a selenium script via an HTTP call using API Gateway (example app)
  • Configure Cloudwatch events to run a script on a schedule (example app)
  • Integrate selenium tests running in Chrome into different event-driven workflows (like CodeDeploy checks, webhooks, or uploads to an S3 bucket)

Since this Lambda function is written using node.js, you can run almost any script written for selenium-webdriver. Example scripts can be found in the examples directory.

This uses a special version of Chrome (headless chromium) from the serverless-chrome project.

This is highly experimental and not all chromedriver functions will work. Check issues for known issues.

Requirements and setup for local development

This project is on the AWS Serverless Application Repository, allowing you to install it in your AWS account with one click. Install in your AWS account here. Quickstart instructions are in the README-SAR.md file.

  • An AWS Account
  • The AWS SAM Local running functions locally with the Serverless Application Model (see: template.yaml, install: npm install -g aws-sam-local)
  • node.js + npm
  • modclean npm modules for reducing function size
  • Bash

Local development setup

1. Fetching large binary dependencies

The headless chromium binary is too large for Github, you need to fetch it using a script bundled in this repository. Marco Lüthy has an excellent post on Medium about how he built chromium for for AWS Lambda here.

    $ ./layer/fetch-binaries.sh
2. Building

This is now handled by the sam build command. In the root of this project, run:

    $ sam build
3. Running locally with SAM Local

SAM Local can run this function on your computer inside a Docker container that acts like AWS Lambda. To run the function with an example event trigger that uses selenium to use headless chromium to visit google.com, run this:

    $ sam local invoke Lambdium -e event.json

Deploying the function to AWS

To deploy the function to your AWS account, you'll need to have followed the instructions above to fetch dependencies. Running it locally with SAM local and the test event (in event.json) is a good idea to verify everything works correctly before running it in the cloud.

1. Creating a S3 bucket for the function deployment

This will create a file called packaged.yaml you can use with Cloudformation to deploy the function.

You need to create a S3 bucket configured on your AWS account to upload the packed function files. For example:

    $ export LAMBDA_BUCKET_NAME=lambdium-upload-bucket

2. Packaging the function for Cloudformation using SAM

    $ sam package --s3-bucket $LAMBDA_BUCKET_NAME > packaged.yaml

3. Deploying the package using SAM

This will create the function using Cloudformation after packaging it is complete.

    $ sam deploy --template-file ./packaged.yaml --stack-name <<your-cloudformation-stack-name>> --capabilities CAPABILITY_IAM

If set, the optional DEBUG_ENV environment variable will log additional information to Cloudwatch.

Running the function

Post-deploy, you can have AWS Lambda run a selenium script. There's an example of a selenium-webdriver simple script in the examples/ directory that the Lambda function can now run.

Expected JSON input for the function event trigger is: {"Base64Script": "<Base64 Encoding of Selenium Script>"} (this can also be provided as an environment variable named BASE64_SCRIPT).

To run the example Selenium script, you can use the example with the AWS CLI in the scripts directory. It takes care of base64 encoding the file and assumes the function name is lambdium running in us-west-2:

    $ scripts/invoke.sh

To use your own selenium-webdriver script:

    $ scripts/invoke.sh ~/Desktop/my-script.js

Related projects

More Repositories

1

faassh

simple go SSH server with reverse tunneling designed for running in cloud functions like AWS lambda
Go
84
star
2

rlinklayer

Richard Linklayer: experimental TCP/IP over Amazon Cloudwatch Logs (and tags)
Go
60
star
3

sayeasy

say.js API wrapper for text-to-speech on a mac over networks.
JavaScript
17
star
4

samuel-l-incident

Trigger PagerDuty incidents that contain the wisdom of Samuel L. Jackson. And bacon.
Ruby
11
star
5

newrelic-k8s-guestbook

demo of node.js running on k8s with New Relic APM
JavaScript
7
star
6

calming-metrics-lambda

lambda function that pulls data from New Relic Insights
JavaScript
5
star
7

xray-collector-node

AWS X-Ray Traces -> New Relic Insights Events
JavaScript
4
star
8

lambda-talk

Lambda powered text-to-speech using Polly + API Gateway. Demo: http://lambda-talk-example.s3-website.us-east-2.amazonaws.com/
CSS
3
star
9

tootles

a PagerDuty incident webhook consumer that prints incoming webhooks to STDOUT.
JavaScript
3
star
10

squarenoia-ios

squareanoia: view nearby SF crime. rebuilt in swift for iOS!
Swift
2
star
11

developer-party-blocks

Creates SVG files appropriate for engraving on Jenga® blocks using Otherplan's Othermill CNC machine.
Makefile
2
star
12

afterparty

New Relic's Node.js Interactive (Unofficial) Afterparty
JavaScript
2
star
13

AndroidPerfTestExample

Automated Android performance testing with AWS DeviceFarm, New Relic, and TravisCI
Java
2
star
14

express-svrs

GitHub repository for AWS CodeStar Express.js web service express-svrs.
JavaScript
1
star
15

go-etherdome

experimental ethereum BaaS running on AWS
JavaScript
1
star
16

llm-chat-react-vercel

Simple LLM chat application with a python backend and react frontend, deployable on Vercel
TypeScript
1
star
17

otel-sensu-handler-plugin

Go
1
star
18

packer-newrelic-infrastructure

Example packer configuration for AWS Linux/Ubuntu with New Relic Infrastructure
Shell
1
star
19

lambda-cloudwatch-to-insights

sends data from an AWS cloudwatch log to New Relic Insights as a custom event
JavaScript
1
star
20

etherimp

Ethereum smart contract based on Robert Louis Stevenson's "The Bottle Imp" (written 1891)
JavaScript
1
star
21

gopher-dance-party-frontend

Express application for dancing things.
JavaScript
1
star