Skip to main content
Runpod’s serverless library enables you to create and deploy scalable functions without managing infrastructure. This tutorial will walk you through creating a simple serverless function that determines whether a number is even.

Creating a Basic Serverless Function

Let’s start by building a function that checks if a given number is even.

Import the Runpod library

Create a new python file called is_even.py. Import the Runpod library:
is_even.py

Define your function

Create a function that takes a job argument:
is_even.py
This function:
  1. Extracts the input from the job dictionary
  2. Checks if the input is an integer
  3. Returns an error message if it’s not an integer
  4. Determines if the number is even and returns the result

Start the Serverless function

Wrap your function with runpod.serverless.start():
is_even.py
This line initializes the serverless function with your specified handler.

Complete code example

Here’s the full code for our serverless function:
is_even.py

Testing your Serverless Function

To test your function locally, use the following command:
When you run the test, you’ll see output similar to this:
This output indicates that:
  1. The serverless worker started successfully
  2. It received the test input
  3. The function processed the input and returned True (as 2 is even)
  4. The job completed successfully

Conclusion

You’ve now created a basic serverless function using Runpod’s Python SDK. This approach allows for efficient, scalable deployment of functions without the need to manage infrastructure. To further explore Runpod’s serverless capabilities, consider:
  • Creating more complex functions
  • Implementing error handling and input validation
  • Exploring Runpod’s documentation for advanced features and best practices
Runpod’s serverless library provides a powerful tool for a wide range of applications, from simple utilities to complex data processing tasks.