IBM Cloud Docs
Enabling event notifications for Code Engine

Enabling event notifications for Code Engine

As an administrator of Code Engine, you might want to send events from other IBM Cloud services to Code Engine to trigger your apps or jobs.

To send information to Event Notifications, you must connect your Code Engine instance to Event Notifications. For more information about working with Event Notifications, see Getting started with Event Notifications.

How are events received?

Your Code Engine instance can work as a destination for Event Notifications.

When configured as a Destination, your Code Engine jobs or apps can be triggered in response to an event notification from another IBM Cloud service or even another Code Engine app or job.

What are some key features of Event Notifications

Review the following features for using Event Notifications with Code Engine

  • You can enable or disable pingSource which allows you to control scheduled event sources, like timers, by enabling or disabling them as needed. This is not possible when using Event Subscriptions.

  • You can control the number of retries and dead letter queues. When calling Code Engine, issues such as network errors or application glitches can cause the requests to fail. A retry is used to provide resiliency to external requests. To learn more, checkout Number of retries.

  • You're not limited to periodic timers. Event Notifications supports various event sources such as HTTP events, cloud service events (e.g., object uploads), functions, and Code Engine job outputs. This modularity enables the creation of flexible workflows that can respond to real-time events instead of just scheduled triggers. By combining multiple event sources, you can design complex, dynamic systems with ease.

  • You can create complex workflows. For example, a job can be triggered by a timer, then when the job completes, it can send an event back into Event Notifications as a source for other services. One scenario for this type of workflow would be having the event trigger an email notification or initiate another workflow.

  • Your jobruns are automatically deleted. By default, Event Notifications automatically deletes job runs after 10 minutes to prevent job-run quota issues. This helps ensure that stale or stuck jobs do not consume resources unnecessarily. It also helps to avoid exceeding Code Engine job quota. You can verify this behavior by observing the Job tab in your Code Engine project, where you'll see that the job run is automatically removed after the 10-minute window.

How are Event Notifications notifications different from subscriptions?

Subscriptions for Event Notifications allow your Code Engine apps or jobs to receive event information.

With Code Engine, your applications or jobs can receive events of interest by subscribing to event producers. Event information is received as POST HTTP requests for applications and as environment variables for jobs.

Code Engine supports the following types of event producers for subscriptions.

Cron
The cron event producer is based on cron-job and generates an event at regular intervals. Use a cron event producer when an action needs to be taken at well-defined intervals or at specific times.
IBM Cloud Object Storage
The Object Storage event producer generates events as changes are made to the objects in your object storage buckets. For example, as objects are added to a bucket, an application can receive an event and then perform an action based on that change, perhaps consuming that new object.
Kafka
The Kafka event producer watches for new messages to appear in a Kafka instance. When you create a Code Engine Kafka subscription for a set of topics, your app or job receives a separate event for each new message that appears in one of the topics.
Webhooks
You can use GitHub webhooks to send events from a GitHub repository to your Code Engine workload.

Alternatively, with Event Notifications you can configure Code Engine as a Destination.

When configured as a Destination, your Code Engine jobs or apps can be triggered in response to an event notification from another IBM Cloud service or even another Code Engine app or job.

Compare subscriptions and event notifications
Task Subscription Event Notifications
Defining event source Create a periodic timer cron-like event source that generates recurring time-based events. Use a pre-defined event source - Periodic Timer
Scheduling As a next step, Schedule the timer Create a topic, choose source as Periodic Timer. Configure using cronExpression
Choose end destination Select the job or application Create a end destination. Code Engine
Subscribe to event No explicit subscription needed. Create a subscription. Choose previously created destination and topic.

To learn more, see Getting started with subscriptions.

Setting up a trigger in the console

In this example, you use the IBM Cloud console to set up a timer trigger in Event Notifications that calls a Code Engine job every 6 hours.

Before you begin, make sure you have the following in your account.

  • An Event Notifications instance
  • A Code Engine project.
  • A target job within the project.

Complete the following steps to create the trigger.

  1. Set up a source in your Event Notifications instance.

    1. Set Up a Source
    2. Choose Periodic Timer as the event source.
    3. Set Up a Topic A Topic acts as a filter for events. You’ll use this to define the events of interest.
    4. Give your topic a name. For example, "Periodic-Trigger-6hr".
    5. Set the cron expression (0 */6 * * *) so that the topic emits events every 6 hours. Learn more about creating-a-topic-with-periodic-timer.
  2. Set up a destination. The destination is where the event is sent for processing. In this example, choose your existing Code Engine job from the list of available destinations. The job will act as the endpoint that will be triggered when the event occurs.

    Note: A service-to-service authorization is needed between Event Notifications and Code Engine. This can be configured by going to Manage > Access > Authorizations in the console. However, when you use the console, this part is handled automatically.

  3. Create a Subscription that links the Topic and the Destination.

    1. Select the "Periodic-Trigger-6hr" topic that you created earlier.

    2. Choose your Code Engine job as the destination.

The subscription ensures that when the event is emitted every 6 hours, the Code Engine job will be triggered. You can observe and verify the interval by reviewing the the Jobruns tab of your project.

The previous steps configured a job as the destination. However, this same procedure can be used if your destination an app. When setting up the destination, provide your app URL.

Setting up a trigger in the CLI

In this example, you use the IBM Cloud CLI to set up a timer trigger in Event Notifications that calls a Code Engine job every 6 hours.

Before you begin, make sure you have the following in your account.

  • A Code Engine project.
  • A target job within the project.

Complete the following steps to set up the trigger.

  1. Create an Event Notifications instance.
    ibmcloud resource service-instance-create "$EVENT_NOTIFICATIONS_INSTANCE_NAME" \
    "$EVENT_NOTIFICATIONS_SERVICE_NAME" "$EVENT_NOTIFICATIONS_PLAN" "$PROJECT_REGION" \
    -g "$PROJECT_RESOURCE_GROUP"
    
  2. Create an authorization policy.
    ibmcloud iam authorization-policy-create event-notifications codeengine Writer,Viewer \ 
    --source-service-instance-id $EVENT_NOTIFICATIONS_INSTANCE_ID \
    --target-service-instance-name "${PROJECT_NAME}"
    
  3. Create a destination by using your Code Engine job.
    CONFIG_JSON=$(cat <<EOF
    {
      "params": {
        "type": "job",
        "job_name": "${JOB_NAME}",
        "project_crn": "${PROJECT_CRN}"
      }
    }
    EOF
    )
    
    ibmcloud event-notifications destination-create \
      --instance-id "${EVENT_NOTIFICATIONS_INSTANCE_ID}" \
      --name "${DESTINATION_NAME}" \
      --type "ibmce" \
      --description "${DESTINATION_DESCRIPTION}" \
      --config "$CONFIG_JSON"
    
  4. Create an Event Notifications topic. Set the topic --topic-timer to 0 */6 * * * for a 6 hour interval.
    ibmcloud event-notifications topic create \
    --name "$TOPIC_NAME" \
    --description "$TOPIC_DESCRIPTION" \
    --service-instance-id "$EVENT_NOTIFICATIONS_CRN" \
    --topic-type timer \
    --topic-timer "$TOPIC_CRON" # Example: "0 */6 * * *" 
    
  5. Subscribe to the destination topic.
    ibmcloud event-notifications subscription create \
    --name "$SUBSCRIPTION_NAME" \
    --description "Triggers CE job from topic" \
    --service-instance-id "$EVENT_NOTIFICATIONS_CRN" \
    --topic-id "$TOPIC_ID" \
    --destination-id "$DESTINATION_ID"