Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Create your first serverless function with Red Hat OpenShift Serverless Functions

January 4, 2021
Naina Singh
Related topics:
KubernetesNode.jsServerless
Related products:
Red Hat OpenShiftRed Hat OpenShift Container Platform

Share:

    Serverless is a powerful and popular paradigm where you don’t have to worry about managing and maintaining your application infrastructure. In the serverless context, a function is a single-purpose piece of code created by the developer but run and monitored by the managed infrastructure. A serverless function’s value is its simplicity and swiftness, which can entice even those who don’t consider themselves developers.

    This article introduces you to Red Hat OpenShift Serverless Functions, a new developer preview feature in Red Hat OpenShift Serverless 1.11. I will provide an overview, then present two example applications demonstrating Serverless Functions with Node.js. Please check the OpenShift Serverless Functions Quick Start document for the example prerequisites.

    OpenShift Serverless Functions

    Red Hat OpenShift Serverless leverages the power of Knative to deliver serverless, event-driven applications that scale on demand. With the OpenShift Serverless 1.11 release, we have added the new Serverless Functions feature, currently available as a developer preview. Serverless Functions comes with pre-defined templates and runtimes and provides a local developer experience. Together, these features make it very easy to create serverless applications.

    How to get Serverless Functions

    Serverless Functions is bundled with the OpenShift Serverless command-line interface (CLI), kn. When you use an OpenShift Serverless Operator for installation, OpenShift Serverless is automatically deployed, and managed on OpenShift. You can access Serverless Functions with the following command:

    $ kn func
    

    Note: See the OpenShift Serverless documentation for installation instructions.

    What's included?

    Serverless Functions comes with predefined runtimes for popular languages such as Quarkus, Node.js, and Go. These runtimes are based on Cloud Native Buildpacks. After you choose a runtime, Serverless Functions creates the appropriate project scaffolding so that you can focus on writing business logic. Serverless Functions also includes a local developer experience to support a quick inner loop of iterative development and testing.

    Invoking Serverless Functions

    You can invoke Serverless Functions using plain HTTP requests or CloudEvents with OpenShift Serverless eventing components. OpenShift Serverless Functions comes with out-of-the-box project templates to jumpstart your code for both the HTTP and CloudEvents trigger types.

    Next, we'll explore two examples. For the first example, we'll configure Serverless Functions for HTTP requests. For the second example, we'll use CloudEvents. Please use the Serverless Functions quick start document to ensure that you have the example prerequisites installed.

    Example 1: Create a serverless function for HTTP requests

    Once you have the prerequisites installed, create a new directory for your serverless function. Once you are in the directory, execute the following command to create and deploy a new serverless function:

    $  kn func create 
    

    By default, the function is initialized with a project template for plain HTTP requests. You can choose your programming language by entering Node.js, Quarkus, or Go as the value for the -l flag. If you do not provide a runtime with the -l flag, the default runtime is Node.js. We'll use Node.js for both of our examples.

    Note: You can use the -c flag to prompt the CLI to guide you in creating your first function through the interactive developer experience, which prompts you to add the language and event values. Type -help anytime for assistance.

    The Node.js runtime

    By default, entering the command $ kn func create creates the scaffolding for a function that is triggered by a plain HTTP request. The scaffolding for our default Node.js runtime includes index.js, package.json, and func.yaml files. We can extend the index.js base code to develop our serverless function.

    As a start, let's add a return message of Greeting <username> in the provided handleGet(context) method. Figure 1 shows the handleGet function in index.js.

    function handleGet from the index.js file. This function accepts the context object. and return the word"Greetings" with the name provided by the Get method. If no name was provided, this functions use the word stranger instead of the provided name.
    Figure 1: This figure is the screenshot of function handleGet(context) from index.js file of the Serverless Function.
    Figure 1: The handleGet(context) function.

    Deploy the function

    Next, we'll deploy this function to our OpenShift cluster. Be sure that you are logged into an OpenShift cluster from your local environment, then type the following command with the project name or cluster namespace:

    $ kn func deploy  -n <namespace>
    

    Remember that you can use the -c flag for an interactive experience.

    Serverless Functions will prompt you to provide a container registry where the resulting image is uploaded. DockerHub is the default registry, but you can use any public image registry.

    Now, go to the Topology view in the OpenShift developer console. You will see your function deployed as a Knative service, as shown in Figure 2.

    Serverless Function deployed on OpenShift Cluster and the Routes highlighted that can be used to access the deployed function from the browser.
    Figure illustrating the deployed Serverless Function on OpenShift cluster.
    Figure 2: View the deployed serverless function on your OpenShift cluster.

    Test the function

    We can use the routes URL shown in Figure 2 to test our deployed serverless function. Enter the following command to delete the function from your cluster:

    $ kn func delete
    

    For a local developer experience, we can test serverless functions using standard language tooling or in a container running locally. Use the following command on the kn command-line to build the container image:

    $ kn func build
    

    To test the built image container in a local environment, enter:

    $ kn func run 
    

    Use the curl command to test your deployed image:

    $ curl ‘https://localhost:8080/?name=Universe’
    

    You may also use the browser to see the results, as shown in Figure 3.

    localhost on port 8080 is being accessed with the name "Universe" as a data for the Get method of the deployed Serverless Function. Browser displays "Greetings Universe!"
    Figure illustrating the deployed function being called from the browser
    Figure 3: The deployed function being called from the browser.

    Example 2: Create a serverless function for CloudEvents

    For our second example, we'll create a serverless function that responds to CloudEvents rather than HTTP requests. Before you start, please check the quick start document to ensure that you have the prerequisites installed for this example.

    Create a new serverless function project

    We'll use the same command that we used previously to create a new project. This time, however, we will provide an events value for the -t flag. Alternatively, we could use the -c flag for interactive prompts.

    $  kn func create -l <node|quarkus> -t  events  
    

    To receive CloudEvents, we will need Knative eventing components, so we'll set that up next.

    Log in to the OpenShift developer console and navigate to the Developer perspective. Click the Add section to see the Channel tile highlighted in Figure 4. This tile creates a default channel.

    Channel Tile is being highlighted under the Add section of OpenShift Developer Console.
    Figure illustrating highlighted in red "Channel" Tile on OpenShift Developer Console.
    Figure 4: Locate the Channel tile in the OpenShift developer console (notice the tile in red).

    Now, we need an event source. For that, we will go back to the Add section and click on the Event Source tile shown in Figure 5.

    "Event Soruce" tile is being shown under the Add section of OpenShift Developer Console.
    Figure illustrates "Event Source" tile highlighted in red box on OpenShift Developer Console
    Figure 5: Locate the Event Source tile in the OpenShift developer console.

    Next, as shown in Figure 6, we will select and configure a ping source as the event source for our deployed function. Note that the Sink section displays the deployed function and the channel we've just created. For this example, we will choose the channel as the sink for our event source.

    Sink options for the Ping Event source are being shown. Options are the previously created channel and the deployed OpenShift Serverless Function
    Figure highlighting the sink options for Event Source
    Figure 6: Select Channel as the sink option for the ping event source.

    After creating the event source, we can view all the components in the Topology view, as shown in Figure 7.

    OpenShift Serverless Function, created Event Source and Channel are beign shown in the topology view of OpenShift Developer Console.
    Figure illustrates all deployed components. Function, Even Source and Channel.
    Figure 7: The deployed serverless function, event source, and channel.

    To add a trigger to the deployed function, hover over the channel, then click and drag the blue line to connect the channel to the function. Figure 8 shows the full deployment details in the Topology view.

    Serverless Function connected to Event Source via Channel is being shown and Serverless Function Pod is active as it is receiving events.
    Figure illustrating Serverless Function connected to Event Source via Channel.
    Figure 8: Connect the serverless function to the event source via the channel.

    When the function starts receiving events, Knative spins up the function pod, and the logs show the call to the function. We have just created and deployed an OpenShift serverless function.

    Looking forward

    OpenShift Serverless Functions is available as a developer preview in OpenShift Serverless 1.11. It is available to all OpenShift users. We will release new features in the coming months, and your feedback is greatly appreciated.

    This article is the first in a series introducing Serverless Functions. My next article will introduce you to creating serverless functions with Quarkus, the supersonic, subatomic Java runtime. In the meantime, you can learn more about OpenShift Serverless Functions by reading the OpenShift Serverless 1.11 release announcement, the OpenShift Serverless documentation, and the OpenShift Serverless Functions documentation.

    Last updated: May 8, 2024

    Related Posts

    • Part 1: Introduction to Serverless with Knative

    • Part 2: Building a Serverless Service

    Recent Posts

    • Exploring Llama Stack with Python: Tool calling and agents

    • Enhance data security in OpenShift Data Foundation

    • AI meets containers: My first step into Podman AI Lab

    • Live migrating VMs with OpenShift Virtualization

    • Storage considerations for OpenShift Virtualization

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue