Serverless Framework – Getting Started

The Serverless Framework is a free and open-source web framework written using Node.js. It is a CLI project which let you generate template projects base on various programming languages and deploy the project to cloud environment base on the configuration done in YMAL file. It supports AWS, Microsoft Azure, Google Cloud, and a few other cloud providers as well.
You computer need to have NodeJs setup before follow below steps.
Environment Setup
First of all, let’s install Serverless CLI project on your computer. Following command will install in global and anytime you can use this in command prompt or terminal.
npm install -g serverless
AWS Profile Configuration
For this sample, the project deploys into AWS, so before creating a Serverless project you need to configure AWS access token and secret into AWS credential profile so that in command can be used to connect to AWS.
You can get more details on this in following URL.
https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_profiles.html
Projects Setup
First, create a directory for the project and cd into it. Then run the following command to start a new project.
serverless create --template aws-nodejs
or
sls create --template aws-nodejs
serverless create
is the command to start a new project we can use --template
to select a template and aws-nodejs
is the template chosen for this tutorial.
The command will create three files .gitignore for source control. serverless.yml is the central configuration file for the whole project. Finally, the handler.js file includes the functions.
In serverless.yml, you can see functions section where hello method points to handler.hello in handler.js file. This will be the main entry point to lambda execution.
functions: hello: handler: handler.hello
'use strict'; module.exports.hello = async event => { return { statusCode: 200, body: JSON.stringify( { message: 'Go Serverless v1.0! Your function executed successfully!', input: event, }, null, 2 ), }; };
Then run the following command to create the package.json file
npm init
Now you can add npm library into the project using npm install command and add required logic into handler.js file.
Running localy
If your function has a basic implementation. yea this command helps you to run in your development machine. You can use the below command to run in local. Before that, you need to install serverless-offline
npm package and add two lines to your YML file.
npm install serverless-offline --save-dev

Now in your terminal, you can run the following command to start your serverless project in offline mode.
serverless offline start
In this case, lambda is configured to run every 10 minutes. you can see Succesfully invoked scheduled function: hello
printed in your console.
Deep dive into serverless.yml

If you look at above sample YML file, you can see 3 sections mark.
Provider section is a general section for an entire service project and where you can define runtime version, stages e.g Dev, QA, Prod, region where this service going to deploy and environment variable common for all lambda defined in this service project. Also, you can configure memorySize and timeout period for service.
In the function section, you can define how many lambdas are being created in this service project and for each lambda, the handler will be the main entry point. event section defines the main triggering point, in this case, it’s a scheduled event like Cron task and Also supports S3, API Gateway, SNS, and SQS many more.
Also in this YML file supports to create custom environment variable, base on command parameter you can dynamically inject to every property define in the file.
Imaging you have two stages, Dev and Prod
custom: envVariables: dev: API_KEY: "" prod: API_KEY: ""
in your YML file, now you can access custom-defined variables by using the below way.
API_KEY: ${self.custom.envVariables.${opt.stage}.API_KEY}
So when you deploying serverless project you have to pass stage parameter into it so that with -s dev
.
Deploying Serverless Project
Using following command you can deploy this project to AWS.
serverless deploy
This will pick the default AWS credential profile configure in your computer and deploy to that account. if you want to choose different AWS account you have to pass the profile name with –aws-profile parameter.
sls deploy --aws-profile Test
Adding -v
will give you a detailed view of deployment steps and that can help you to trace back to any issue that happens on deployment.
sls deploy -v
Summarise
The serverless framework is an awesome tool that gives you everything to create scalable, and reliable applications with any cloud provider. And serverless plugin helps you create common tools like Step Functions, Logs tracker, which can be shared with others.