A First Look at the Serverless Framework
Published:
The Serverless Framework consists of an open source CLI and a hosted dashboard. We will create a boilerplate Node Lambda handler and deploy it to AWS.
Outline
All of this project’s code can be found in the First Look monorepo on my GitHub.
Introduction
The Serverless Framework consists of an open source CLI and a hosted dashboard. Together, they provide you with full serverless application lifecycle management. It helps you develop and deploy your AWS Lambda functions, along with the AWS infrastructure resources they require.
History
It was created by Austen Collins in 2015 as a boilerplate project for a company or something called Servant, whose existence seems to be completely wiped from the internet aside from this git commit.
Servant Boilerplate LADEN: AWS Lambda, Angular, AWS DynamoDB, Express, Node.js
A Servant Boilerplate Application already integrated with Servant (
https://www.servant.co
) built on the LADEN stack. Use this to rapidly build Servant applications. This stack in particular is perfect for integrations or any applciation that needs to offload intense processing tasks to AWS Lambda. We use this for our apps! – The Servant TeamThis boilerplate is current under development…
When it was officially announced the project was called JAWS before being renamed to Serverless after the newly formed Serverless Inc.
One of the projects I had recycled the branding from was the JavaScript AWS framework. I’m a big JavaScript fan and at the time there just wasn’t a good application framework for AWS. Something that could help developers be productive on AWS.
The Story of the Serverless Framework with Austen Collins (September 14, 2020)
Core Concepts
The Framework has four main concepts. Here is how they pertain to AWS and Lambda.
Functions
A Function is an AWS Lambda function. It’s an independent unit of deployment, like a microservice. It’s merely code, deployed in the cloud, that is most often written to perform a single job such as:
- Saving a user to the database
- Processing a file in a database
- Performing a scheduled task
Events
Anything that triggers a Lambda Function to execute is regarded as an Event. Events are infrastructure events on AWS such as:
- API Gateway HTTP endpoint request for a REST API
- S3 bucket upload for an image
- CloudWatch timer run every 5 minutes
- SNS message topic
When you define an event for your Lambda functions, the Framework will automatically create any infrastructure necessary for that event such as an API Gateway endpoint and configure your AWS Lambda Functions to listen to it.
Resources
Resources are AWS infrastructure components which your Functions use such as:
- DynamoDB Table for saving Users/Posts/Comments data
- S3 Bucket for saving images or files
- SNS Topic for sending messages asynchronously
- Anything defined in CloudFormation
The Serverless Framework deploys your Functions and the Events that trigger them, along with the AWS infrastructure components your Functions depend upon.
Services
A Service is the Framework’s unit of organization. You can think of it as a project file, though you can have multiple services for a single application. It’s where you define:
- Your Functions
- The Events that trigger them
- The Resources your Functions use
Create a Project
Install the Serverless CLI
In our serverless.yml
file we have a nodejs12
runtime and a single handler
named hello
.
Function Handler
handler.js
returns a JSON object containing a message
demanding less servers.
Serverless Deploy
The sls deploy
command deploys your entire service via CloudFormation. Run this command when you have made infrastructure changes such as editing serverless.yml
.
Serverless Invoke
The sls invoke
command invokes deployed function. It allows sending event data to the function, reading logs and displaying other important information about the function invocation.