Serverless Framework

Serverless Framework

About

  • Serverless Framework is open-source software that builds, compiles, and packages code for serverless deployment and then deploys the package to the cloud.

  • It Helps us to focus on Business Logic Rather than software and Hardware

  • Developers write Lambda functions, which are triggered by the client app, and user’s requests come through the API Gateway. AWS Lambda provides functions as a service

  • We usually wrap up our serverless script in serverless.yml

Installation

  • Prerequisites:

    • Nodejs

    • AWS: To run serverless commands that interface with your AWS account, you will need to setup your AWS account credentials on your machine.

    • To know more Click_Here

  • In Linux Follow the Below command to install serverless-framework

    npm install -g serverless
  • Verify Installation:

    serverless --version
  • The above command Will give you result similar to those below

    Framework Core: 2.20.1
    Plugin: 4.4.2
    SDK: 2.3.2
    Components: 3.5.1

Get Started with Serverless

An average serverless.yml will Look like the below

service: slsNewService

frameworkVersion: '2'

provider:
  name: aws
  runtime: python3.8
  lambdaHashingVersion: 20201221

functions:
  helloworld:
  	handler: handler.helloworld
    events:
        - httpApi:
        path: /users/create
        method: get

   environment:
     variable: value

resources:
 Resources:
   NewResource:
     Type: AWS::S3::Bucket
     Properties:
       BucketName: my-new-bucket

Create Project

You can Get started with the project by generating Templates

AWS-Python:

  • To generate AWS-Python Template for serverless , Use the below Command:

    serverless create --template aws-python3 --path MyNewService
  • The Above Command will generate below mentioned two files in MyNewService Directory :

    • serverless.yml

    • handler.py

Similarly For JS ...

AWS-JS:

  • To generate AWS-NodeJS Template for serverless, Use the below Command:

    serverless create --template aws-nodejs --path MyNewService
  • The Above Command will generate below mentioned two files in MyNewService Directory :

    • serverless.yml

    • handler.js

To know more about other templates Click_Here

Terms in-Detail:

  • service: It is simply the name of your project.

  • frameworkVersion: Framework Version

  • app & org for use with serverless_dashboard

  • provider : This section includes configurations related to cloud provider like aws, azure, GCP, etc., (We Talk about AWS Here)

  • You can use the package and exclude configuration for more control over the packaging process, to know more Click_Here

  • functions: Functions are AWS Lambda functions. You can find all options available on this Serverless documentation page.

  • resources: We can Add CloudFormation resource templates to this

Deploy a Service

So to deploy the service to AWS, use the below Command

serverless deploy

Remove a Service

In order to remove a service that we've deployed, we can use Below Command

serverless remove

Key_Points

  • Permissions: If your lambda needs to access other AWS services (S3, SQS, SNS…), you will need to add the proper permissions via the provider>iamRoleStatements , To know more Click_here

References

Last updated