Skip to content

Commit f71b0a2

Browse files
authored
Merge pull request #24 from smithclay/layers
Add support for AWS Lambda Layers
2 parents 3433280 + 9ca07ad commit f71b0a2

17 files changed

+92
-65
lines changed

README.md

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
## lambdium
22
### headless chrome + selenium webdriver in AWS Lambda
33

4-
**Lambdium allows you to run a Selenium Webdriver script written in Javascript inside of an AWS Lambda function bundled with [Headless Chromium](https://developers.google.com/web/updates/2017/04/headless-chrome).**
4+
**Lambdium uses Selenium Webdriver with [Headless Chromium](https://developers.google.com/web/updates/2017/04/headless-chrome) to run Webdriver scripts written in JavaScript on AWS Lambda.**
55

6-
You can use this AWS Lambda function by itself or with other AWS services to:
6+
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:
77

88
* Run many concurrent selenium scripts at the same time without worrying about the infrastructure
99
* Run execute a selenium script via an HTTP call using API Gateway ([example app](/examples/apps/api-gateway.yaml))
@@ -33,27 +33,17 @@ This uses a special version of Chrome (headless chromium) from the [serverless-c
3333
The headless chromium binary is too large for Github, you need to fetch it using a script bundled in this repository. [Marco Lüthy](https://github.com/adieuadieu) has an excellent post on Medium about how he built chromium for for AWS Lambda [here](https://medium.com/@marco.luethy/running-headless-chrome-on-aws-lambda-fa82ad33a9eb).
3434

3535
```sh
36-
$ ./scripts/fetch-dependencies.sh
36+
$ ./layer/fetch-binaries.sh
3737
```
3838

39-
##### 2. Cleaning up the `node_modules` directory to reduce function size
39+
##### 2. Building
4040

41-
It's a good idea to clean the `node_modules` directory before packaging to make the function size significantly smaller (making the function run faster!). You can do this using the `modclean` package:
42-
43-
To install it:
44-
45-
```sh
46-
$ npm i -g modclean
47-
```
48-
49-
Then, run:
41+
This is now handled by the `sam build` command. In the root of this project, run:
5042

5143
```sh
52-
$ modclean --patterns="default:*"
44+
$ sam build
5345
```
5446

55-
Follow the prompts and choose 'Y' to remove extraneous files from `node_modules`.
56-
5747
##### 3. Running locally with SAM Local
5848

5949
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:
@@ -79,7 +69,7 @@ You need to create a S3 bucket configured on your AWS account to upload the pack
7969
#### 2. Packaging the function for Cloudformation using SAM
8070

8171
```sh
82-
$ sam package --template-file template.yaml --s3-bucket $LAMBDA_BUCKET_NAME --output-template-file packaged.yaml
72+
$ sam package --s3-bucket $LAMBDA_BUCKET_NAME > packaged.yaml
8373
```
8474

8575
#### 3. Deploying the package using SAM

layer/fetch-binaries.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
3+
mkdir -p bin
4+
5+
# Get headless shell
6+
curl -SL https://github.com/adieuadieu/serverless-chrome/releases/download/v1.0.0-35/stable-headless-chromium-amazonlinux-2017-03.zip > chromeheadless.zip
7+
unzip chromeheadless.zip -d bin/
8+
rm chromeheadless.zip
9+
10+
# Get Chromedriver
11+
curl -SL https://chromedriver.storage.googleapis.com/2.35/chromedriver_linux64.zip> chromedriver.zip
12+
unzip chromedriver.zip -d bin/
13+
rm chromedriver.zip
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

index.js renamed to src/index.js

File renamed without changes.
File renamed without changes.

lib/chromium.js renamed to src/lib/chromium.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ const defaultChromeFlags = [
4040
//'--trace-startup=*',
4141
];
4242

43-
const HEADLESS_CHROME_PATH = 'bin/headless-chromium';
44-
const CHROMEDRIVER_PATH = '/var/task/bin/chromedriver';
43+
const HEADLESS_CHROME_PATH = '/opt/bin/headless-chromium';
44+
const CHROMEDRIVER_PATH = '/opt/bin/chromedriver';
4545
exports.createSession = function() {
4646
var service;
4747
if (process.env.LOG_DEBUG || process.env.SAM_LOCAL) {
@@ -60,7 +60,7 @@ exports.createSession = function() {
6060
options.setLoggingPrefs(logPrefs);
6161

6262
options.setPerfLoggingPrefs({ enableNetwork: true, enablePage: true });
63-
options.setChromeBinaryPath(path.join(process.env.LAMBDA_TASK_ROOT, HEADLESS_CHROME_PATH));
63+
options.setChromeBinaryPath(HEADLESS_CHROME_PATH);
6464
options.addArguments(defaultChromeFlags);
6565
return chrome.Driver.createSession(options, service);
6666
}
File renamed without changes.
File renamed without changes.

package-lock.json renamed to src/package-lock.json

Lines changed: 43 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

template.yaml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,41 @@ AWSTemplateFormatVersion : '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
33
Description: selenium with headless chromium
44
Resources:
5+
DeploymentPermission:
6+
Type: "AWS::Lambda::LayerVersionPermission"
7+
Properties:
8+
Action: lambda:GetLayerVersion
9+
LayerVersionArn: !Ref ChromiumLayer
10+
Principal: '*'
11+
12+
ChromiumLayer:
13+
Type: AWS::Serverless::LayerVersion
14+
Properties:
15+
LayerName: chromium-selenium-layer
16+
Description: Headless Chromium and Selenium WebDriver
17+
ContentUri: ./layer
18+
CompatibleRuntimes:
19+
- nodejs8.10
20+
- python3.7
21+
- python2.7
22+
- go1.x
23+
- java8
24+
LicenseInfo: 'MIT'
25+
RetentionPolicy: Retain
26+
527
Lambdium:
628
Type: AWS::Serverless::Function
729
Properties:
830
Handler: index.handler
9-
Runtime: nodejs6.10
31+
Runtime: nodejs8.10
1032
FunctionName: lambdium
1133
Description: headless chromium running selenium
1234
# This needs to be fairly large: chromium needs a lot of memory
1335
MemorySize: 1156
1436
Timeout: 20
37+
Layers:
38+
- !Ref ChromiumLayer
1539
Environment:
1640
Variables:
1741
CLEAR_TMP: "true"
18-
CodeUri: .
42+
CodeUri: ./src

0 commit comments

Comments
 (0)