Exploring OpenShift Serverless Functions with FastAPI

Hemanth BITRA
3 min readAug 6, 2024

--

Hello Everyone,

I’m diving into the world of Red Hat OpenShift Serverless Functions and am excited to share my findings with you!

What Are OpenShift Serverless Functions?

OpenShift Serverless is Red Hat’s Kubernetes-based container platform that enables the development and deployment of serverless applications. One of the key components of OpenShift Serverless is function serving, which allows you to create and manage functions that automatically scale up or down based on demand

Key Features of OpenShift Serverless Function Serving

  • Event-Driven Architecture: Functions can be triggered by various events, such as HTTP requests, messages from a queue, or changes in data storage.
  • Automatic Scaling: Functions automatically scale in response to incoming requests. When there are no requests, the functions scale down to zero, ensuring that resources are used efficiently.
  • Microservices Integration: Functions can be used as part of a microservices architecture, allowing for seamless integration with other services running on OpenShift.
  • Developer Productivity: Simplifies the development process by allowing developers to focus on writing code without worrying about the underlying infrastructure.
  • Knative Support: Built on top of Knative, an open-source project that extends Kubernetes to provide a set of middleware components for building serverless applications.
  • Multi-Language Support: Supports functions written in multiple programming languages, giving developers flexibility in choosing the right tool for the job.
  • Built-in Security: Ensures applications are secure, reliable, and highly available with integrated security monitoring and automation features.

Benefits of OpenShift Serverless

  • Cost Efficiency: Only pay for resources when the function is actively running.
  • Scalability: Handles high loads seamlessly by automatically scaling the number of instances based on demand.
  • Reduced Operational Overhead: Developers do not need to manage the infrastructure, as it is abstracted away.

Use Cases

  • Webhooks: Respond to events from other systems.
  • Data Processing: Perform real-time data processing tasks.
  • IoT Applications: Handle data from IoT devices in a scalable manner.
  • Backend for Mobile or Web Applications: Provide backend functionality that scales with user demand.

Demo Use Case: Webhook with FastAPI

In this demo, I’ll show how to set up a webhook receiver using FastAPI. This function will receive and parse JSON webhook messages, transforming them as needed.

Creating the Serverless Function

  1. Prepare Your YAML Manifest: Below is a sample YAML file to create a serverless function in OpenShift.

2. Apply the Manifest: Use the following command to deploy the serverless function:

oc apply -f fastapi-webhook-receiver-serverless.yaml

This command creates the serverless function in OpenShift.

Below is the UI look when Serverless Function is created in OpenShift

3. Get the Endpoint: Retrieve the function’s endpoint with:

oc get ksvc fastapi-service -o jsonpath='{.status.url}'

4. Function Scaling: The function scales up when a sample request is sent and scales down to zero when idle, optimizing cost.

Case 1: When serverless function is create, it will create a deployment like below, if there is no traffic is served it scale the pods to Zero

Case 2: When sample request is sent to webhook like below, openshift will scale up the pods in the deployment instantly and accepts the webhook message

Case 3: If there is no active traffic serving on the openshift endpoint it auto-terminated the pods to save the cost.

Conclusion

OpenShift Serverless Functions with FastAPI offer a powerful way to build and manage scalable serverless applications. Whether you’re handling webhooks, this approach simplifies development and reduces operational overhead.

Happy coding!

--

--

Hemanth BITRA

Specialist Software Engineer, working in IT banking sector