CloudDevOpsServerless Applications

Serverless Framework features

Serverless Framework features

There are many features available in Serverless Framework, although they vary with the cloud provider.
I will list and describe a few of the more important and more common ones.

Services and deployment:

A service is a project where we define the functions, the events that trigger them, and any infrastructure resources that are required for the function to perform.
They are collected together into one file, which is called serverless.yml:

eg.
myServerlessService/
serverless.yml

When we start using Serverless Framework for deployment, we will be using one single service.
But as the application grows, it is recommended that you have multiple services as shown in the following code:

users/
serverless.yml # Contains 4 functions
posts/
serverless.yml # Contains 4 functions

Having multiple services can isolate the infrastructure resources that are to be used. But it also has a drawback, as currently each service creates a separate REST API on API Gateway.

To create the service, we have to use the create command, and we must pass the runtime language in which you would like to write the service. We can also provide the path, as shown in the following example:

$ serverless create --template <runtimes> --path myService 

The main purpose of Serverless Framework is to deploy functions, events, and infrastructure resources into the remote cloud without much hassle, and that is done through the deploy plugin. There are various features that this deploy plugin provides.
Below is a few of them:

  • Deploy to different stages and regions:
$ serverless deploy --stage production --region us-east-1 
  • Deploying single function from the service:
$ serverless deploy function <function_name> 
  • Deploying package to cloud:
$ serverless deploy --package <path to package> 

This deploy plugin works in the following ways:

  • The framework packages up the targeted AWS Lambda function into a .zip file
    null
  • The framework fetches the hash of the already uploaded function .zip file and compares it to the local .zip file hash
  • The framework terminates if both hashes are the same
  • That .zip file is uploaded to your S3 bucket using the same name as the previous function, which is the CloudFormation stack it is pointing to

Serverless Functions and events:

Functions are the properties that are defined within the service, and they are defined within the serverless.yml, so we name the function and provide the handler property to the function, and this property points to the function file, which could be Node.js or Python. We can add multiple functions within the property.
The functions can inherit the properties from the provider or we can define the properties at function level.
These function properties vary as per the cloud provider, as shown in the following code:

# serverless.yml
service: myService
provider:
  name: aws
  runtime: nodejs6.10
  memorySize: 512 # will be inherited by all functions
functions:
  usersAdd:
    handler: handler.userAdd
    description: optional description for your function
userModify:
    handler: handler.userModify
userDelete:
    handler: handler.userDelete
    memorySize: 256 # function specific

The serverless functions can be listed as arrays if we create a separate file for every function:

 # serverless.yml
 functions:
     - ${file(../user-functions.yml)}
   - ${file(../post-functions.yml)}
 # user-functions.yml
addUser:
     handler: handler.user
deleteUser:
     handler: handler.user

An environment object property can also be added to the function within the service, and it should be a key–pair value. Also, the function-specific environment variable overrides the provider-specific environment variable:

 # serverless.yml
service: service-name
provider: aws
 functions:
   hello:
       handler: handler.hello
       environment:
           TABLE_NAME: tableName

Events are things that trigger the function, such as the S3 bucket upload. There are multiple events supported by Serverless Framework, but they vary as per the cloud provider.
We can define multiple events for a single function, as shown in the following code:

events:
 - http:
    path: handler
    method: get

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Wp05 bpN 0E

Please type the text above:

Close
Close