Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Tutorials
    • All tutorials
    • URL shortener
    • Entering data into storage systems
    • Storing application runtime logs
    • Deploying a web application using the Java Servlet API
    • Developing a Slack bot
    • Developing a Telegram bot
    • Developing a custom integration in API Gateway
    • Developing CRUD APIs for movie services
    • Building a CI/CD pipeline in GitLab
    • Working with an API gateway via WebSocket
    • Creating an interactive serverless application using WebSocket
    • Automatically copying objects from one Object Storage bucket to another
    • Visualizing logs in Grafana using the Cloud Logging plugin
    • Canary release of a Cloud Functions function
    • Interactive debugging of Cloud Functions functions
    • Creating a Node.js function using TypeScript
    • Running a containerized app in Serverless Containers
    • Streaming Yandex Cloud Postbox events to Data Streams and analyzing them using DataLens
    • Using API Gateway to set up speech synthesis in SpeechKit
    • Connecting to YDB from a Cloud Functions function in Python
    • Connecting to a YDB database from a Cloud Functions function in Node.js
    • API Gateway protection with Smart Web Security
    • Deploying a web app with JWT authorization in API Gateway and authentication in Firebase
    • Automatic data upload to Yandex SpeechSense using Yandex Workflows
    • Configuring responses in Cloud Logging and Yandex Cloud Functions
    • Setting up Workflows integration with Tracker, YandexGPT, and Yandex Cloud Postbox
    • Developing functions in Functions Framework and deploying them to Yandex Serverless Containers

In this article:

  • Prepare your cloud
  • Required paid resources
  • Create a service account
  • Configure a Managed Service for YDB database
  • Create a database
  • Create a table
  • Create a Data Streams data stream
  • Prepare the Yandex Cloud Postbox and Cloud DNS resources
  • Create an address
  • Pass domain ownership verification
  • Create the configuration
  • Create Cloud Functions resources
  • Get the function code
  • Get the DB connection details
  • Create a function
  • Create a trigger
  • Send emails
  • Configure visualization in DataLens
  • Create a connection
  • Create a dataset
  • Create charts
  • Create a dashboard
  • How to delete the resources you created
  1. Serverless technologies
  2. Streaming Yandex Cloud Postbox events to Data Streams and analyzing them using DataLens

Streaming Yandex Cloud Postbox events to Yandex Data Streams and analyzing them using Yandex DataLens

Written by
Yandex Cloud
Updated at May 7, 2025
  • Prepare your cloud
    • Required paid resources
    • Create a service account
  • Configure a Managed Service for YDB database
    • Create a database
    • Create a table
  • Create a Data Streams data stream
  • Prepare the Yandex Cloud Postbox and Cloud DNS resources
    • Create an address
    • Pass domain ownership verification
    • Create the configuration
  • Create Cloud Functions resources
    • Get the function code
    • Get the DB connection details
    • Create a function
    • Create a trigger
  • Send emails
  • Configure visualization in DataLens
    • Create a connection
    • Create a dataset
    • Create charts
    • Create a dashboard
  • How to delete the resources you created

In this tutorial you will configure streaming of Yandex Cloud Postbox events to Yandex Data Streams and their visualization in Yandex DataLens for later analysis. The events are produced by outgoing emails.

You can implement all the solutions from this tutorial in the Yandex Cloud management console; no development skills are required. To process and store data, you will use Yandex Cloud Functions and Yandex Managed Service for YDB.

To configure event streaming and visualization:

  1. Prepare your cloud.
  2. Configure a Managed Service for YDB database.
  3. Create a Data Streams data stream.
  4. Prepare Yandex Cloud Postbox and Cloud DNS resources.
  5. Prepare Cloud Functions resources.
  6. Send emails.
  7. Configure visualization in DataLens.

If you no longer need the resources you created, delete them.

Prepare your cloudPrepare your cloud

Sign up in Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or register a new account.
  2. On the Yandex Cloud Billing page, make sure you have a billing account linked and it has the ACTIVE or TRIAL_ACTIVE status. If you do not have a billing account, create one and link a cloud to it.

If you have an active billing account, you can navigate to the cloud page to create or select a folder for your infrastructure to operate in.

Learn more about clouds and folders.

Required paid resourcesRequired paid resources

