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.
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.
On the left-hand side, click Endpoints
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
Click on the copy-icon to copy the curl command
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:
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:
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.
Click on Endpoints tab
Check Enable as Web Action checkbox
Click the Save button
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.
This site uses Akismet to reduce spam. Learn how your comment data is processed.