Enterprise Java

Build a Serverless “Hello World” Function

Serverless, Function as a Service (FaaS) or just cloud functions allows you to write code that will run in the cloud. You can use a number of different languages such as JavaScript (Node.js), Swift, Python, Java, PHP and others to write the function code.  What’s nice is that you don’t need to worry about servers, containers, deployment, etc. You write the code and a cloud platform will make sure it executes!

In this blog post you will learn how to build a Hello World function. You will use IBM Cloud Functions to build and run the function (more information about this at the end). For now, let’s jump to creating your first function.

Creating and testing a cloud function

In this section you will create and test a new function.

      1. Register for a free Lite IBM Cloud account or sign into an existing account
      2. Click on Catalog
      3. Remove the label:lite filter and click Enter to update the service list
      4. Type functions and click Enter
      5. Click on Functions box

        Creating a new function
        Creating a new function
      6. Click on Start Creating button
      7. Click on Create Action option
      8. Enter helloworld for Action Name.
        1. Keep the Enclosing Package default value.
        2. For Runtime, also keep the default value of Node.js
      9. Click Create button. A code editor with the function code will load and look like this:
        Function code
        Function code

        • You didn’t use a template but even the default function comes with the most basic JavaScript code which returns Hello World.
        • The function is called main.
        • The params objects allows to pass parameters into the function. I will cover that in another blog post.
        • return returns a JSON object with the function response. In this case it’s just a simple text.
          Next step is to test the function.
      10. Click the Invoke button (upper right in the editor). The result should look like this:

      serverless
      Function response

You just created a new cloud function (you didn’t write any code but that’s OK for now) and you also were able to test the function right in the cloud.

It’s very likely that you would want to invoke this function as a REST API. Luckily that’s very easy to do.

Invoking as a REST API

In this section you will invoke the function as a REST API.

  1. On the left-hand side, click Endpoints
  2. In the CURL section you will see a curl command to invoke this function. Click the eye-icon to show the username/password. Basic Authentication is used to protect this function

    serverless
    curl command
  3. Click on the copy-icon to copy the curl command
  4. Open a Terminal window and paste the curl command. You should see full JSON response from the API (function):

serverless
Function response (JSON formatted with jsonlint.com)

It’s also possible to invoke the function API directly from the browser address bar. In order to do that, format the URL in the following format:

https://username:password@openwhisk.ng.bluemix.net/api/v1/namespaces/maxkatzorg_dev/actions/hello?blocking=true

Replace username and password with the actual values. You will be able to see these values when you click on the eye-icon. Here is how it looks when running from Chrome:

Invoking from a browser

Invoke as a Web Action

In this section you will learn how to invoke the function as a Web Action. A Web Action is very similar to API you invoked in the previous section just without authentication. In a Web Action, authentication is up to the developer.

      1. Click on Endpoints tab
      2. Check Enable as Web Action checkbox
      3. Click the Save button
      4. Copy the URL and then paste it in a browser address bar. You should see this in the browser:
        {
           "message": "Hello World"
        }

        If you use the latest version of Firefox you should see a formatted version of this output (due to Firefox’s built-in tools)

IBM Cloud Functions

In this blog post I used IBM Cloud Functions to create the cloud function. IBM Cloud Functions is based on the popular open source Apache OpenWhisk project.

Summary

In this blog post you learned how build your first cloud function. It was fast and simple. Now, this space is not new (as most people might think). A cloud-based environment where you could execute code had been part of Backend as a Service (BaaS) or Mobile Backend as a Service (mBaas) type companies. Companies such as Parse (acquired by Facebook and then open sourced), StackMob (acquired by PayPal) Kinvey (acquired by Progress) and Appery.io provided (Appery.io and Kinvey provide today) the option to write and execute code in the cloud without worrying much about servers. You did have some limits on resources and different pricing structure. That’s a topic for a different blog post.

Published on Java Code Geeks with permission by Max Katz , partner at our JCG program. See the original article here: Build a Serverless “Hello World” Function

Opinions expressed by Java Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button