The infrastructure support costs include:

  • Fee for YDB operations and data storage (see Yandex Managed Service for YDB pricing).
  • Data Streams data storage fee (see Yandex Data Streams pricing).
  • Fee for the number of function calls, computing resources allocated to executing the function, and outgoing traffic (see Cloud Functions pricing).
  • Fee for using Yandex Cloud Postbox (see Yandex Cloud Postbox pricing).
  • Fee for the DataLens plan (see Yandex DataLens pricing).

Create a service accountCreate a service account

Management console
  1. In the management console, select the appropriate folder.
  2. In the list of services, select Identity and Access Management.
  3. Click Create service account.
  4. Enter a name for the service account: yds-functions.
  5. Click Add role and select the yds.editor and functions.functionInvoker roles.
  6. Click Create.

Configure a Managed Service for YDB databaseConfigure a Managed Service for YDB database

You can use any DBMS suitable for analytical tasks as event storage. In this tutorial, we use the Managed Service for YDB serverless database. If you use another DBMS, the SQL query for creating a table and code for loading events into that table may be different.

Create a databaseCreate a database

Management console
  1. In the management console, select the appropriate folder.
  2. Select Managed Service for YDB.
  3. Click Create a database.
  4. Specify the DB Name, postbox-events-ydb.
  5. Under Database type, select Serverless.
  6. Click Create a database.

Wait for the DB to start. While being created, your database will have the Provisioning status. Once it is ready for use, its status will change to Running.

Create a tableCreate a table

Management console
  1. On the Databases page, select the postbox-events-ydb DB.

  2. To open the DB root directory, go to the Navigation tab.

  3. To create a DB query, click New SQL query in the upper right corner. The Query page opens.

  4. In the Query field, enter:

    CREATE TABLE postbox_events
    (
        saved_datetime Datetime NOT NULL,
        eventid String NOT NULL,
        eventtype String,
        mail_timestamp Timestamp,
        mail_messageid String,
        mail_ch_from String,
        mail_ch_to String, 
        mail_ch_messageid String,
        mail_ch_subject String,
        delivery_timestamp Timestamp,
        delivery_time_ms Uint64,
        delivery_recipients String,
        bounce_bounceType String,
        bounce_bounceSubType String,
        bounce_bouncedRecipients String,
        bounce_timestamp Timestamp,
        -- message Json,
        PRIMARY KEY (saved_datetime, eventid)
    )
    
  5. Click Run.

    After you run the query, the postbox_events table will be created.

Create a Data Streams data streamCreate a Data Streams data stream

Create a data stream to log events.

Management console
  1. In the management console, select Data Streams.
  2. Click Create stream.
  3. Specify the postbox-events-ydb database.
  4. Enter a name for the data stream: postbox-events-stream.
  5. Select the Actual usage pricing plan.
  6. Click Create.

Wait for the stream to start. Once the stream is ready for use, its status will change from Creating to Active.

Prepare the Yandex Cloud Postbox and Cloud DNS resourcesPrepare the Yandex Cloud Postbox and Cloud DNS resources

Create an address to send emails from, verify your domain ownership, and configure event logging to Data Streams.

Create an addressCreate an address

  1. On your computer, generate the privatekey.pem key to create a DKIM signature:

    Linux
    openssl genrsa -out privatekey.pem 2048
    
  2. Create an address:

    Management console
    1. In the management console, select Cloud Postbox.
    2. Click Create address.
    3. In the Domain field, specify the domain to send emails from. The domain can be of any level.
    4. In the Selector field, choose a selector, e.g., postbox. You should only use this selector in that single resource record you create at domain ownership verification.
    5. In the Private key field, copy the contents of the privatekey.pem file.
    6. Click Create address.

Pass domain ownership verificationPass domain ownership verification

To send emails, confirm domain ownership. After creating an address, DKIM signature settings will be generated on its page. Specify them as the values of the resource record you need to add to your domain zone. You can add a record with your registrar or in Yandex Cloud DNS if you have delegated your domain.

Example of creating a resource record in Cloud DNS

