Ishanka Ranatunga

Software Engineer | Tech Geek

A quick guide to Serverless Framework with AWS Lambda

In my previous article, I talked about what Serverless is. In case you missed it, I suggest you to read it in here. 😄

So in this article I’m going to tell how to create a simple serverless function with AWS Lambda in a simple way. 😉 If you are familiar with AWS Lambda Console, you know the pain of creating and managing the Lambda function. Fortunately there are now so many frameworks that we can use to reduce our workload. When you start to use a framework you will know how simple it is.

Let me list down some of the framework that we can use.

From all of the above, I have been using the Serverless Framework for like a year to deploy my Lambdas. I found it’s really easy to use and configure. Also, there are lot a plugins for that framework by its’ great community. 🙇‍♂️

ezgif-6-c9b3ee086989.gif

If you want to use another provider instead of AWS, it’s really easy to migrate. You just need to modify few lines and you are good to go. It’s that simple. 😉

So, in this article I’m going to show you how to create and deploy a simple NodeJs Lambda function with Serverless Framework. Let’s get started!

Firstly, we need to install the serverless framework using the following command.

npm install -g serverless

After installing the npm package, we can either use serverless or sls which are equivalent commands.

Now we need to configure the AWS IAM User credentials for Serverless Framewrok. To do that run the following command. If you don’t know how to get those, you can refer this guide.

serverless config credentials --provider aws --key AWS_ACCESS_KEY --secret AWS_SECRET

To create a serverless project, they have given us boilerplates for each language and the providers they support. You can find all of them here. In this case we are going to initiate the project with the following command for NodeJs.

serverless create --template aws-nodejs --path simple-lambda

Now we have two files named handler.js and serverless.yml inside the directory. The handler.js file basically contains our lambda function code. Here we can add more functions as much as we need for our project.

The serverless.yml is where the magic happens. This file contains the configuration of our project where it defines the details of the service provider, environment variables,  functions, resources, serverless plugins etc.

For every function that you write in the handler.js file, you need to configure here. Basically that configurations are responsible for creating API Gateway routes for the functions.

So what if we want to test the code that we wrote is working? For that we can use the following command;

serverless invoke --function hello --log

This command will run our hello function and give us the logs.

Okay, then what about if we need a local API for all the functions?

This is where a plugin comes handy. We can use a plugin called serverless-offline which will help us to run a local API for our project. Installing a plugin is simple. You just need to install the npm package and add the plugin name to the serverless.yml.

npm install serverless-offline

After adding the plugin to the serverless.yml, it should look like this;

Now you can run the following command to create the local API for your project.

serverless offline start

Response:
Serverless: Starting Offline: dev/us-east-1.
Serverless: Routes for hello:
Serverless: GET /
Serverless: POST /{apiVersion}/functions/simple-lambda-dev-hello/invocations

Serverless: Offline [HTTP] listening on http://localhost:3000
Serverless: Enter "rp" to replay the last request

Now you can make a GET request to http://localhost:3000/ and see the response.

Once we are done with testings, we can use the following command to deploy the our code to AWS.

serverless deploy

Response:
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service simple-lambda.zip file to S3 (386 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
...........................
Serverless: Stack update finished...
Service Information
service: simple-lambda
stage: dev
region: us-east-1
stack: simple-lambda-dev
resources: 9
api keys:
  None
endpoints:
  GET - https://oujlmpmbt1.execute-api.us-east-1.amazonaws.com/dev
functions:
  hello: simple-lambda-dev-hello
layers:
  None

This will deploy all the functions, resources, events that you defined in the serverless.yml.

Yey! Now you have an endpoint for your deployed Lambda function.

tenor

If you want to see the logs of your deployed Lambda function you can use;

serverless logs --function hello

Response:
START RequestId: 7ffe81b5-3221-46cd-aaab-61ce7921148e Version: $LATEST
END RequestId: 7ffe81b5-3221-46cd-aaab-61ce7921148e
REPORT RequestId: 7ffe81b5-3221-46cd-aaab-61ce7921148e  Duration: 2.46 ms       Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 59 MB  Init Duration: 1.68 ms

Visit the official docs if you want to find out more about the logs.

Okay, what if you want to remove the functions which you deployed? Again, it’s just one simple command.

serverless remove

Response:
Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack removal progress...
..........
Serverless: Stack removal finished...

Look how simple was that. Now you can really focus on your code and make your life easier with AWS Lambda.

That’s all folks. Happy Coding! ☺️

A Short Introduction to Serverless & FaaS

Hey folks 👋, it has been a while since my last post 👉 An Introduction to NoSQL.  Recently, I had to revise some of the things that I have learned about in past few months. One of them is the Serverless Architecture. So I decided to write a post about it while I’m revising. 😁

Serverless?

Serverless is a hot 🔥 trend in cloud computing. The serverless architecture is not actually “serverless” though. .
tenorYes. It is just the name. 😉 Serverless does not mean that there are no servers involved. It simply means, the developers don’t have to think about server management and low-level infrastructure. The cloud provider will be responsible for managing resources, provisioning, patching and make servers available for the tasks. So that the developer can focus only in coding. 👨‍💻1_dAnZk19kGszKTvAgag31sQ.jpeg

In Serverless Architecture, applications significantly depend on third-party services known as Backend-as-a-Service (BaaS) or on custom code that’s run in ephemeral containers  known as Function-as-a-Service (FaaS).

  • BaaS :
    Applications that depend on 3rd party applications/services to execute a certain amount of the application’s logic and state.
    eg: Auth0, Firebase
  • FaaS :
    Applications that run in stateless compute containers that can be triggered via events.
    eg: AWS Lambda, Google Cloud Functions

Basically, FaaS is about running back-end code without managing your own server. Here instead of writing applications, we can write piece of applications with event rules which trigger our code when needed. So that you’ll be billed only for the fraction of a second, measuring resource usage. 💰

I have worked with PaaS solutions like, Heroku. As the PaaS providers promised they have offered us the freedom of managing servers but they don’t have great auto scaling, so we have to still think about it. 🤔 Also PaaS is designed for long-running applications. So that the application is running always to serve the incoming requests. In FaaS, our function begins to serve a request and terminated after it is processed. That means, when there are no requests, our functions should not consume the resources.

But there is a issue, ☝ not all workload can be converted to a event triggered model and neither can all code be separated by it’s dependencies and some of them may require intensive installation and configurations. So we should keep in mind that FaaS cannot be used in every situations.

Here are some use cases of FaaS; 👍
-Backends
-Web Apps
-Scheduled Tasks
-Chat Bots
-IoT Services

Let’s list down some benefits of Serverless approach 👇

  • In serverless, you are only charged for the time it took the function to run. So we don’t have to pay for the idle time.
  • Serverless gives auto scaling without involving us. Depending on the load cloud provider will scale up function instances.
  • We can use different language runtime according to the use case
  • Developers can focus on coding without worrying about the server management

There are drawbacks also. Below are some of them 👇

  • Serverless can also add complexity rather than reducing it.
  • Potentially hard to debug
  • Hard to work with different services : using Firbase instead of DynamoDB
  • Cold start : It takes some time for a scalable serverless platform to handle the first request by a function

Serverless architecture allows us to write piece of applications with event rules which trigger our code when needed and run quickly without using lot of server resources. But it doesn’t mean that FaaS is usable only in small scenarios. Although a function is a small unit, it can be invoked millions of times per second. The question is what functionalities we should move from our application to functions and how to do it without making our application complex. 💭