Skip to content

Commit f501eb4

Browse files
committed
Initial commit
0 parents  commit f501eb4

File tree

7 files changed

+2337
-0
lines changed

7 files changed

+2337
-0
lines changed

.babelrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"presets": ["es2017", "es2016"],
3+
"plugins": [
4+
[
5+
"add-header-comment",
6+
{
7+
"header": [
8+
"NOTE: This code is transpiled from ES2017 using Babel.",
9+
"Instead of modifying this file directly, work with the source code instead and upload the transpiled output here."
10+
],
11+
"commentLineStart": " * ",
12+
"commentEnd": "\n "
13+
}
14+
],
15+
"transform-es2017-object-entries",
16+
"transform-object-rest-spread",
17+
"array-includes"
18+
]
19+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
dist/
3+
artifact.zip

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# es2017-lambda-boilerplate
2+
3+
## What is it?
4+
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.
6+
7+
This boilerplate adds support for the following most commonly used JavaScript features that are not natively supported on AWS Lambda:
8+
9+
| ES2016/ES7 | Supported? |
10+
|:-----------|:----------:|
11+
| [Exponentiation operator (`**`)](http://node.green/#ES2016-features-exponentiation------operator) | :white_check_mark: |
12+
| [`Array.prototype.includes`](http://node.green/#ES2016-features-Array-prototype-includes) | :white_check_mark: |
13+
14+
| ES2017/ES8 | Supported? |
15+
|:-----------|:----------:|
16+
| [`Object.values`](http://node.green/#ES2017-features-Object-static-methods-Object-values), [`Object.entries`](http://node.green/#ES2017-features-Object-static-methods-Object-entries) | :white_check_mark: |
17+
| [Trailing commas in function syntax](http://node.green/#ES2017-features-trailing-commas-in-function-syntax) | :white_check_mark: |
18+
| [`async`/`await`](http://node.green/#ES2017-features-async-functions) | :white_check_mark: |
19+
20+
| ESNEXT | Supported? |
21+
|:-------|:----------:|
22+
| [Object rest/spread properties](http://node.green/#ESNEXT-candidate--stage-3--object-rest-spread-properties) | :white_check_mark: |
23+
24+
*Note: Only features which are not normally available on AWS Lambda Node.js 6.10.0 are listed. Most ES2015/ES6 features and earlier are supported.*
25+
26+
## Usage
27+
28+
Edit your Lambda function under `src/index.js`, and run:
29+
30+
```
31+
npm run package
32+
```
33+
34+
This will create an `artifact.zip` file which you can upload to AWS Lambda.
35+
36+
## Why?
37+
38+
### Latest ES2017 features
39+
40+
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.
41+
42+
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.
43+
44+
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:
45+
46+
```js
47+
const EC2 = new AWS.EC2();
48+
const Route53 = new AWS.Route53();
49+
50+
// Get instance by ID.
51+
const instances = await EC2.describeInstances({ InstanceIds: 'i-abcdef01' }).promise();
52+
53+
// Get public IP address.
54+
const publicIpAddress = instances.Reservations[0].Instances[0].PublicIpAddress;
55+
56+
// Do something else with the IP address...
57+
await Route53.changeResourceRecordSets({
58+
// ...
59+
}).promise();
60+
```
61+
62+
The usage of `async`/`await` reduces all the cruft involved when using either normal AWS SDK callbacks or chaining Promises.
63+
64+
### Internet connectivity handling
65+
66+
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.
67+
68+
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.
69+
70+
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.
71+
72+
Set the `INTERNET_CONNECTIVITY_TEST` constant to `true` in order to use this feature, otherwise it will not invoke the Internet connectivity test.
73+
74+
## Acknowledgements
75+
76+
This boilerplate was inspired from this [post](http://jessesnet.com/development-notes/2016/nodejs-es7-aws-lambda/) by Jesse Cascio.
77+
78+
## License
79+
80+
MIT

0 commit comments

Comments
 (0)