Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
CarlosRoggan
Product and Topic Expert
Product and Topic Expert
With other words:

We write our first funny serverless Function
with
SAP Cloud Platform Serverless Runtime
and we use the cloud based tool
Extension Center


This blog is part of a series of tutorials explaining how to write serverless functions using the Function-as-a-Service offering in SAP Cloud Platform Serverless Runtime

Quicklinks:
Preparation Blog
Quick Guide



Overview


A "serverless function" is a function.
It is written in Javascript for Node.js
It is deployed to the "Functions-as-a-Service" runtime of SAP Cloud Platform
The function can be created using the nice tool "Extension Center" which is available in SAP Cloud Platform

This blog guides you through the creation of a simple silly example
The titles of the chapters show how simple it is:

  • Open Extension Center

  • Create Project

  • Create Function

  • Create Trigger

  • Try it


Prerequisites


You should be an absolute newbie
But not too much absolute newbie
You should have gone through the Preparation blog, so you're able to open the Extension Center

Open Extension Center


Opening the Extension Center is mentioned in the Prerequisites section, so we can assume that it is already open
Hence closing this chapter

Create Project


Make sure that Extensions is selected in the left menu pane
Note:
As you know, under the umbrella of “Serverless Runtime”, we also get the Capability ‘”OData Provisioning” which can by accessed from “Extension Center” -> “Services” entry as well.
But we ignore it

Click on New Extension
The wizard for creating a new “Extension” is opened

What is that, a new “Extension”?
For my small brain (not "extended"), things are easier to understand with concrete and simple words.
So, translated into my language (which consists of limited set of simple words):
A new Extension is a new project
And yes, you can use it to extend SAP solutions or whatever

OK, now we really create that extension.
The first wizard page allows to choose a template, which helps to create a project structure.
There’s a template called “Blank Template”. Let’s choose this one.
It is less convenient, please forgive me, but that means better learning experience

After selecting the template, click on “step 2 “
Enter a dumb “Name” for your extension, e.g. “testproject”, choose whatever “Runtime” you prefer and press “Create”


The development environment is presented to us in its full glory:


The wizard has created the minimum skeleton: 2 files in 1 project

The main entry file is opened in the editor:
faas.json
In my language I call it the manifest (or: main descriptor) of a Functions project
Or you can think of it as descriptor of your extension
Note:
This file is waiting to be edited manually, only be aware of one rule:
Never change the property "project". The value is generated according to the "extension" name and if it isn't equal, the deployment fails

The second file is the package.json file, the descriptor of Node.js projects

Note:
Yes, currently a Function can only be implemented in Node.js, Java as runtime might be supported later

Sigh… an empty functions project is like a bottle without wine...

Create Function


What we want is a function.
Now 2 things are required:
- Create a javascript file
- Let faas.json know where it is

We could do both steps manually:
The development environment allows us to create files and folders
Also, we can edit the faas.json file directly in the editor

BUT:
Extension Center offers a nice form-based editor.
So it is time to hit the toggle-button “Form View” to change from the plain code editor to the form based editor

Now, in the “Functions” tab, we can press the button “Add Function”
In the creation dialog, we’re asked for the required information:

Function name: This is really just a name (not file name)
Module name: This is really a file name (not just a name)
Handler name: This is really….well, since a module can have multiple methods, we’re required to mention the main entry
Timeout: we can leave the default


After entering some values, we have to press the  +  button.
Afterwards, we can create more functions (files), or press “Add” to close the dialog

Now the new function is there, in the list. But what has happened?
Let’s switch back to the Code View
We can see:
a “lib” folder has been created
the new javascript file inside
our handler function inside
the metadata has been added to the descriptor file

And one thing is noteworthy – and for sure you have to get used to it:
The faas.json file has been edited, so it is dirty
Now, we cannot just “save” the file. I mean locally save the changes in the file
We have to press Save and Deploy
And we really should do that.
Because if we forget that (important) click and do any other (silly) click somewhere in the Extension Center, or in the browser, then all changes are lost
Remember my words:
Someday you will lose your changes – and then you will remember me, and the lesson I told you:
Always do saveanddeploy


And what now?
We have a function
What to do with it?
Open it with double click: now we can see the function
But it does nothing
Let’s add one line:
console.log('*** MIAOW ***')

Oh - now it really does something


Let me again show the whole code of the function:
 module.exports = { 
myhandler: function (event, context) {
console.log('*** MIAOW ***') ;
return '...miaow...!';
}
}

This is really all:
Just a function.
It has to be exported such that it can be called by the Node.js runtime

Create Trigger


While we’re having a coffee and waiting for the function to miaow….
We can have a look at our Extension Center, Form View again
We created a Function with the help of the first Tab

But what’s that second tab: “Triggers” ?
Yes, a “Trigger” is just a trigger.
In the Serverless-Universe, every“thing” is strolling around in the space, along with its container - until it is needed
I like to compare it with my pussycat:

It is sleeping all the day (not during night)
If I want to cuddle it, I have to wake it up
Then it does its job (being fluffy and purr)
Then it goes back to sleep

I have a few possibilities to wake it up:

Talk to it (“come here my pussycat” - typically useless)
Call it (“miu miu miu” - pussycat might move one ear)
Tease it (ends with scratches on my hands)
Seduce it (Showing favorite snacks - irresistible!)

OK, hope you like the cat-comparison
It is silly
Good. Going back, our function is in the cloud and it is sleeping.
We need to choose a trigger to wake it up and do something

In the Extension Center, we switch to the “Form View” and open the “Triggers” tab
We expand the drop-down of the “Add Trigger” button
Choose “HTTP” from the drop down
In the dialog, we enter the required values:

The name is just a name. It will be used in faas.json to reference the trigger
Also we specify the function which should be triggered


Press Add and don’t forget to remember my famous last words (already forgotten? Here they are: Always do saveanddeploy)
We have to “saveanddeploy” the updated function-project, to let the framework generate a URL
After the deployment status turns to a greenish “Ready”, we can view the details of the new trigger.
Click on the little “details” button to display a popup which contains the trigger URL



Try it


At this point in time, our cat is still sleeping on a fluffy cloud but the snack is already in our hands…
We only need to use it, to wake up the sleeping beauty:

We just need to click the link

As a result, we get that cute response


That's it:
We've created our first function
Silly - but serverless

Summary


We've created a function using the Extension Center in the cloud
The function can be invoked with an HTTP URL, by anybody
The function sleeps until we wake it up
The function does its job, then goes back to sleep

The good thing: payment is only required when the function is awake
With other words:
Food can be consumed only while pussycat is awake :burrito:

Quick Guide


To create a function with extension center:
Create project
Create function
Create trigger
Use Details button for URL

Don't forget saveanddeploy

Links


See Overview Blog

Appendix: The sleeping function