All Projects → irvinlim → Es2017 Lambda Boilerplate

irvinlim / Es2017 Lambda Boilerplate

Licence: mit
AWS Lambda boilerplate for Node.js 6.10, adding ES2018/7/6 features, Docker-based unit testing and various CI/CD configurations

Programming Languages

javascript
184084 projects - #8 most used programming language
es2017
19 projects
es2016
15 projects

Projects that are alternatives of or similar to Es2017 Lambda Boilerplate

Serverless Sharp
Serverless image optimizer for S3, Lambda, and Cloudfront
Stars: ✭ 102 (-39.64%)
Mutual labels:  serverless, aws-lambda, lambda
Zappa
Serverless Python
Stars: ✭ 11,859 (+6917.16%)
Mutual labels:  serverless, aws-lambda, lambda
Node Lambda Log
Basic logging mechanism for Node 6.10+ Lambda Functions
Stars: ✭ 115 (-31.95%)
Mutual labels:  serverless, aws-lambda, lambda
Awesome Aws
A curated list of awesome Amazon Web Services (AWS) libraries, open source repos, guides, blogs, and other resources. Featuring the Fiery Meter of AWSome.
Stars: ✭ 9,895 (+5755.03%)
Mutual labels:  serverless, lambda, aws-sdk
Flogo
Project Flogo is an open source ecosystem of opinionated event-driven capabilities to simplify building efficient & modern serverless functions, microservices & edge apps.
Stars: ✭ 1,891 (+1018.93%)
Mutual labels:  serverless, aws-lambda, lambda
Lambstatus
[Maintenance mode] Serverless Status Page System
Stars: ✭ 1,323 (+682.84%)
Mutual labels:  serverless, aws-lambda, lambda
Serverless Sam
Serverless framework plugin to export AWS SAM templates for a service
Stars: ✭ 143 (-15.38%)
Mutual labels:  serverless, aws-lambda, lambda
Serverless Node Simple Messaging
Simple email AWS lambda function
Stars: ✭ 75 (-55.62%)
Mutual labels:  serverless, aws-lambda, lambda
Spark On Lambda
Apache Spark on AWS Lambda
Stars: ✭ 137 (-18.93%)
Mutual labels:  serverless, aws-lambda, lambda
Aws Lambda List
A list of hopefully useful AWS lambdas and lambda-related resources.
Stars: ✭ 130 (-23.08%)
Mutual labels:  serverless, aws-lambda, lambda
Lambcycle
🐑🛵 A declarative lambda middleware with life cycle hooks 🐑🛵
Stars: ✭ 88 (-47.93%)
Mutual labels:  serverless, aws-lambda, lambda
Laravel Bridge
Package to use Laravel on AWS Lambda with Bref
Stars: ✭ 168 (-0.59%)
Mutual labels:  serverless, aws-lambda, lambda
Aws Serverless Airline Booking
Airline Booking is a sample web application that provides Flight Search, Flight Payment, Flight Booking and Loyalty points including end-to-end testing, GraphQL and CI/CD. This web application was the theme of Build on Serverless Season 2 on AWS Twitch running from April 24th until end of August in 2019.
Stars: ✭ 1,290 (+663.31%)
Mutual labels:  serverless, aws-lambda, lambda
Serverless Next.js
⚡ Deploy your Next.js apps on AWS Lambda@Edge via Serverless Components
Stars: ✭ 2,977 (+1661.54%)
Mutual labels:  serverless, aws-lambda, lambda
Lambda Refarch Webapp
The Web Application reference architecture is a general-purpose, event-driven, web application back-end that uses AWS Lambda, Amazon API Gateway for its business logic. It also uses Amazon DynamoDB as its database and Amazon Cognito for user management. All static content is hosted using AWS Amplify Console.
Stars: ✭ 1,208 (+614.79%)
Mutual labels:  serverless, aws-lambda, lambda
Serverless Layers
Serverless.js plugin that implements AWS Lambda Layers which reduces drastically lambda size, warm-up and deployment time.
Stars: ✭ 119 (-29.59%)
Mutual labels:  serverless, aws-lambda, lambda
Up
Up focuses on deploying "vanilla" HTTP servers so there's nothing new to learn, just develop with your favorite existing frameworks such as Express, Koa, Django, Golang net/http or others.
Stars: ✭ 8,439 (+4893.49%)
Mutual labels:  serverless, aws-lambda, lambda
Serverless Node Simple Image Resize
Simple image resize AWS lambda function
Stars: ✭ 74 (-56.21%)
Mutual labels:  serverless, aws-lambda, lambda
Iopipe Js Core
Observe and develop serverless apps with confidence on AWS Lambda with Tracing, Metrics, Profiling, Monitoring, and more.
Stars: ✭ 123 (-27.22%)
Mutual labels:  serverless, aws-lambda, lambda
Portkey
Live-coding the Cloud
Stars: ✭ 139 (-17.75%)
Mutual labels:  serverless, aws-lambda, lambda

es2017-lambda-boilerplate

es2017-lambda-boilerplate

