Serverless Framework with Node js

The Serverless framework allows you to build & deploy auto-scaling functions. It takes care of provisioning all the resources that you need. In this series we’ll focus on deploying Lambda functions to AWS (written in node.js)

  1. Introduction
  2. What is Lambda?
  3. Core concepts
  4. Installation and using the CLI
  5. Configure AWS credentials
  6. Creating first project
  7. Deploying and invoking functions
  8. Integrate with API Gateway
  9. Working with input
  10. Deploy other AWS resources
  11. Environment variables

Serverless Framework is your single toolkit for deploying serverless architectures to any provider. You build the features, we configure the infrastructure. Done.

The Serverless Framework is a CLI tool that allows users to build & deploy auto-scaling, pay-per-execution, event-driven functions.

Write your code, configure events to trigger your functions, then deploy & run those functions to your cloud provider via the Serverless CLI.

What is AWS Lambda?

AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume — there is no charge when your code is not running.

With Lambda, you can run code for virtually any type of application or backend service — all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.

Core concepts of AWS Lambda

Functions

Functions are small pieces of code that should only do 1 thing. They take a certain input, process it and deliver some output. The idea is to have a lot of tiny functions that make up a big application. This technique is often referred to as microservices.

Creating and managing a ton of funtions sounds like a lot of work, but don’t worry. Serverless will deploy all of them with a single command.

Events

Your functions can only run after certain events. On AWS, many services can trigger an event. For example: you could trigger a function whenever you upload a new file to a S3 bucket. To do that, you configure S3 to send an event to your Lambda function. In turn, your Lambda function could perform actions on the newly uploaded file (such as generating thumbnails for an image).

Not only S3 can send events to Lambda. Other services such as DynamoDB and API Gateway can do the same.

Using the Serverless framework, you can define all the events that can trigger your function in the serverless.yml file. When you deploy to AWS, the framework will automatically create all these events for you.

Resources

Resources refer to other AWS services that your application needs. For example: you might use DynamoDB to store details of your users. You might also need an S3 bucket for storing file uploads.

You could manually create each of these, but that would be time consuming and difficult to repeat. Instead you can define them in a single file (in serverless.yml) and Serverless will provision them for you.

Some benefits of using this technique include:

  • Serverless will make sure that your entire infrastructure is stable. It will automatically rollback changes if an error occurs during a deploy.
  • You can re-deploy to a new cloud any moment. You only have to run sls deploy when you want to deploy your app to another AWS account.

Services

A service is basically a group of functions, events and resources. If you’re building a backend for your website, then your service might be called “website-backend”. This service will contain many functions (eg: getBlogPost, getOverview, getAuthors, …) and other resources.

You can create 1 service for you entire project or you can create many services. This usually depends on how complex your application is.

Plugins

Plugins are pieces of code that can extend the functionalities of the framework. For example, there are plugins that can optimize your JavaScript code when you deploy or plugins that add support for TypeScript, …

Installing CLI Tool

Learn how to install the Serverless framework on your computer using npm. Afterwards we’ll go over it’s command line interface and how to use it to get help for all the different commands.

Before you begin, make sure that you have installed node.js on your machine. Afterwards, installing Serverless is very simple:

npm install -g serverless

To create a new node.js project for AWS, you should use:

sls create --template aws-nodejs

Note that sls is the same as serverless, it's just a shortcut.

Deploying your entire infrastructure with a single command:

sls deploy

Lets explore sample app configurations.

Whenever you deploy your app, Serverless will provision these resources through CloudFormation. It will also make sure that your application is always in a consistent state and will rollback changes if something goes wrong.

In this video I show you how to provision an S3 bucket and a DynamoDB table. This is the code snippet used in the video:

You can provision almost any resource on AWS this way. Want to deploy another resource that wasn’t shown in this video? Luckily Amazon has a bunch of examples on it’s website: AWS Resource Types Reference.

Like what you read? Give Tarun Sharma a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.