Prerequisites
Before you begin, ensure that the following prerequisites are met:
Task 1: Configure the NATS back end
Steps
In Kyma dashboard, choose Modify Modules.
Select the Eventing module and choose Edit.
Choose NATS as the back end type.
Choose Save.
Result
When you choose the backed type, the Eventing module's state becomes Ready.
Task 2: Create a new namespace
Steps
From the SAP BTP cockpit, follow the link to Kyma dashboard.
Go to Namespaces and select Create.
As name, enter eventing and select Create.
Result
If successful, you see the namespace details for your new namespace. The namespace has the status Active.
Task 3: Create a Function
Steps
In Kyma dashboard, select the namespace you created in the previous task.
Go to Workloads→Functions.
The namespace does not contain Functions yet.
Select Create and enter the following parameters:
- Name: lastorder
- Language: JavaScript
- Runtime: Node.js 20
Replace the source of the Function's editor:
123456
module.exports = {
main: async function (event, context) {
console.log("Received event:", event.data);
return;
}
}
Select Create.
Result
If successful, after a few seconds, the Function's status is Running.
Task 4: Subscribe to various event types
Steps
In the eventing namespace, go to Configuration→Subscriptions, and select Create.
Set up the Subscription with the following parameters:
- Name: lastorder-sub
- Types: order.received.v1 and order.changed.v1
- Service: lastorder
Switch to the YAML tab.
In the spec section, add source: myapp.
1234
...
spec:
source: myapp
...
Select Create.
Result
If successful, after a few seconds, the Subscription's status is Ready.
Task 5: Trigger the workload with an event
Steps
Note
For specifying the event payload, you can send events based on the
CloudEvents specification. Various
SDKs are also available, so that you could publish/emit and subscribe/consume them in your custom applications written in a variety of different programming languages.
Open a terminal window and run:
1
kubectl -n kyma-system port-forward service/eventing-publisher-proxy 3000:80
To publish the event order.received.v1 and trigger the Function, open another terminal window and run:
123456789
curl -v -X POST \
-H "ce-specversion: 1.0" \
-H "ce-type: order.received.v1" \
-H "ce-source: myapp" \
-H "ce-eventtypeversion: v1" \
-H "ce-id: cc99dcdd-6f6d-43d6-afef-d024eb276584" \
-H "content-type: application/json" \
-d "{\"orderCode\":\"3211213\", \"orderStatus\":\"received\"}" \
http://localhost:3000/publish
To publish the event order.changed.v1 and trigger the Function, in the same terminal window, run:
123456789
curl -v -X POST \
-H "ce-specversion: 1.0" \
-H "ce-type: order.changed.v1" \
-H "ce-source: myapp" \
-H "ce-eventtypeversion: v1" \
-H "ce-id: 94064655-7e9e-4795-97a3-81bfd497aac6" \
-H "content-type: application/json" \
-d "{\"orderCode\":\"3211213\", \"orderStatus\":\"changed\"}" \
http://localhost:3000/publish
In the eventing namespace, go to Workloads→Functions and choose your Function lastorder.
Find the section Replicas of the Function and choose the replica's name.
Under Containers, choose View Logs.
Result
If the events were delivered successfully, you see them in the logs as in the following example:
12345
> nodejs20-runtime@0.1.0 start
> node server.js
user code loaded in 0sec 0.654321ms
Received event: { orderCode: '3211213', orderStatus: 'received' }
Received event: { orderCode: '3211213', orderStatus: 'changed' }
Congratulations! You have successfully completed this exercise about the Eventing module in SAP BTP, Kyma runtime. You created a Subscription that subscribes to two event types and triggered a workload with an event.