in aws lambda serverless ~ read.

Introduction to AWS Lambda using Serverless

In this post we are going to talk about how to use AWS Lambda function with Serverless.

What is AWS Lambda?

You can think of AWS Lambda as a way remove the trouble of maintaining your server while focusing on the unique business logic that's relevant to your code. Instead of spending your time trying to maintain the server which AWS is already doing for you.

Why do I need to use AWS Lambda?

Because the following reasons.

  1. Don't need to worry about your server maintenance anymore.
  2. Easier to scale up within AWS console.
  3. Way lot cheaper
  4. More time in your actual business logic code.

What is Serverless?

Serverless is a open source Framework to handle the deploy, modification of your lambda functions more conviniently.

Why is Serverless?

Because the following reasons.

  1. Programmers are just too dam lazy.

Demo

In the following we are going to start from scratch to create a Nodejs app that will say hello to us using AWS Lambda function.

  1. Create a AWS account in their website.

  2. Let's install server by issuing the following command.

npm install -g serverless

  1. Next let's create a Nodejs template by issuing the following command.

serverless create -t aws-nodejs

  1. Next let's have a look at the news we just created.
'use strict';

module.exports.hello = (event, context, callback) => {  
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,
    }),
  };

  callback(null, response);

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};

Here the template gives us an example function called hello that will be run each time a certain route has been hit.

Let's look at the other file.

service: aws-nodejs

provider:  
  name: aws
  runtime: nodejs4.3

functions:  
  hello:
    handler: handler.hello
    events:
      - http:
          path: users/create
          method: get

Here I've removed some noisy. Here as you can see that we have a events attribute under where we've got a http route with a path of users/create which means each time we hit this route, the hello() function will be fired.

  1. Next before we deploy the code, we need to add the configuration. Head over to your AWS console to get your credential like the following.

  1. Next run the following command to add it to Serverless

serverless config credentials --provider aws --key YOUR_KEY --secret __YOUR_SECRET

  1. Next you can deploy the function to AWS by running the following command.

serverless deploy

  1. It will return a url where once been hit will invoke the function hello().

End

So there it is your really crash course of how to use AWS Lambda serverless functions. As always if you have any opinions or ideas please leave it below, Thanks.

comments powered by Disqus