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.
New project from template
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>
}
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.
You can view the project structure in the Explorer. It is a node.js project. The lib folder contains the following sections describe how you can write function code using node.js 8 and node.js 10. Write a handler method as a JavaScript function in a node.js module. It receives the event and context parameters from the runtime. The handler is called for each function invocation. It can return a simple value directly, undefined or a promise to handle the asynchronous execution.
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.