You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43-19Lines changed: 43 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,6 @@
2
2
3
3
[](https://travis-ci.org/irvinlim/es2017-lambda-boilerplate)[](https://github.com/irvinlim/es2017-lambda-boilerplate/releases)[](http://opensource.org/licenses/MIT)
4
4
5
-
## What is it?
6
-
7
5
This is a boilerplate for [AWS Lambda](https://aws.amazon.com/lambda/) Node.js 6.10.0 functions, which allows you to use the latest JavaScript [ES2017/ES8 features](https://hackernoon.com/es8-was-released-and-here-are-its-main-new-features-ee9c394adf66) within a Lambda function.
8
6
9
7
This boilerplate adds support for the following most commonly used JavaScript features that are not natively supported on AWS Lambda:
@@ -24,21 +22,25 @@ _Note: Only features which are not normally available on AWS Lambda Node.js 6.10
24
22
25
23
## Usage
26
24
27
-
Edit your Lambda function under `src/index.js`, and run:
25
+
Edit your Lambda function under `src/main.js`, and run:
28
26
29
-
```
27
+
```js
30
28
npm run package
31
29
```
32
30
33
31
This will create an `artifact.zip` file which you can upload to AWS Lambda.
34
32
35
33
## Testing
36
34
37
-
### Test boilerplate
35
+
You can run automated tests for your Lambda function inside of a Docker container using [docker-lambda](https://github.com/lambci/docker-lambda):
38
36
39
-
_NOTE: Test boilerplate under development._
37
+
```js
38
+
npm run test
39
+
```
40
+
41
+
The test runner used is [Jest](https://github.com/facebook/jest) (with [Jasmine](https://jasmine.github.io)). All files in the `test/` directory which end with `.test.js` will be interpreted as a test suite.
40
42
41
-
The boilerplate allows you to run automated tests using [docker-lambda](https://github.com/lambci/docker-lambda)
43
+
This also requires Docker to be installed on your host; see the [docs for docker-lambda](https://github.com/lambci/docker-lambda) for more instructions.
42
44
43
45
### Specification tests
44
46
@@ -47,15 +49,17 @@ In order to ensure that the Babel configuration works and is following the spec,
47
49
***Functional testing**: Runs the relevant spec tests from [Test262](https://github.com/tc39/test262) (actual tests taken from [node.green](http://node.green/)) on [docker-lambda](https://github.com/lambci/docker-lambda) to mock the AWS Lambda environment
48
50
***Snapshot testing**: Unit testing strategy by storing snapshots of Babel-transformed source code and running unit tests against them
49
51
52
+
You can find the spec tests under `spec/functional` and `spec/snapshot` respectively.
53
+
54
+
If you are not going to modify `.babelrc`, you can choose to skip these tests by omitting the `npm run spec` script in `.travis.yml`. This will help to speed up your builds by a bit.
55
+
50
56
## Why?
51
57
52
58
### Latest ES2017 features
53
59
54
60
Even though Lambda supposedly supports Node.js 6.10.0, not all JavaScript features are supported. [www.whatdoeslambdasupport.com](http://www.whatdoeslambdasupport.com/) has a comprehensive list of what is supported and what are not.
55
61
56
-
If you are used to using features like `async`/`await` which have been around for a while now, you might find it tedious to build your own tooling to transpile all the latest ECMAScript features that you have been using all along.
57
-
58
-
That's why I built this boilerplate - using `async`/`await` was really important to me when making use of the AWS SDK on Lambda, like as follows:
62
+
This boilerplate adds support for the most commonly used features that are not available on Node 6.10.0 or AWS Lambda, such as `async`/`await` when used with the [AWS SDK](https://github.com/aws/aws-sdk-js):
The usage of `async`/`await` reduces all the cruft involved when using either normal AWS SDK callbacks or chaining Promises.
77
-
78
-
### Internet connectivity handling
80
+
### Run automated tests locally/through CI
79
81
80
-
I was also bitten badly by the fact that placing a Lambda function in a VPC requires a NAT gateway in order for the Lambda function to have outbound Internet connectivity, and I was trying to use the bundled AWS SDK to perform operations on the AWS API.
82
+
Instead of testing your Lambda function by uploading to AWS Lambda every single time, running automated tests in conjunction with CI is a better option. By using Docker to mock the AWS Lambda environment locally, you can write test cases to verify the correctness of your function, given an input (the [Lambda event](http://docs.aws.amazon.com/lambda/latest/dg/eventsources.html)):
81
83
82
-
Not knowing that using the SDK requires Internet connectivity (I assumed that the SDK could call the IPv4 link-local address for the metadata server `http://169.254.169.254` for API calls, and thus required for it to be placed in a VPC), I was stuck for a good couple of hours to find out why my Lambda functions consistently hit the 30s timeout I had set.
83
-
84
-
This boilerplate performs a quick Internet connectivity test (up to 1000ms) to help you guard and debug against this problem, terminating the execution instead of timing out only after the full duration of the Lambda execution time.
84
+
```js
85
+
importrunfrom'./util/runner';
86
+
87
+
it('should work', function() {
88
+
// Sample event from SNS.
89
+
constevent= {
90
+
Records: [
91
+
{
92
+
EventVersion:'1.0',
93
+
EventSource:'aws:sns',
94
+
Sns: {
95
+
MessageId:'95df01b4-ee98-5cb9-9903-4c221d41eb5e',
96
+
Message:'Hello from SNS!',
97
+
...
98
+
},
99
+
},
100
+
],
101
+
};
102
+
103
+
// Run the Lambda function against this event.
104
+
constresult=run(event);
105
+
106
+
expect(result).toEqual(true);
107
+
});
108
+
```
85
109
86
-
Set the `INTERNET_CONNECTIVITY_TEST` constant to `true` in order to use this feature, otherwise it will not invoke the Internet connectivity test.
110
+
This strategy also does not utilise your AWS Lambda invocation credits - meaning you are free to run as many tests as often as you like!
87
111
88
112
## Acknowledgements
89
113
90
-
This boilerplate was inspired from this [post](http://jessesnet.com/development-notes/2016/nodejs-es7-aws-lambda/) by Jesse Cascio.
114
+
This boilerplate was first inspired from this [post](http://jessesnet.com/development-notes/2016/nodejs-es7-aws-lambda/) by Jesse Cascio.
0 commit comments