Creating a Subscription using Kyma Eventing

Objective

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

Create a Subscription

Business Scenario

Create a subscription that subscribes to multiple event types, and trigger the workload with an event on 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..

Live Environment

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: Set up a New Namespace

Steps

  1. Set up a new namespace.

    In this task, you will set up a new, clean namespace with the name eventing.

    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.

    You see the namespace details for your new namespace. Its status should be Active.

  2. Create a Function.

    In this task, you will set up a new Function with the name lastorder.

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

    2. Go to WorkloadsFunctions.

      You see that the namespace contains no 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

      After some seconds, you see the Function in Kyma dashboard. Its status should be Running.

  3. Set up a Subscription with Various Event Types.

    In this task, you will create a subscription CR and thus, subscribe to the events order.received.v1 and order.changed.v1:

    1. In the Kyma dashboard, 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
      • Source:myapp
    3. Select Create

    4. After some seconds, you see your subscription in Kyma dashboard. Its status should be Ready.

  4. Trigger the Workload with an Event.

    In this task, you will publish the events order.received.v1 and order.changed.v1 and trigger the Function.

    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
      12
      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 the following :

      Code Snippet
      12345678910
      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 the following:

      Code Snippet
      12345678910
      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
  5. Confirm Event Delivery

    In this task, you will make sure that the events you just triggered were successfully delivered.

    1. In Kyma dashboard, go to WorkloadsFunctions and select your Function lastorder.

    2. Find the section Replicas of the Function and select the name.

    3. Under Containers, select View Logs.

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

      Code Snippet
      123456
      > 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' }

Result

Congratulations! You have successfully completed this exercise about the Eventing module on SAP BTP, Kyma runtime. You created a subscription that subscribes to various event types and triggered the workload with an event.

Log in to track your progress & complete quizzes