Creating a Subscription using Kyma Eventing

Objective

After completing this lesson, you will be able to create a subscription

Create a Subscription

Prerequisites

Before you begin, ensure that the following prerequisites are met:

Usage Scenario

Create a Subscription that subscribes to multiple event types, and trigger the workload with an event in SAP BTP, Kyma runtime.

After you have learned that you can subscribe to an event using SAP BTP, Kyma runtime, you will put this knowledge into practice by subscribing to more than one event type using a Subscription custom resource. You will also trigger the events and verify they have been delivered successfully.

Note

We strongly recommend that you use a Free Tier Service, as services which aren't included in Free Tier lead to additional costs for you.

Task Flow

In this exercise, you will perform the following steps:

  1. Set up a new namespace.
  2. Create a Function.
  3. Set up a Subscription with various event types.
  4. Trigger the workload with an event.
  5. Confirm event delivery.

Task 1: Configure the NATS back end

Steps

  1. In Kyma dashboard, choose Modify Modules.

  2. Select the Eventing module and choose Edit.

  3. Choose NATS as the back end type.

  4. Choose Save.

Result

When you choose the backed type, the Eventing module's state becomes Ready.

Task 2: Create a new namespace

Steps

  1. From the SAP BTP cockpit, follow the link to Kyma dashboard.

  2. Go to Namespaces and select Create.

  3. 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

  1. In Kyma dashboard, select the namespace you created in the previous task.

  2. Go to WorkloadsFunctions.

    The namespace does not contain Functions yet.

    Create a function
  3. Select Create and enter the following parameters:

    • Name: lastorder
    • Language: JavaScript
    • Runtime: Node.js 20
  4. Replace the source of the Function's editor:

    JavaScript
    123456
    module.exports = { main: async function (event, context) { console.log("Received event:", event.data); return; } }
  5. Select Create.

Result

If successful, after a few seconds, the Function's status is Running.

Task 4: Subscribe to various event types

Steps

  1. In the eventing namespace, go to ConfigurationSubscriptions, and select Create.

  2. Set up the Subscription with the following parameters:

    • Name: lastorder-sub
    • Types: order.received.v1 and order.changed.v1
    • Service: lastorder
  3. Switch to the YAML tab.

  4. In the spec section, add source: myapp.

    Code Snippet
    1234
    ... spec: source: myapp ...
  5. 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.

  1. Open a terminal window and run:

    Code Snippet
    1
    kubectl -n kyma-system port-forward service/eventing-publisher-proxy 3000:80
  2. To publish the event order.received.v1 and trigger the Function, open another terminal window and run:

    Code Snippet
    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
  3. To publish the event order.changed.v1 and trigger the Function, in the same terminal window, run:

    Code Snippet
    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
  4. In the eventing namespace, go to WorkloadsFunctions and choose your Function lastorder.

  5. Find the section Replicas of the Function and choose the replica's name.

  6. Under Containers, choose View Logs.

Result

If the events were delivered successfully, you see them in the logs as in the following example:

Code Snippet
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.