Skip to content

Commit 40b69ea

Browse files
authored
Merge pull request #9 from smithclay/serverless-application-model
switch to SAM local from terraform
2 parents 4f589cb + eb0abd9 commit 40b69ea

File tree

10 files changed

+122
-137
lines changed

10 files changed

+122
-137
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ lambda_function.zip
55
build/
66
bin/
77
node_modules
8+
packaged.yaml
89

Makefile

Lines changed: 0 additions & 22 deletions
This file was deleted.

README.md

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,75 @@ Since this Lambda function is written using node.js, you can run almost any scri
1818
#### Requirements
1919

2020
* An AWS Account
21-
* [Terraform](https://terraform.io) (optional but highly recommended for function creation and deploy)
21+
* [AWS SAM Local](https://github.com/awslabs/aws-sam-local) AWS tool for running functions locally [Serverless Application Model](https://github.com/awslabs/serverless-application-model)
2222
* node.js + npm
2323
* Bash
24-
* `make`
2524

26-
#### Installing `modclean`
25+
#### Fetching dependencies
2726

28-
Lambdium uses the `modclean` NPM package to reduce the size of the `node_modules` directory. To install it, run:
27+
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).
2928

3029
```sh
31-
npm i -g modclean
30+
$ ./scripts/fetch-dependencies.sh
3231
```
3332

34-
#### Fetching dependencies
33+
#### Running locally with SAM
3534

36-
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).
35+
```sh
36+
$ sam local invoke Lambdium -e event.json
37+
```
38+
39+
### Deploying to
40+
41+
#### Creating a bucket for the function deployment
42+
43+
This will create a file called `packaged.yaml` you can use with Cloudformation to deploy the function.
44+
45+
You need to have an S3 bucket configured on your AWS account to upload the packed function files. For example:
3746

3847
```sh
39-
$ ./scripts/fetch-dependencies.sh
48+
$ export LAMBDA_BUCKET_NAME=lambdium-upload-bucket
49+
```
50+
51+
##### Reducing function size for performance (and faster uploads!)
52+
53+
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:
54+
55+
To install it:
56+
57+
```sh
58+
$ npm i -g modclean
59+
```
60+
61+
Then, run:
62+
63+
```sh
64+
$ modclean --patterns="default:*"
4065
```
4166

42-
#### Building Lambda `.zip` archive
67+
##### Packaging the function for Cloudformation using SAM
4368

4469
```sh
45-
$ make lambda
70+
$ sam package --template-file template.yaml --s3-bucket $LAMBDA_BUCKET_NAME --output-template-file packaged.yaml
4671
```
4772

48-
#### Creating and Deploying Using Terraform
73+
#### Creating and Deploying Using SAM
4974

50-
This will create the function using Terraform with all required permissions.
75+
This will create the function using Cloudformation after packaging it is complete.
5176

5277
```sh
53-
$ make deploy
78+
$ sam deploy --template-file ./packaged.yaml --stack-name <<your-cloudformation-stack-name>> --capabilities CAPABILITY_IAM
5479
```
5580
56-
The optional `DEBUG_ENV` environment variable will log additional information to Cloudwatch. The `PATH` environment variable points to the `bin` directory in this project—this is required in order to launch the `chromedriver` process.
81+
If set, the optional `DEBUG_ENV` environment variable will log additional information to Cloudwatch.
5782
58-
## Usage
83+
## Invoking the function
5984
60-
If dependencies are installed and `make deploy` succeeds you can have Lambda run a script. There's an example of a selenium-webdriver simple script in the `examples/` directory that the Lambda function can now run.
85+
Post-deploy, you can have lambda run a Webdriver script. There's an example of a selenium-webdriver simple script in the `examples/` directory that the Lambda function can now run.
6186
62-
Expected JSON input for the function: `{"Base64Script": "<Base64 Encoding of Selenium Script>"}` (this can also be provided as an environment variable named `BASE64_SCRIPT`).
87+
Expected JSON input for the function is: `{"Base64Script": "<Base64 Encoding of Selenium Script>"}` (this can also be provided as an environment variable named `BASE64_SCRIPT`).
6388
64-
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:
89+
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`:
6590
6691
```sh
6792
$ scripts/invoke.sh

