Skip to content

Commit 5d03438

Browse files
authored
Merge pull request #18 from irvinlim/0.3.1
Improve documentation in README and various code files, as well as to clean up some oddities in the code.
2 parents b43be1a + 11a2d53 commit 5d03438

6 files changed

Lines changed: 58 additions & 32 deletions

File tree

README.md

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
[![Travis CI](https://img.shields.io/travis/irvinlim/es2017-lambda-boilerplate.svg)](https://travis-ci.org/irvinlim/es2017-lambda-boilerplate) ![](https://codebuild.ap-southeast-1.amazonaws.com/badges?uuid=eyJlbmNyeXB0ZWREYXRhIjoiNlhFcld3M3VSMFM0MEkzUlBMQk1FdDU1c1RGc2dnVlpNaDdFZHlzSnQydDVJNm9RVFhxbXA3NkYxK3QwUVd4eVZyUTRiejZ1UGhRTFJYMTJJSzNLT2ZBPSIsIml2UGFyYW1ldGVyU3BlYyI6ImRFMWxLcHo2LzJmb3YycGEiLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D&branch=master) [![Greenkeeper badge](https://badges.greenkeeper.io/irvinlim/es2017-lambda-boilerplate.svg)](https://greenkeeper.io/) [![GitHub](https://img.shields.io/github/release/irvinlim/es2017-lambda-boilerplate.svg)](https://github.com/irvinlim/es2017-lambda-boilerplate/releases) [![The MIT License](https://img.shields.io/badge/license-MIT-orange.svg)](http://opensource.org/licenses/MIT)
66

7-
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.
7+
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). The boilerplate also allows you to test your function in a Docker container (thanks to [docker-lambda](https://github.com/lambci/docker-lambda)), and also includes common configurations for CI/CD, for both [Travis CI](https://travis-ci.org/) and [AWS CodeBuild](https://aws.amazon.com/codebuild/) + [AWS CloudFormation](https://aws.amazon.com/cloudformation/).
88

9-
This boilerplate adds support for the following most commonly used JavaScript features that are not natively supported on AWS Lambda:
9+
## ES2017 features
10+
11+
This boilerplate adds support for the following most commonly used JavaScript features that are not natively supported on Node.js 6.10:
1012

1113
| Feature | Supported? |
1214
| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :----------------: |
@@ -20,17 +22,23 @@ This boilerplate adds support for the following most commonly used JavaScript fe
2022
| **ESNEXT** | |
2123
| [Object rest/spread properties](http://node.green/#ESNEXT-candidate--stage-3--object-rest-spread-properties) | :white_check_mark: |
2224

23-
_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+
_Note: Most ES2015/ES6 are earlier features are supported._
2426

2527
## Usage
2628

2729
Edit your Lambda function under `src/main.js`, and run:
2830

2931
```sh
30-
npm run package
32+
npm run build
3133
```
3234

33-
This will create an `artifact.zip` file which you can upload to AWS Lambda.
35+
This will transpile your functions down to ES5 using Babel, so that it can be executed using the Node.js 6.10.0 runtime.
36+
37+
For convenience, the following command will create an `artifact.zip` file which you can upload to AWS Lambda:
38+
39+
```sh
40+
npm run package
41+
```
3442

3543
## Testing
3644

@@ -40,9 +48,9 @@ You can run automated tests for your Lambda function inside of a Docker containe
4048
npm run test
4149
```
4250

43-
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.
51+
All files in the `test/` directory which end with `.test.js` will be interpreted as a test suite. A sample unit test is provided under [test/example.test.js](https://github.com/irvinlim/es2017-lambda-boilerplate/blob/master/test/example.test.js) to get you started.
4452

45-
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.
53+
The test runner used is [Jest](https://github.com/facebook/jest) (with [Jasmine](https://jasmine.github.io)). Using docker-lambda also requires Docker to be installed on your host; see the [docs for docker-lambda](https://github.com/lambci/docker-lambda) for more instructions.
4654

4755
### Specification tests
4856

@@ -57,21 +65,21 @@ If you are not going to modify `.babelrc`, you can choose to skip these tests by
5765

5866
## Deployment
5967

60-
### Deployment through AWS SDK
68+
### Deployment using the AWS SDK
6169

62-
You can automatically deploy to AWS Lambda locally or through CI (e.g. Travis) using the AWS SDK, as long as you provide an access key for an IAM user that has write access to AWS Lambda. A single NPM script allows you to deploy using this method:
70+
You can automatically deploy to AWS Lambda locally or through CI (e.g. Travis CI) using the AWS SDK, as long as you provide an access key for an IAM user that has write access to AWS Lambda. A single NPM script allows you to deploy using this method:
6371

6472
```sh
6573
npm run deploy
6674
```
6775

68-
See [Environment variables](#environment-variables) for the list of environment variables that are required for deployment.
76+
See [Environment variables](#environment-variables) for the list of environment variables that are required for SDK deployment.
6977

7078
### Deployment through CloudFormation + CodeBuild
7179

72-
Instead of depending on external tools like Travis, you can also choose to use [AWS CloudFormation](https://aws.amazon.com/cloudformation/) to bootstrap the relevant AWS resources, integrated with [AWS CodeBuild](https://aws.amazon.com/codebuild/) and [AWS CodePipeline](https://aws.amazon.com/codepipeline/). Alternatively, deployment via [AWS CodeStar](https://aws.amazon.com/codestar/) might also be supported out of the box.
80+
Instead of depending on external tools like Travis CI, you can also choose to use [AWS CloudFormation](https://aws.amazon.com/cloudformation/) to bootstrap the relevant AWS resources, integrated with [AWS CodeBuild](https://aws.amazon.com/codebuild/) and [AWS CodePipeline](https://aws.amazon.com/codepipeline/). Alternatively, deployment via [AWS CodeStar](https://aws.amazon.com/codestar/) may also be supported out of the box.
7381

74-
To modify the build process, you can update the CodeBuild configuration file at [`buildspec.yml`](https://github.com/irvinlim/es2017-lambda-boilerplate/blob/master/buildspec.yml), or to modify any Lambda properties, you can update the CloudFormation configuration file at [`samTemplate.yml`](https://github.com/irvinlim/es2017-lambda-boilerplate/blob/master/samTemplate.yml).
82+
To modify the build process, you can update the CodeBuild configuration file at [`buildspec.yml`](https://github.com/irvinlim/es2017-lambda-boilerplate/blob/master/buildspec.yml). To modify the properties of the resultant Lambda function, you can update the CloudFormation configuration file at [`samTemplate.yml`](https://github.com/irvinlim/es2017-lambda-boilerplate/blob/master/samTemplate.yml).
7583

7684
If you are new to AWS CI/CD tools, you can follow the official [AWS tutorial](http://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html) to set up a build pipeline using CodePipeline. Take note of the following:
7785

@@ -86,24 +94,30 @@ You can write Lambda functions that make use of the [AWS SDK](https://github.com
8694

8795
Also make sure that your function has Internet connectivity (i.e. not within a VPC without a NAT gateway). The `internetConnectivityTest.js` utility is included to help to debug such problems early when deploying to AWS Lambda.
8896

89-
## Environment variables
97+
### Environment variables
9098

91-
The following environment variables are supported:
99+
If you plan to use the AWS SDK, either for deployment (using `npm run deploy`), or within your function itself, you need to pass the following environment variables:
92100

93101
* `AWS_ACCESS_KEY_ID`: IAM user access key ID
94102
* `AWS_SECRET_ACCESS_KEY`: IAM user secret access key
95-
* `AWS_REGION`: AWS region where the Lambda function resides in
96-
* `LAMBDA_FUNCTION_NAME`: Name or ARN of the Lambda function
103+
* `AWS_REGION`: AWS region where the Lambda function resides in (_required for SDK deployment only_)
104+
* `LAMBDA_FUNCTION_NAME`: Name or ARN of the Lambda function (_required for SDK deployment only_)
97105

98-
This will work if you store it in a `.env` file in the root of the project (see [dotenv](https://github.com/motdotla/dotenv)), or if you define it within Travis CI itself (see [Travis docs](https://docs.travis-ci.com/user/environment-variables/)).
106+
This will work if you store it in a `.env` file in the root of the project (see [dotenv](https://github.com/motdotla/dotenv)), or if you define it within Travis CI itself (see [Travis CI docs](https://docs.travis-ci.com/user/environment-variables/)).
99107

100-
## Why?
108+
### IAM user permissions
109+
110+
The minimum permissions required for the IAM user for SDK deployment are:
101111

102-
### Latest ES2017 features
112+
* [`lambda:UpdateFunctionCode`](https://docs.aws.amazon.com/lambda/latest/dg/API_UpdateFunctionCode.html)
113+
114+
Remember to add more permissions as required if you need to access the SDK in your function.
115+
116+
## Why?
103117

104-
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.
118+
### Latest ES2017/ES2016 features
105119

106-
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):
120+
The highest version of Node.js supported on AWS Lambda is 6.10.0, which supports only features up to ES2015/ES6. Newer features in ES2017, such as `async`/`await`, are incredibly useful when performing network requests, such as when used with the [AWS SDK](https://github.com/aws/aws-sdk-js):
107121

108122
```js
109123
const EC2 = new AWS.EC2();

bin/deploy.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/**
2-
* Simple script that deploys the zipped package to AWS Lambda.
2+
* Simple script that deploys the zipped package to AWS Lambda using the AWS SDK.
3+
*
4+
* Alternatively, you can use AWS CodeBuild + AWS CloudFormation to deploy the
5+
* function whenever you push to your repository.
36
*/
47

58
import AWS from 'aws-sdk';

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* Main AWS Lambda function source code.
3-
* The following is written using ES2017/ES8, and should be transpiled to ES6 before uploading to AWS Lambda.
2+
* Index for AWS Lambda function. The main source code should go under `main.js`.
3+
* Generated with [es2017-lambda-boilerplate](https://github.com/irvinlim/es2017-lambda-boilerplate).
44
*/
55

66
import internetConnectivityTest from './util/internetConnectivityTest';

src/main.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
/**
2-
* Feel free to modify your Lambda function here!
2+
* Main source code for your Lambda function, exposed via a default export.
3+
* Feel free to write your Lambda function in this file!
34
*/
45

56
import AWS from 'aws-sdk';
67
import request from 'jsonrequest';
78

8-
const router = (event, context) => {
9+
/**
10+
* Write your Lambda function code here.
11+
*
12+
* @param {object} event Lambda event object passed from the caller.
13+
* @param {object} context Lambda context object (see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html).
14+
* @returns {*} The return value will be passed to the caller.
15+
* @throws {Error} If an Error is thrown, the Lambda will be considered failed; the error object will be passed to the caller.
16+
*/
17+
export default (event, context) => {
18+
// Example router.
919
switch (event.type) {
1020
case 'rest-api-example':
1121
return restApiExample(event.data, context);
@@ -14,6 +24,8 @@ const router = (event, context) => {
1424
}
1525
};
1626

27+
// Example function which uses async/await to perform a request to a REST API.
28+
// This example uses the `jsonrequest` library, which supports Promises.
1729
const restApiExample = async (data, context) => {
1830
const { user, repo } = data;
1931

@@ -27,11 +39,10 @@ const restApiExample = async (data, context) => {
2739
return await request(options);
2840
};
2941

42+
// Example function which invokes the AWS SDK, utilising `.promise()` for async/await.
3043
const awsSdkExample = async (data, context) => {
3144
const EC2 = new AWS.EC2();
3245

3346
// Sample EC2 call.
3447
return await EC2.describeInstances().promise();
3548
};
36-
37-
module.exports = router;

test/example.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
2-
* Example test using Jest/Jasmine.
3-
*
4-
* Use run() to run your Lambda function within docker-lambda.
2+
* Example test using Jest/Jasmine. Run all tests using `npm run test`.
3+
* Use run() to invoke your Lambda function within a Docker container with docker-lambda.
54
*/
65

76
import run from './util/runner';

test/util/runner.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import dockerLambda from 'docker-lambda';
22
import dotenv from 'dotenv';
3-
import fs from 'fs';
43
import path from 'path';
54

65
// Read environment variables from .env file.

0 commit comments

Comments
 (0)