Management console
  1. In the management console, select the folder containing the address and your domain zone.

  2. Select Cloud DNS.

  3. Select your domain zone.

  4. Click Create record.

  5. In the Name field, specify the name portion generated when creating the address, omitting the domain in <selector>._domainkey format, e.g., postbox._domainkey.

    Note

    For other DNS services, you may need to copy the entire record. The final record must look like this: <selector>._domainkey.<domain>., e.g., postbox._domainkey.example.com..

  6. In the Type list, select TXT.

  7. Copy the contents of the Value field from the Signature verification section and paste it into the Data field. Note that the record value must be enclosed in quotes, for example:

    "v=DKIM1;h=sha256;k=rsa;p=M1B...aCA8"
    
  8. Click Create.

  9. Go to Cloud Postbox.

  10. Select the address you created.

  11. Click Verify address. If the record is correct, the verification status on the address page will change to Success.

DNS server responses are cached, so delays may occur when updating a resource record.

Create the configurationCreate the configuration

Management console
  1. In the management console, select Cloud Postbox.

  2. In the left-hand panel, select Configurations.

  3. Click Create configuration.

  4. Enter the configuration name: postbox-events-config.

  5. Under Subscriptions, click Add:

    1. Enter a name for the subscription: postbox-events-subscribe.
    2. Select the postbox-events-stream data stream.
    3. Toggle the Enabled option on to activate the subscription.
  6. Click Create configuration.

Create Cloud Functions resourcesCreate Cloud Functions resources

Create a function to send data from the stream to the DB, and a trigger to invoke the function when events are logged in the data stream.

Get the function codeGet the function code

Archive
Repository

Download the postbox-events.zip archive to your computer. The archive contains the index.py and requirements.txt files with the function code.

On your computer, do the following:

  1. Clone the repository with the function code:

    git clone https://github.com/yandex-cloud-examples/yc-postbox-events/blob/main/build/postbox-events.git
    

    Go to the directory with the repository. Make sure it contains the index.py and requirements.txt files with the function code.

  2. Create an archive named postbox-events.zip and add the index.py and requirements.txt files to it.

Get the DB connection detailsGet the DB connection details

Management console
  1. Go to the management console.

  2. Select the folder hosting your DB and go to Managed Service for YDB.

  3. Select the DB you want to get the endpoint and path for.

    • The DB endpoint is specified under Connection in the first part of the Endpoint field value (preceding /?database=):

      For example, this is the endpoint of a DB in serverless mode: grpcs://ydb.serverless.yandexcloud.net:2135.

    • The DB path is specified under Connection in the second part of the Endpoint field value (following /?database=).

      Example of a DB path: /ru-central1/b1gia87mbaomkfvs6rgl/etnudu2n9ri35luqe4h1.

Create a functionCreate a function

To create a function, you will need the function code and DB connection details.

Management console
  1. In the management console, select Cloud Functions.

  2. Create a function:

    1. Click Create function.
    2. In the Name field, enter a name for the function, e.g., postbox-events-function.
    3. Click Create.
  3. Create a function version:

    1. In the Editor window that opens, select Python 3.12.

    2. Disable the Add files with code examples option.

    3. Click Continue.

    4. In the Method field, select ZIP archive.

    5. Click Attach file and select the postbox-events.zip archive.

    6. In the Entry point field, specify index.handler.

    7. Under Parameters, specify:

      • Timeout: 10

      • Memory: 128 MB

      • Service account: yds-functions

      • Environment variables:

        Key Description Sample value
        YDB_DATABASE DB path /ru-central1/b1go123e9vjq********/etnu15kr22********
        YDB_ENDPOINT DB endpoint grpcs://ydb.serverless.yandexcloud.net:2135
        YDB_TABLE Table name postbox_events
    8. Click Save changes.

Create a triggerCreate a trigger

Management console
  1. In the management console, select Cloud Functions.
  2. In the left-hand panel, select Triggers.
  3. Click Create trigger.
  4. In the Name field, enter a name for the trigger, e.g., postbox-events-trigger.
  5. In the Type field, select Data Streams.
  6. In the Launched resource field, select Function.
  7. Under Data Streams settings, select the postbox-events-stream data stream and the yds-functions service account.
  8. Under Function settings, select postbox-events-function and the yds-functions service account.
  9. Click Create trigger.

