Serverless Framework Tutorial for Beginners Using AWS Lambda

serverless framework tutorial for beginners

Serverless architecture is gaining popularity and it is been used by many organizations for all its operational tasks. Ther are many companies which use serverless services like Lamba for its microservices architecture.

AWS, Google Cloud, and Azure provide good web portal, CLI, and SDK for their respective serverless services. However, When you go through the CI/CD process, you need a good framework other than CLI’s and SDKs for good traceability and management. Here is where serverless framework comes into play. provides a framework for deploying serverless code to AWS Lambda, Google Cloud Functions and Azure Functions. You can organize your serverless deployment using configuration files provided by this framework.

We have done a basic deployment on AWS Lambda using this framework and we loved it. This guide will help you to get started with the serverless framework on AWS Lambda.

Installation and Configuration

[alert-warning]You should have awscli installed and configured on your system. Serverless framework will use the ~/.aws/credentials file for deploying lambdas[/alert-warning]

1. Install node js. Follow steps from here from here — > Latest Nodejs Installation

2. Install the serverless framework.

npm install -g serverless

3. Check the installation using the following command.


Getting Started

Let’s get started with a demo python application.

Step1: cd in your project directory. You can use any folder of your choice.

Step 2:. Create a basic python service template. It will create a python based serverless template.

serverless create --template aws-python --path devopscube-demo

The output would look like the following.

Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "/serverless-demo/devopscube-demo"
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                 , v1.15.3

Serverless: Successfully generated boilerplate for template: "aws-python"

It will create the following folder structure

|-- devopscube-demo
|   |--
|   |-- serverless.yml

Step 3: cd into devopscube-demo This folder would contain two files named and serverless.yml. file contains a python code that will be deployed to lambda. serverless.yml contains the configuration which tells the serverless framework about how and what events it should associate with the given lambda function. You can specify the function name in serverless.yml file. In our case its the default hello

cd devopscube-demo

Step 4Lets deploy the basic service to AWS. This would create all the basic configurations on AWS for deploying the lambda function. (IAM Role, S3 bucket to keep the artifact, Cloudformation Template for Lambda and AWS log group for Lambda logs.)

serverless deploy -v

In your AWS dashboard, you can see the created lambda function.

Step 5: Now, let’s invoke our basic python function.

serverless invoke -f hello -l

You will see a successful execution with the following output

    "body": "{\"input\": {}, \"message\": \"Go Serverless v1.0! Your function executed successfully!\"}",
    "statusCode": 200
START RequestId: ebb938e0-5803-11e7-825b-8f51036d398a Version: $LATEST
END RequestId: ebb938e0-5803-11e7-825b-8f51036d398a
REPORT RequestId: ebb938e0-5803-11e7-825b-8f51036d398a	Duration: 0.26 ms	Billed Duration: 100 ms 	Memory Size: 1024 MB	Max Memory Used: 19 MB

Step 5: Now let’s remove the default python program and add our own program which returns the sum of two numbers. Our new would look like the following.

  import json

  def hello(event, context):
      a = 1
      b = 4
      return a+b

Step 6: Since we have updated our function, we need to deploy the function again for updating the lambda for new code. let’s deploy our code using the following command.

serverless deploy function -f hello

Once deployed, you can invoke the function again using the invoke command.

serverless invoke -f hello -l

Adding More Function

You can add more functions to your existing template. Here is what you have to do.

1. Create a python file named in your project directory where you have the file.

2. Add the following code to the file.

  import json

  def demo(event, context):
      body = {
          "message": "Go Serverless v1.0! Your function executed successfully!",
          "input": event

      response = {
          "statusCode": 200,
          "body": json.dumps(body)

      return response

      # Use this code if you don't use the http event with the LAMBDA-PROXY integration
      return {
          "message": "Go Serverless v1.0! Your function executed successfully!",
          "event": event

The above code contains the python definition called demo. In our first example, it was hello.

2. Add a new function called demo-function in the serverless.yml file along with your first hello function. Your function definition would look like the following.

      handler: handler.hello
      handler: testing.demo

3. Now that we have added a new handler and a function, we should redeploy the service.

serverless deploy -v

3. Once deployed, invoke our new function.

serverless invoke -f demo -l

4. If you edit the code, you can update and re-invoke it using the following command.

serverless deploy function -f demo
serverless invoke -f demo -l

There are more functionalities in this framework. If you are using or thinking about using AWS Lambda, Google Cloud Functions or Azure functions, you should definitely try out the serverless framework. In this serverless framework tutorial, we have just scratched the surface. We will be covering more topics on the serverless framework.

Leave a Reply

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

You May Also Like