In recent years of application hosting, the ideology of paying for the computational resources used rather than paying for the servers has gained popularity. Tools like the Serverless Framework have become immensely popular. Serverless is a framework that helps deploy applications in a Server-independent environment. Programming languages are typically used to create such applications and Python is often on top of this list for creating these web-based applications. 

Python is a high-level programming language with many packages that helps in creating Applications and Machine Learning Models. Creating a Serverless Python Application helps in leveraging the features of Python to deploy them in a relatively cost-effective manner.

This article will provide a guide on Serverless, Python, and steps to build a Serverless Python function.

What is Python?

Serverless Python: python logo | Hevo data
Image Source: logos-world.net

Python is a popular general-purpose Programming Language that is simple and easy to code. It has a minimal learning curve as it is easily readable and uses English keywords for better understanding. It has a vast set of libraries that have inbuilt high-level functions. It employs the Object-oriented Programming Structure making it highly useful in creating applications. 

Python has use cases ranging from Website Development, Application Development, Machine, and Deep Learning, Artificial Intelligence, Data Analytics, Data Science and Automation, and many more. All the Features required for these use cases are present in Python libraries, with new & better libraries being created on regular basis. It uses a relatively straightforward syntax, thereby making the above operations easier. This makes Python the first choice for developers.

Key Features of Python

serverless python: python features | Hevo data
Image Source: www.cleveroad.com
  • Easier Learning Curve: Python provides an environment that makes the process of development and automation easy and hassle-free. The Open-source availability makes it an easy platform to get started for beginners.
  • Robust Platform: The simple syntax of Python, combined with the use of English keywords make it a good choice for project development. The exceptions and constraints are better managed when compared to other programming languages. This makes the Python platform robust, versatile, and suitable for applications like Machine Learning and Web Development.
  • Huge Support Network: Python being an Open-source platform and one of the most popular languages has resulted in huge online communities that provide continuous updates for libraries and modules. This community also offers help and suggestions to new coders. 
Simplify Data Integration & ETL with Hevo’s No-code Data Pipeline

Hevo Data, a No-code Data Pipeline helps to load data from any data source such as Databases, SaaS applications, Cloud Storage, SDKs, and Streaming Services and simplifies the ETL process. It supports 100+ data sources (including 40+ free data sources) and is a 3-step process by just selecting the data source, providing valid credentials, and choosing the destination. Hevo not only loads the data onto the desired Data Warehouse/Destination but also enriches the data and transforms it into an analysis-ready form without having to write a single line of code.

Hevo’s automated pipeline offers data to be delivered in real-time without any loss from Source to Destination. Its fault-tolerant and scalable architecture ensure that the data is handled in a secure, consistent manner with zero data loss and supports different forms of data. The solutions provided are consistent and work with different BI tools as well.

GET STARTED WITH HEVO FOR FREE[/hevoButton]

Hevo is the fastest, easiest, and most reliable data replication platform that will save your engineering bandwidth and time multifold. Try our 14-day full access free trial today to experience an entirely automated hassle-free Data Replication!

What is Serverless?

serverless python: serverless computing logo | Hevo data
Image Source: www.blog.runcloud.io

Serverless is a Cloud Computing Framework where the Cloud Service provider takes care of the architecture. This enables the users to avoid buying, renting, or managing the servers. A Serverless Platform also enables the dynamic allocation of Servers as well as real-time Computational Requirements. 

Serverless doesn’t mean that applications run without Servers, but instead, it means that the clients aren’t required to manage and pay for the Servers. The cost of operation for a Serverless Platform is significantly less than a traditional system. A single drawback here is that the client becomes completely dependent on the Server vendor.

Key Features of Serverless

  • Servers that are Fully-managed: The Cloud partner takes care of all the architecture associated with the backend of Servers. 
  • Cost-effective: Serverless Architecture runs at a relatively less operational cost. The cost is associated with the computational usage and not the infrastructural aspects.
  • Fault-tolerance: The serverless architecture is not limited by the number of servers and has many fail-safes associated with it making it highly fault-tolerant.
  • High Scalability: Serverless architecture has a dynamic structure with theoretically unlimited storage and computational power. It is easily scalable and can be implemented using a minimum number of lines of code.

How to Write Serverless Python Functions?

A) Creating a Simple Serverless Python Function

Creating a Serverless Python function is a simple process that requires using Node and NPM.

Step 1: Set up/Install the Dependencies

  • Install the Node and NPM on your system using the following command:
npm install -g serverless
  • Using the Command Line Interface, create a project as follows:
mkdir ~/Hevo-serverless-Example

cd ~/Hevo-serverless-Example

sls create -n Hevo-serverless-Example -t aws-python3

The name of the directory created here is Hevo-serverless-Example. The aws-python3 module is used for designing the AWS template for Python3, which you will be using to create the serverless function.

Here, –n XXXXXX is used to define the name of the project. In this example, the name of the project is Hevo-serverless-Example. This can be changed to any name of your choice.

Now, move to the contents of the directory. 

cat serverless.yml

The serverless.yml has preloaded comments that help in configuring sections of config.

Step 2: Build your Serverless Function in Python

