Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Build a Serverless Hello World Function

DZone's Guide to

Build a Serverless Hello World Function

If you are just getting started with serverless, this article is the perfect way to go through the motions of a basic Hello World function.

· Cloud Zone ·
Free Resource

Discover a centralized approach to monitor your virtual infrastructure, on-premise IT environment, and cloud infrastructure – all on a single platform.

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
    cloudfunctions-catalog 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

    • 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. The next step is to test the function.
  10. Click the Invoke button (upper right in the editor). The result should look like this:
    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
    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):
    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 to build your first cloud function. It was fast and simple. Now, this space is not new (as some 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.

Learn how to auto-discover your containers and monitor their performance, capture Docker host and container metrics to allocate host resources, and provision containers.

Topics:
cloud ,serverless function ,tutorial ,hello world ,invoking functions

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}