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)
- What is Lambda?
- Core concepts
- Installation and using the CLI
- Configure AWS credentials
- Creating first project
- Deploying and invoking functions
- Integrate with API Gateway
- Working with input
- Deploy other AWS resources
- 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 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.
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 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 deploywhen you want to deploy your app to another AWS account.
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.
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
sls is the same as
serverless, it's just a shortcut.
Deploying your entire infrastructure with a single command:
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.