DEV Community

Cover image for A First Look at the Serverless Framework
ajcwebdev
ajcwebdev

Posted on • Updated on • Originally published at ajcwebdev.com

A First Look at the Serverless Framework

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 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 Team

This boilerplate is current under development...

Austen Collins - Serverless initial commit (April 20, 2015)

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

npm install -g serverless
serverless
Enter fullscreen mode Exit fullscreen mode
 What do you want to make? AWS - Node.js - Starter
 What do you want to call this project? aws-node-project

Downloading "aws-node" template...

Project successfully created in 'aws-node-project' folder.

 What org do you want to add this to? ajcwebdev
 What application do you want to add this to? [create a new app]
 What do you want to name this application? starters

Your project has been setup with org: "ajcwebdev" and app: "starters"
Enter fullscreen mode Exit fullscreen mode

In our serverless.yml file we have a nodejs12 runtime and a single handler named hello.

# serverless.yml

org: ajcwebdev
app: starters
service: aws-node-project

frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221

functions:
  hello:
    handler: handler.hello
Enter fullscreen mode Exit fullscreen mode

Function Handler

handler.js returns a JSON object containing a message demanding less servers.

// handler.js

'use strict'

module.exports.hello = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      { message: 'But could we have even LESS servers?', input: event }, null, 2
    ),
  }
}
Enter fullscreen mode Exit fullscreen mode

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 deploy
Enter fullscreen mode Exit fullscreen mode
Serverless: Using provider credentials, configured via dashboard:
https://app.serverless.com/ajcwebdev/apps/starters/aws-node-project/dev/us-east-1/providers

Service Information

service: aws-node-project
stage: dev
region: us-east-1
stack: aws-node-project-dev
resources: 8
api keys:
  None
endpoints:
  None
functions:
  hello: aws-node-project-dev-hello
layers:
  None

Serverless: Publishing service to the Serverless Dashboard...

Serverless: Successfully published your service to the Serverless Dashboard:
https://app.serverless.com/ajcwebdev/apps/starters/aws-node-project/dev/us-east-1
Enter fullscreen mode Exit fullscreen mode

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.

serverless invoke --function hello
Enter fullscreen mode Exit fullscreen mode
Serverless: Using provider credentials, configured via dashboard:
https://app.serverless.com/ajcwebdev/apps/starters/aws-node-project/dev/us-east-1/providers

{
  "statusCode": 200,
  "body": "{
    \n  \"message\": \"But could we have even LESS servers?\",
    \n  \"input\": {}\n}"
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)