Let us create a basic Serverless Python function that shows a message as follows:

def handler(event, context):

    return {"message": "Hello! Welcome to Serverless Example"}

Now, save this code in the Hevo-serverless-Example directory as hello.py. The functions are generally referred to as Handlers but the functions can be named anything you want.

After the function is created, add it to serverless.yml by editing and replacing the following section:

functions:

  hello:

    handler: hello.handler

After you save the serverless.yml file, you now need to configure the AWS credentials. When the serverless.yml is ready to be deployed, use the following command:

sls deploy

The deployment will take time as the following processes will be performed during the deployment:

  • Based on the Serverless.yml file a new Cloud Formation Template would be made.
  • Compression of hello.py and Cloud Formation Template would take place.
  • The compressed files would be uploaded to the Amazon S3 Bucket.

The following command can be used to test the deployment of the Serverless Python function:

sls invoke -f hello

You should now see the following response:

{"message": "Hello! Welcome to Serverless Example"}

You have now successfully created your first Serverless Python function!

B) Creating an Advanced Serverless Python Function

As you learned, creating a Serverless Python function is easy, but now let’s create an Advanced Serverless Python function that offers more functionalities:

Step 1: Building a Function for HTTP Requests

You now need to create a new file named httprequest.py. Once you’ve done the same, add the following code to it:

import requests

def handler(event, context):

    r = requests.get("https://news.ycombinator.com/news")

    return {"content": r.text}

Now update the serverless.yml by editing the following sections:

functions:

  hello:

    handler: hello.handler

  httprequest:

    handler: httprequest.handler

Use the same steps as before to deploy the function. Use the additional command to deploy httprequest.py file:

sls deploy

sls invoke -f httprequest

Note: If you see an ImportError, install the dependencies of Serverless using the following command:

sls plugin install -n serverless-python-requirements

Add the following lines to the end of serverless.yml to install the dependencies:

- serverless-python-requirements

Also, add the custom section into the Serverless.yml file:

custom:

  pythonRequirements:

    dockerizePip: true

This section tells the serverless-python-requirements plugin to compile the Python packages in a Docker container before bundling them in the zip archive. You also need to install docker.

Here, you first have to deploy and invoke your function again:

sls deploy

sls invoke -f httprequest

Step 2: Design an HTTP Request-based Worker Function

Let us now set up a worker for the HTTP request function for your advanced Serverless Python function. You can do this by adding the below-mentioned code in your Serverless.yml file:

functions:

  httprequest:

    handler: httprequest.handler

    events:

     - schedule: rate(10 minutes)

Now, go ahead and deploy the Serverless Python function:

sls deploy

Your httprequest will now get automatically triggered in 10 minutes.

Step 3: Create a Web API

Web APIs help in triggering functions, invoking a gateway, and a lot more. A serverless framework takes care of this.

To create an API using the following section and add it to the Serverless.yml file.

functions:

  webapi:

    handler: webapi.handler

    events:

      - http:

          path: /

          method: get

Since a new handler is added, a new webapi.py file needs to be created.

import JSON
def handler(event, context):

    return {"statusCode": 200, "body": json.dumps({"message": "Example HTTP response for Hevo-serverless-Example"})}

You’ll now need to deploy the Serverless Python function again:

sls deploy

The Serverless Framework will provide an endpoint:

endpoints: GET - https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/dev

You should see the following response for the Serverless Python function:

{"message": "Example HTTP response for Hevo-serverless-Example"}

You have successfully created a Serverless Python function that sends HTTP requests for a predefined interval as well as creates an API for the function!

Conclusion

With the growing popularity of Serverless Cloud Architecture, developers and organizations are creating and hosting applications that support this architecture. Python is the first choice for developing these applications as it is easy to use and houses huge library support. Creating a Serverless Python Application helps in leveraging the features of Serverless Architecture along with the benefits of the Python Programming Language. This article gave a step-by-step guide on creating a Serverless Python Function.

There are various Data Sources that organizations leverage to capture a variety of valuable data points. But, transferring data from these sources into a Data Warehouse for a holistic analysis is a hectic task. It requires you to code and maintains complex functions that can help achieve a smooth flow of data. An Automated Data Pipeline helps in solving this issue and this is where Hevo comes into the picture. Hevo Data is a No-code Data Pipeline and has awesome 100+ pre-built Integrations that you can choose from.

visit our website to explore hevo

Hevo can help you Integrate your data from 100+ data sources and load them into a destination to analyze real-time data at an affordable price. It will make your life easier and Data Migration hassle-free. It is user-friendly, reliable, and secure.

SIGN UP for a 14-day free trial and see the difference!

Share your experience of learning about Serverless Python Functions in the comments section below.

Arsalan Mohammed
Research Analyst, Hevo Data

Arsalan is a research analyst at Hevo and a data science enthusiast with over two years of experience in the field. He completed his B.tech in computer science with a specialization in Artificial Intelligence and finds joy in sharing the knowledge acquired with data practitioners. His interest in data analysis and architecture drives him to write nearly a hundred articles on various topics related to the data industry.

No-code Data Pipeline For your Data Warehouse