Send emailsSend emails

  1. Create a service account named postbox-user with the postbox.sender role and static access keys.

  2. Send several test emails using the address you created earlier.

  3. Make sure the emails were sent: check the inbox of the address you sent the emails to.

  4. Make sure data is delivered to the services:

    Management console
    1. Check the data stream:

      1. In the management console, select Data Streams.

      2. Select the data stream named postbox-events-stream.

      3. Go to the Data viewer tab.

        The charts should display data on the emails you sent.

    2. Check the DB:

      1. In the management console, select Managed Service for YDB.

      2. Select the postbox-events-ydb DB.

      3. Go to the Navigation tab.

      4. Select the postbox_events table.

        You should see records in the table. Some columns may have the NULL value: this depends on the type of notification received from Yandex Cloud Postbox.

Configure visualization in DataLensConfigure visualization in DataLens

To monitor the emails you send, set up a connection, create a dataset, charts, and a dashboard.

Create a connectionCreate a connection

DataLens interface
  1. Go to the DataLens home page.

  2. Click Create connection.

  3. Select the YDB connector.

  4. Specify the connection parameters:

    • Cloud and folder: Select the folder you are using to complete this tutorial.
    • Service account: yds-functions.
    • Database: postbox-events-ydb.
    • Cache TTL in seconds: Default.
    • SQL query access level: Allow subqueries from datasets and queries from charts.
  5. Click Create connection.

  6. Enter a connection name, e.g., postbox-events-connection, and click Create.

Create a datasetCreate a dataset

DataLens interface
  1. Go to the DataLens home page.
  2. Click Create dataset.
  3. In the left-hand panel, click Add.
  4. Select a postbox-events-connection connection.
  5. In the left-hand menu, select the postbox-events table and drag it to the right.
  6. Click Save.
  7. In the window that opens, specify postbox-events-dataset and click Create.

Create chartsCreate charts

In this tutorial, we will create the Events by day and Event list charts as an example. You can create other charts in the same way.

Events by dayEvents by day

DataLens interface
  1. Go to the DataLens home page.

  2. Click Create chart.

  3. In the left-hand panel, click Select dataset and select postbox-events-dataset.

  4. Select Bar chart as the chart type.

  5. Drag the dimensions as follows:

    • delivery_timestamp to the X section: Dates will be plotted on the X axis.
    • eventid to the Y section: Number of events will be plotted on the Y axis.
    • eventtype to the Colors section: This enables using a different color for each event type.
  6. Click Save.

  7. In the window that opens, specify the chart name, Events by day, and click Save.

Event listEvent list

DataLens interface
  1. Go to the DataLens home page.

  2. Click Create chart.

  3. In the left-hand panel, click Select dataset and select postbox-events-dataset.

  4. Select Table as the chart type.

  5. Drag the following dimensions to the Columns section:

    • saved_datetime: Date and time of saving the event.
    • eventid: Event type.
    • mail_ch_to: Address of the recipient.
    • delivery_timestamp: Email delivery date and time.
    • delivery_time_ms: Delivery time in milliseconds.
    • mail_ch_subject: Email subject.
    • bounce_bounceType: Delivery error type.
    • bounce_bounceSubType: Non-delivery message.
  6. Drag the delivery_timestamp dimension to the Sorting section to sort the table by delivery time.

  7. Click Save.

  8. In the window that opens, specify the chart name, Event list, and click Save.

Create a dashboardCreate a dashboard

DataLens interface
  1. Go to the DataLens home page.

  2. Click Create dashboard.

  3. Add the Events by day, Event list, and other charts you created earlier to your dashboard:

    1. In the bottom panel, click Chart.
    2. In the Chart field of the window that opens, click Select and select Events by day.
    3. Click Add.
    4. Repeat these steps to add the other charts.
  4. Change the size and position of your charts.

  5. Click Save.

  6. In the window that opens, enter a name for the dashboard: postbox-events-dashboard.

  7. Click Create.

After completing this tutorial, you can use charts and tables to analyze events.

How to delete the resources you createdHow to delete the resources you created

To stop paying for the resources you created:

  1. Delete the trigger invoking the function in Cloud Functions.
  2. Delete the Cloud Functions function.
  3. Delete the Managed Service for YDB database.
  4. Delete the Data Streams stream.
  5. Delete the Yandex Cloud Postbox address and configuration.

Was the article helpful?

Previous
Running a containerized app in Serverless Containers
Next
Using API Gateway to set up speech synthesis in SpeechKit
Yandex project
© 2025 Yandex.Cloud LLC