Product Information
Develop SAP Cloud Platform Serverless Runtime application using SAP Business Application Studio
Before you go ahead and read through this blog, you need to have some basic know-how on SAP Cloud Platform Serverless Runtime and SAP Business Application Studio.
If SAP Cloud Platform Serverless Runtime is new to you, I recommend you to go through the Overview of Function as a Service blog from dear Carlos Roggan . Not only has he explained the concept and how to work with it beautifully, but also it has links to other reference blogs and materials associated with SAP Cloud Platform Serverless Runtime.
If you are even new to SAP Business Application Studio (BAS), please follow this info blog, which points to other blogs from this central blog.
Now that you have knowledge of both SAP Cloud Platform Serverless Runtime and SAP Business Application Studio, let us discuss in this blog how can you develop a SAP Cloud Platform Serverless Runtime application and build & deploy on SAP Cloud Platform using SAP Business Application Studio.
Let us understand the steps which need to be performed and than we will go in detail of each step.
- Open BAS and Create Dev Space.
- Create Project from Template
- Open the Project in a workspace
- Debug the Function as a Service
- Deploy to SAP Cloud Platform
Let us dig deeper.
Open BAS and Create Dev Space – A dev space is a pre-configured environment with the required tools and extensions tailored for a specific business scenario. Before developing any application, you should create a dev space that is suitable to your development scenario.
In our case, we’ll need to create a dev space for developing applications using SAP Cloud Platform Serverless Runtime.
There are a couple of advantages for using a dev space. First, it contains only a pre-defined set of tools required to develop, so your environment is cleaner, faster, and easier to use. Second, you have the freedom to install your own tools to configure your dev space as you please, providing a local-like development experience.
The dev space Manager
Creating a new dev space
The dev space enables working environment to be cleaner by allowing to download only required tools and extensions. In our case, to be able to work, we need to select the application type as Basic and Extension Factory Serverless Runtime Development Tools from the list of available SAP basic tools extension. It automatically selects the MTA Tools.
The MTA Tools allow you to perform operations such as build, deploy & validation on multi-target applications. The MTA Tools are provided as an extension to SAP Business Application Studio and contains Cloud Foundry CLI, Cloud MTA Build Tool.
Create Project from Template – Now that the dev space with required extensions created, let us go ahead and create a new project using from template. We need to select “Serverless Extensions Project” template to create a new SAP Cloud Platform Extension Factory, serverless runtime project.
On clicking Next button, the Business Application Studio opens a set of guided widget to create a project, a function and a trigger sequentially.
Create a FaaS Project
Create a Function
A function is part of an extension. If you notice here, while creating a function you need to have a module and a handler also. The module should be created locally. The handler method must contain parameters that call the function such as event, context, and so on. Also, it can have additional configuration data.
Create a Trigger for the Function
A Function is instantiated in response to events (trigger). Each trigger is defined as a JSON object in the following format:
-
- HTTP trigger – Sample Code
“<trigger_name>” : {
“type”: “HTTP”,
“function”: <function_name_that_should_be_triggered>
} - Timer trigger – Sample Code
“<trigger_name>” : {
“type”: “Timer”,
“function”: <function_name_that_should_be_triggered>
“timerCronExpression”: <cronexpression_schedule>
}
- HTTP trigger – Sample Code
As of now, HTTP and TIMER based trigger are supported from BAS. AMQP and Cloud Events based triggers will be supported shortly.
Open the Project in a workspace – Once you complete all steps above, you will get an alert as below.
Select to open in new workspace and you will have your project open as below in a new workspace.
In the project we have created, if you notice in the index.js module, you can find the handler template as below. You can write your customized business logic here.
handler: function (event, context) {
/*Enter your function's business logic here*/
return 'hello world from a function!';
}
Debug the Function as a Service – It is always better to try out the function locally before deploying it in the SCP.If you give a close look, you will also find a test folder, which does contain an integration folder. This helps in debugging and thus helps in checking the correctness of the function before deploying it.
Below steps need to be performed for each function before starting debugging of it. It follows the standard Run Configurations for an Extension process. Do find the exact steps to be followed to be able to debug developed function.
Click on the Run Configuration as shown below.
Click on the Create Configuration as shown below.
Select the faas.json file from the command palate
Finally allow to execute the below command.
A configuration tree appears in the RUN CONFIGURATIONS view containing the run configurations that were created for the runnable objects. A new configuration is added to your launch.json file.
To run the configuration, select the configuration and choose the Run icon. Open the <<function_name>>.http file and you can send GET and POST Request to test if the functionality is working fine locally. You can also put a break point to check the flow.
Deploy on SAP Cloud Platform – Now that you have created a project having function and trigger, it needs to be deployed in SAP Cloud Platform for productive usage. Thankfully this also can be done directly from SAP Cloud Platform Business Application Studio.
As of now, building and deploying an MTA deployable directly from Project Context is yet to be done for SAP Cloud Platform serverless runtime. We need to do this using the Terminal feature of SAP Cloud Platform Business Application Studio.
The overall process of deploying on SAP Cloud Platform consists of two steps.
Connecting the Business Application Studio to the SAP Cloud Platform
Connect to the SCP Cloud Platform organization and space where you want to deploy the deploy-able.
With the right credentials, it allows to select the organization and the space. And you get an alert as below.
Deploying the deploy-able in the right SAP Cloud Platform space
To be able to deploy, we need to start a Terminal in Business Application Studio. Log in to the SAP Cloud Platform Extension Factory, serverless runtime CLI with following command.
Please make sure you have an SAP Cloud Platform Serverless Runtime instance with its service key created. After successful login, it will show the serverless instances available in the same space. Under BINDING heading is the service key name of the serverless instance.
Provide the service instance number which you want to use to deploy the created function project. Once successful log in is done, you can deploy the project to the selected service using following command.
After successful deploy, you can retrieve metadata, status information, and links to the project using the following command:
Congrats… Following the steps given above, you would have now been able to create a Function as a Service Project with a serverless runtime function. You can enhance your function logic to meet your business need and experience the magic. SAP Cloud Platform Serverless Runtime becomes quite powerful, when it is integrated with a service like SAP Cloud Platform Enterprise Messaging. It brings in a decoupled architectural essence, which definitely is one of the most important advantages of an Extension for which a serverless runtime is used. You can implement similar use cases from Business Application Studio.
In summary, you would have noticed, how easy the development process has become to develop a serverless runtime project and deploy the same in SAP Cloud Platform when you try to build it from SAP Business Application Studio.
So do enjoy working with SAP Business Application Studio and SAP Cloud Platform Serverless Runtime.
Do not hesitate to provide your feedback on the blog and of course do mention if any issue you face while following this blog.
Hi Pradeep,
Thanks for sharing, it's a very nice and helpful blog. Just want to know if I could develop the XF extension as CPS(SAP Variant Configuration and Pricing) pricing service extension ? Any samples I could try to learn?
BR
Hi Yanbing,
I have replied your query in your mail.
Hope your queries have been clarified. Should you still have certain queries which need to be clarified, please let me know.
regards
Pradeep
Hello Pradeep Panda,
thanks for this useful blog.
I have problems using the debugger. When I try to call the function: GET http://localhost:8080/testfunction I always get a HTTP 404 Not Found. What am I doing wrong?
Best regards,
Michael
Hi Michael-Peter Liermann
localhost:8080 will only work if you execute the request inside the Application studio environment. To run the function locally inside the app studio environment, you have to open <functionname>.http file under the integration folder and click on 'send request' for the respective HTTP method (GET/POST) to view the response on the right panel.
If you want to run/debug the function outside the App studio environment, you need to expose the service on port 8080 and run it in the browser or any rest client(for ex: Postman). To achieve this, you need to click on Expose and open button in the notification dialog which appears when you run the configuration and press enter. Finally, append the URL in the new window with the function name you want to run/debug. The final URL will look like this:
https://<subaccount>-workspaces-ws-lf5cv-app1.cry10.int.applicationstudio.cloud.sap/testfunction
I hope this helps.
Regards,
Satvindar
Hello Satvindar Chug,
thanks for your reply. I tried it in the BAS, but still the same result:
Any ideas?
Best regards
Hi Michael-Peter Liermann
Can you please share your project so that I can reproduce the scenario from my end?
Regards,
Satvindar
Hello Satvindar Chug,
The error already occurs when I create a project with the template "Serverless Extensions Project" in BAS. I am on eu20cf/ Azure, if that helps.
Best regards,
Michael
Hello Michael-Peter Liermann
You run the project/function locally once the project is created successfully. So, in order to understand the issue, it will be great if you can share the exact steps you have followed. Else, if possible we can have a short meeting on Teams as well.
Regards,
Satvindar
Hi,
I ran into the same issue.
Once I added "/" to the end of the web address it started to work
BEFORE:
GET http://localhost:8080/testfunct
AFTER:
GET http://localhost:8080/testfunct/ <=== Add this / character to address
Cheers, Philipp
Hi Team ,
We are trying to create custom extensions for Pricing and Configuration(Variant configuration and pricing) to call the custom implementations. Would love to know how to do that.
Hi Suresh,
Sorry for replying you late.
In case you want to use the function as a service features to create custom extensions for Pricing and Configuration, we would recommend you to look into kyma.
Should you intend to talk to the right person to get to know details about it, please let me know.
You can mail me in pradeep.panda@sap.com
regards
Pradeep