Trigger a workload with an event

We already know how to create and expose a workload (Function and microservice). Now it's time to actually use an event to trigger a workload.

Create a Function

First, create a sample Function that prints out the received event to console:

  • Kyma Dashboard
  • kubectl
  1. Go to Namespaces and select the default Namespace.
  1. Go to Workloads > Functions and click Create Function +.
  1. Name the Function lastorder and click Create.
  1. In the inline editor for the Function, replace its source with the following code:

    Click to copy
    module.exports = {
    main: async function (event, context) {
    console.log("Received event:", event.data);
    return;
    }
    }
  2. Save your changes.

  1. Wait a few seconds for the Function to have status RUNNING.

Create a Subscription

Next, to subscribe to events, we need a Subscription custom resource. We're going to subscribe to events of the type order.received.v1. All the published events of this type are then forwarded to an HTTP endpoint called Sink. You can define this endpoint in the Subscription's spec.

  • Kyma Dashboard
  • kubectl
  1. In your Function's view, go to Configuration and click Create Subscription+.
  1. Provide the following parameters:

    • Subscription name: lastorder-sub
    • Application name: myapp
    • Event name: order.received
    • Event version: v1
    • Event type is generated automatically. For this example, it's sap.kyma.custom.myapp.order.received.v1.
  2. Click Create.

  1. Wait a few seconds for the Subscription to have status READY.

Trigger the workload with an event

We created the lastorder Function and subscribed to the order.received.v1 event by creating a Subscription CR. Now it's time to publish your event and trigger the Function. In this example, we'll port-forward the Kyma Eventing Service to localhost.

  1. Port-forward the Kyma Eventing Service to localhost. We will use port 3000. In your terminal, run:
    Click to copy
    kubectl -n kyma-system port-forward service/eventing-event-publisher-proxy 3000:80
  2. Now publish an event to trigger your Function. In another terminal window, run:
  • curl
  • CloudEvents Conformance Tool
Click to copy
curl -v -X POST \
-H "ce-specversion: 1.0" \
-H "ce-type: sap.kyma.custom.myapp.order.received.v1" \
-H "ce-source: /default/io.kyma-project/custom" \
-H "ce-eventtypeversion: v1" \
-H "ce-id: 759815c3-b142-48f2-bf18-c6502dc0998f" \
-H "content-type: application/json" \
-d "{\"orderCode\":\"3211213\"}" \
http://localhost:3000/publish

Verify the event delivery

To verify that the event was properly delivered, check the logs of the Function:

  • Kyma Dashboard
  • kubectl
  1. In Kyma Dashboard, return to the view of your lastorder Function.
  1. Go to Code and find the Replicas of the Function section.
  1. Click on View Logs.
  1. You see the received event in the logs:

    Click to copy
    Received event: { orderCode: '3211213' }