event.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Base64Script": "Ly8gU2FtcGxlIHNlbGVuaW11bS13ZWJkcml2ZXIgc2NyaXB0IHRoYXQgdmlzaXRzIGdvb2dsZS5jb20KLy8gVGhpcyB1c2VzIHRoZSBzZWxlbml1bS13ZWJkcml2ZXIgMy40IHBhY2thZ2UuCi8vIERvY3M6IGh0dHBzOi8vc2VsZW5pdW1ocS5naXRodWIuaW8vc2VsZW5pdW0vZG9jcy9hcGkvamF2YXNjcmlwdC9tb2R1bGUvc2VsZW5pdW0td2ViZHJpdmVyL2luZGV4Lmh0bWwKLy8gJGJyb3dzZXIgPSB3ZWJkcml2ZXIgc2Vzc2lvbgovLyAkZHJpdmVyID0gZHJpdmVyIGxpYnJhcmllcwovLyBjb25zb2xlLmxvZyB3aWxsIG91dHB1dCB0byBBV1MgTGFtYmRhIGxvZ3MgKHZpYSBDbG91ZHdhdGNoKQoKY29uc29sZS5sb2coJ0Fib3V0IHRvIHZpc2l0IGdvb2dsZS5jb20uLi4nKTsKJGJyb3dzZXIuZ2V0KCdodHRwOi8vd3d3Lmdvb2dsZS5jb20vbmNyJyk7CiRicm93c2VyLmZpbmRFbGVtZW50KCRkcml2ZXIuQnkubmFtZSgnYnRuSycpKS5jbGljaygpOwokYnJvd3Nlci53YWl0KCRkcml2ZXIudW50aWwudGl0bGVJcygnR29vZ2xlJyksIDEwMDApOwokYnJvd3Nlci5nZXRUaXRsZSgpLnRoZW4oZnVuY3Rpb24odGl0bGUpIHsKICAgIGNvbnNvbGUubG9nKCJ0aXRsZSBpczogIiArIHRpdGxlKTsKfSk7CmNvbnNvbGUubG9nKCdGaW5pc2hlZCBydW5uaW5nIHNjcmlwdCEnKTs="
3+
}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ console.log('Loading function');
1010

1111
exports.handler = (event, context, callback) => {
1212
context.callbackWaitsForEmptyEventLoop = false;
13-
13+
1414
if (process.env.CLEAR_TMP) {
1515
log('attempting to clear /tmp directory')
1616
log(child.execSync('rm -rf /tmp/core*').toString());

lambda.tf

Lines changed: 0 additions & 82 deletions
This file was deleted.

lib/chromium.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ const defaultChromeFlags = [
4242
];
4343

4444
const HEADLESS_CHROME_PATH = 'bin/headless_shell';
45-
45+
const CHROMEDRIVER_PATH = '/var/task/bin/chromedriver';
4646
exports.createSession = function() {
47-
const service = new chrome.ServiceBuilder()
47+
const service = new chrome.ServiceBuilder(CHROMEDRIVER_PATH)
4848
.enableVerboseLogging()
4949
.build();
5050

package-lock.json

Lines changed: 53 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambdium",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "headless chromium in lambda prototype",
55
"main": "index.js",
66
"scripts": {

template.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
AWSTemplateFormatVersion : '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: headless chromium running selenium
4+
Resources:
5+
Lambdium:
6+
Type: AWS::Serverless::Function
7+
Properties:
8+
Handler: index.handler
9+
Runtime: nodejs6.10
10+
FunctionName: lambdium
11+
Description: headless chromium running selenium
12+
MemorySize: 1024
13+
Timeout: 10
14+
Environment:
15+
Variables:
16+
DEBUG_ENV: "true"
17+
CLEAR_TMP: "true"
18+
CodeUri: .

0 commit comments

Comments
 (0)