cancel
Showing results for 
Search instead for 
Did you mean: 

Kyma - Functions - Multiple files

drvup
Contributor
0 Kudos

Hey Folks,

I am thinking about the usage of Kyma-Functions on a kyma environment. I do have a really simple requirement to handle an event, sent to Event Mesh, and I want to read additional data from S/4 and send it to a 3rd party system.

No rocket science at all. Checking this example repo, it seems logically and easy. But I want to keep it readable and structured, also I want to use the SAP Cloud SDK with a generated VDM.

All of that requires me to have multiple files within a FaaS.

Back in the good old deprecated "Serverless-Service", I was able to upload multiple files. Within Kyma-Functions it seems to be not possible as I do receive all the time response "User function not loaded" from the deployed function, if I do require any other file.

Anyone out there, who is already using Kyma-Functions with more than just the "handler.js" file?

Any idea is appreciated 🙂

Kind regards,

Cedric

Accepted Solutions (1)

Accepted Solutions (1)

quovadis
Product and Topic Expert
Product and Topic Expert

Hello btp_trial73 ,

This is possible. The way I do it is very much similar to the old FaaS-way.

You will need to have a git repo based nodejs16 or nodejs18 kyma function where you will have another source file called index.js;

Then from handler.js you can include it using require syntax for instance const foo = require('./index.js'); assuming both handler.js and index.js are in the same directory.

------------index.js
class k8sCluster {
...
}
module.exports = { k8sCluster: k8sCluster };
------------handler.js

const foo = require('./index.js');
const k8sapi = new foo.k8sCluster({ cluster: process.env["DATA"] } );

When it comes to index.js you are free to include there any other npm or custom libraries (= your other files);

I hope that helps;

PS.

The method described works with nodejs14 kyma serverless as well; Please note nodejs14 serverless has been deprecated.

Here goes a serverless sample which demonstrated how to use standard SAP libraries and SAP Cloud-SDK:

https://github.com/SAP-samples/kyma-runtime-extension-samples/blob/main/kyma-serverless/fun/readme.m...

https://github.com/SAP-samples/kyma-runtime-extension-samples/tree/main/kyma-serverless/fun/fun-srv

drvup
Contributor
0 Kudos

Thanks, Piotr!

I saw this ability to have your function coding stored on as git repo, but did not think further. The clue is about having the handler.js and the index.js file within the same repo.

I'll try my best to set this up using typescript as well, to get the most quality out of it. If I do succeed, I'll post a blog post about it 😉

Thanks for your support and the great blog post about how to set up SSH for repo access, together with a good example, as well.

Answers (0)