Tuesday, December 6, 2016

Create Serverless Microservices with Node.js and AWS Lambda

If you've ever wanted to write a web app or API without messing around with the server, Amazon's Lambda might be what you're looking for. AWS is a collection of developer tools that Amazon develops and publicly offers.

In this article we will get up and running with Lambda, a new tool in the Amazon Web Services (AWS) suite. We'll be using Lambda to create a HTTP GET endpoint that will make requests using the GitHub API to pull repository info from GitHub and return a JSON response.

To follow along with this article you will need an AWS account of your own. You can create a free AWS account at https://aws.amazon.com/

What is AWS Lambda?

The Lambda tag line is "Run Code without Thinking about Servers." At first glance this may sound confusing. Where or how does the code actually run then?

Serverless and Functions as a Service

"Serverless" is a new software infrastructure term you may have heard. It is used for describing solutions for on-demand code execution. The term "serverless" can be misleading because, in fact, there are still servers in the equation. A better descriptor is FaaS, or "Functions as a Service."

Both definitions are used to describe a new development and deployment experience. This new experience is considered "serverless" because you as a developer no longer have to manage, monitor, or scale any servers that are running your code. You just upload your code to a FaaS provider (AWS Lambda, in this case) and the FaaS provider executes it and manages any infrastructure for you behind the scenes.

The pros and cons of Serverless architecture

Given this expanded definition of the "Serverless" architecture, let's look at some of the pros and cons when working with Lambda.

Pros

  • On-demand usage pricing.

    Traditional server hosting uses a recurring billing cycle. Your server is always up and running using resources and waiting for input. You pay a monthly or yearly fee to keep it running for the duration of your billing cycle.

    Lambda uses on-demand pricing, where billing is calculated per function use. This means that if your project utilizes Lambda functions that are not being used in high demand, you can save a significant amount of money over traditional hosting solutions.

    Lambda pricing is as follows:

    • $0.20 per 1 million requests
    • $0.00001667 for every GB-second of compute time, with every execution rounded up to the nearest 100ms

    Learn more at: http://ift.tt/20XOJQ6

  • Built-in auto scaling

    In a traditional hosted infrastructure, there comes a time where you may need to worry about performance and scaling. As the traffic and usage of your application increase, you may need to add more hosted servers to your infrastructure to keep up with demand. This can cause failures and bottlenecks for your users. Lambda takes care of scaling silently when needed, removing additional cognitive overhead.

Cons

  • Inconsistent local development work flow.

    You can write Lambda function code locally and test it in isolation, but you won't be able to simulate a production environment locally without creating your own hacked-together version of Lambda.

Lambda Key Concepts

Function code and triggers

Lambda has two main concepts: code and triggers. Code is self-explanatory. In our case, it's the JavaScript code that you write and upload to Lambda to produce your desired behaviors.

Once uploaded, code will not execute on its own. Lambda has an additional concept called "triggers." Triggers are events fired by other AWS services that pass data to the Lambda function for execution.

Some example triggers are:

  • An HTTP request to AWS API Gateway fires lambda code
  • An event fired on an interval, like a cron job from CloudWatch Events
  • A DynamoDB table is updated and triggers Lambda code

Lambda code function signature

You define a Lambda function by exporting a regular function from JavaScript that matches the expected Lambda signature.

exports.myLambdaFunction = (event, context, callback) => {
   // Use callback() and return 
}

The function receives three arguments:

  1. event — A key-value pair dictionary of 'trigger data' that Lambda passes to the function.

  2. context — AWS internal information such as AWS request ID, Lambda expiration timeout, and Log info.
    For more info see: http://ift.tt/20gvIFy

  3. callback — A standard async JavaScript callback handler.
    For more info see: http://ift.tt/1qwAYdx

Building a Lambda Function

To get started creating a new Lambda function, visit the Lambda dashboard at:
http://ift.tt/2ghCIVD

Continue reading %Create Serverless Microservices with Node.js and AWS Lambda%


by Kev Zettler via SitePoint

No comments:

Post a Comment