Travis CI David David Greenkeeper badge GitHub The MIT License

Note: As of April 2018, AWS has announced support for Lambda functions on Node.js 8.10, which already supports all ES2018-2016 features added by this boilerplate (and more). You can however, still use this boilerplate on Node.js 8.10 to make use of the unit testing features.


This is a boilerplate for AWS Lambda Node.js 6.10.0 functions, which allows you to use the latest JavaScript ES2016, ES2017 and ES2018 features. The boilerplate also allows you to test your function in a Docker container (thanks to docker-lambda), and also includes common configurations for CI/CD, for both Travis CI and AWS CodeBuild + AWS CloudFormation.

Latest JavaScript features

This boilerplate adds support for the following most commonly used JavaScript features that are not natively supported on Node.js 6.10:

Feature Supported?
ES2016/ES7
Exponentiation operator (**)
Array.prototype.includes
ES2017/ES8
Object.values, Object.entries
Trailing commas in function syntax
async/await
ES2018/ES9
Object rest/spread properties

Note: Most ES2015/ES6 are earlier features are supported.

Usage

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

npm run build

This will transpile your functions down to ES5 using Babel, so that it can be executed using the Node.js 6.10.0 runtime.

For convenience, the following command will create an artifact.zip file which you can upload to AWS Lambda:

npm run package

Testing

You can run automated tests for your Lambda function inside of a Docker container using docker-lambda:

npm run test

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 to get you started.

The test runner used is Jest (with Jasmine). Using docker-lambda also requires Docker to be installed on your host; see the docs for docker-lambda for more instructions.

Specification tests

In order to ensure that the Babel configuration works and is following the spec, the boilerplate also runs several automated tests to catch any Babel misconfigurations.

  • Functional testing: Runs the relevant spec tests from Test262 (actual tests taken from node.green) on docker-lambda to mock the AWS Lambda environment
  • Snapshot testing: Unit testing strategy by storing snapshots of Babel-transformed source code and running unit tests against them

You can find the spec tests under spec/functional and spec/snapshot respectively.

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.

Deployment

Deployment using the AWS SDK

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:

npm run deploy

See Environment variables for the list of environment variables that are required for SDK deployment.

Deployment through CloudFormation + CodeBuild

Instead of depending on external tools like Travis CI, you can also choose to use AWS CloudFormation to bootstrap the relevant AWS resources, integrated with AWS CodeBuild and AWS CodePipeline. Alternatively, deployment via AWS CodeStar may also be supported out of the box.

To modify the build process, you can update the CodeBuild configuration file at buildspec.yml. To modify the properties of the resultant Lambda function, you can update the CloudFormation configuration file at samTemplate.yml.

If you are new to AWS CI/CD tools, you can follow the official AWS tutorial to set up a build pipeline using CodePipeline. Take note of the following:

  • Set up a S3 bucket for uploading CodeBuild artifacts to.
    • If the CodeBuild build fails, you may need to set the S3_BUCKET environment variable within CodeBuild directly.
  • Ensure that the IAM roles have the necessary permissions to access required resources, including the S3 bucket.
  • The CloudFormation template filename under CodePipeline settings should be template.yml.

Using the AWS SDK

You can write Lambda functions that make use of the AWS SDK by simply import-ing aws-sdk. The package is installed globally within the AWS Lambda environment, so you don't need to add it to your package.json.

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.

Environment variables

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:

  • AWS_ACCESS_KEY_ID: IAM user access key ID
  • AWS_SECRET_ACCESS_KEY: IAM user secret access key
  • AWS_REGION: AWS region where the Lambda function resides in (required for SDK deployment only)
  • LAMBDA_FUNCTION_NAME: Name or ARN of the Lambda function (required for SDK deployment only)

This will work if you store it in a .env file in the root of the project (see dotenv), or if you define it within Travis CI itself (see Travis CI docs).

IAM user permissions

The minimum permissions required for the IAM user for SDK deployment are:

Remember to add more permissions as required if you need to access the SDK in your function.

Why?

Reduce callback hell with async/await

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:

const EC2 = new AWS.EC2();
const Route53 = new AWS.Route53();

// Get instance by ID.
const instances = await EC2.describeInstances({ InstanceIds: 'i-abcdef01' }).promise();

// Get public IP address.
const publicIpAddress = instances.Reservations[0].Instances[0].PublicIpAddress;

// Do something else with the IP address...
await Route53.changeResourceRecordSets({
    // ...
}).promise();

Run automated tests locally/through CI

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):

import run from './util/runner';

it('should work', function() {
    // Sample event from SNS.
    const event = {
        Records: [
            {
                EventVersion: '1.0',
                EventSource: 'aws:sns',
                Sns: {
                    MessageId: '95df01b4-ee98-5cb9-9903-4c221d41eb5e',
                    Message: 'Hello from SNS!',
                    ...
                },
            },
        ],
    };

    // Run the Lambda function against this event.
    const result = run(event);

    expect(result).toEqual(true);
});

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!

Acknowledgements

This boilerplate was first inspired from this post by Jesse Cascio.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].