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
- Go to Namespaces and select the default Namespace.
- Go to Workloads > Functions and click Create Function +.
- Name the Function
lastorder
and click Create.
In the inline editor for the Function, replace its source with the following code:
Click to copymodule.exports = {main: async function (event, context) {console.log("Received event:", event.data);return;}}Save your changes.
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
- In your Function's view, go to Configuration and click Create Subscription+.
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
.
- Subscription name:
Click Create.
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.
- Port-forward the Kyma Eventing Service to localhost. We will use port
3000
. In your terminal, run:Click to copykubectl -n kyma-system port-forward service/eventing-event-publisher-proxy 3000:80 - Now publish an event to trigger your Function. In another terminal window, run:
- curl
- CloudEvents Conformance Tool
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
- In Kyma Dashboard, return to the view of your
lastorder
Function.
- Go to Code and find the Replicas of the Function section.
- Click on View Logs.
You see the received event in the logs:
Click to copyReceived event: { orderCode: '3211213' }