Serverless Framework Tutorial for Beginners Using AWS Lambda

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.

Serverless.com 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

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

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

2. Install the serverless framework.

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.

The output would look like the following.

It will create the following folder structure

READ  How To Install and Configure GUI for Amazon EC2 RHEL 7 Instance

Step 3: cd into devopscube-demo This folder would contain two files named handler.py and serverless.yml. handler.py 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

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.)

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

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

You will see a successful execution with the following output

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

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.

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

READ  How To Setup GUI For Amazon EC2 Linux Instance

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 demo-function.py in your project directory where you have the handler.py file.

2. Add the following code to the file.

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.

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

3. Once deployed, invoke our new function.

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

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.

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.

Leave a Reply

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