Skip to Content
Technical Articles

SAP C/4HANA Extensibility – Triggering Lambdas with Events

This blog will detail how a Lambda function can be triggered to run when an event is published into the Kyma environment.  To eliminate any barriers, we will utilize the SAP Commerce mock application to send events, but the process flow would be the same if using SAP Commerce. This blog continues from the setup of the SAP Commerce mock application detailed in this blog. The setup of SAP Commerce can be found in this blog.

In the home workspace of your Kyma instance choose the commerce-mock application and then choose Create Binding and bind the application to your mocks namespace.  This will provide access to the commerce-mock application in your mocks namespace.

This will also result in one Bound Application showing in the Namespaces view. Choose the mocks namespace tile to open the namespace.

Choose the Catalog, Services and then choose SAP Commerce Cloud – Events, which is labeled with commerce-mock.

 

Choose Add to add a service instance, which will allow us to utilize the events within this namespace.

Accept the default values and choose Create Instance.

Next choose the Lambdas menu option and then choose Add Lambda.

For the Function Name provide the name order-created.  In the code view set the function code to:

module.exports = { main: function (event, context) {    
    console.log("hello from order created");
    console.log("Received data: ", event.data);
} }

The event.data will contain the data object that the event has published.

Next, bind the function to the order.created event of the commerce mock application by first choosing the Select Function Trigger button and then choose the Event Trigger option.

Find the event order.created by either utilizing the search field or by scrolling through the list.  Select order.created and then choose the Add button to add the event trigger.

Review your entries for the order-created function to verify they match the following screenshot and then choose the Create button to initiate the deployment of the lambda function.

The deployment of the lambda function may take a minute.  Refresh the page until the STATUS is green.

Open the commerce mock application, choose Remote APIs and then choose SAP Commerce Cloud – Events.

This will navigate you to a page where you can send an event and also provides details regarding the API.  In Event Topics choose order.created.v1 from the drop down list for the desired event.  This will place an entry into the field below, which can be edited as desired.  This is the data object that will be sent and consumed by our lambda function.  Press the Send Event button to trigger the event.

Viewing the Event Flow

Next, take a look at the tools provided by Kyma to help you troubleshoot and view the system interactions.

Within the mocks namespace choose the Lambdas menu option.  In the lambda you created, order-created, choose the ellipse and then choose Show Logs.

Use the search options to filter the results or scroll through the records to find the output from sending the order.created event.

Next, explore the tracing features of Kyma, which are provided by Jaeger.  The intent of this section is to outline how a trace should appear for a working function. Using a known working function should help identify possible focus areas in the case of troubleshooting issues.

In the home workspace choose the Tracing menu.

Tracing is done on a service base level.  Within the Find Traces pane, set connector as the chosen service and then choose the Find Traces to show the results of the event order.created being sent.  Notice how the event has spanned multiple services.  Any of these services could be used as the selected service in the Find Traces to show the same information.  Choose the trace to obtain further information.

Here you can see the entire flow of trace in the order it was processed, and the time utilized by each service.  Each of these services can then be selected to see more detailed information.

The event-publish-knative-service showing the event information including the event-type.

The istio-proxy.mocks showing the call to the order-created lambda.

1 Comment
You must be Logged on to comment or reply to a post.