Streaming Yandex Cloud Postbox events to Yandex Data Streams and analyzing them using Yandex DataLens
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
To configure event streaming and visualization:
- Prepare your cloud.
- Configure a Managed Service for YDB database.
- Create a Data Streams data stream.
- Prepare Yandex Cloud Postbox and Cloud DNS resources.
- Prepare Cloud Functions resources.
- Send emails.
- Configure visualization in DataLens.
If you no longer need the resources you created, delete them.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required 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 account
- In the management console
, select the appropriate folder. - In the list of services, select Identity and Access Management.
- Click Create service account.
- Enter a name for the service account:
yds-functions
. - Click
Add role and select theyds.editor
andfunctions.functionInvoker
roles. - Click Create.
Configure 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 database
- In the management console
, select the appropriate folder. - Select Managed Service for YDB.
- Click Create a database.
- Specify the DB Name,
postbox-events-ydb
. - Under Database type, select
Serverless
. - 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 table
-
On the Databases page, select the
postbox-events-ydb
DB. -
To open the DB root directory, go to the
Navigation tab. -
To create a DB query, click New SQL query in the upper right corner. The Query page opens.
-
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) )
-
Click
Run.After you run the query, the
postbox_events
table will be created.
Create a Data Streams data stream
Create a data stream to log events.
- In the management console
, select Data Streams. - Click Create stream.
- Specify the
postbox-events-ydb
database. - Enter a name for the data stream:
postbox-events-stream
. - Select the
Actual usage
pricing plan. - 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 resources
Create an address to send emails from, verify your domain ownership, and configure event logging to Data Streams.
Create an address
-
On your computer, generate the
privatekey.pem
key to create a DKIM signature:Linuxopenssl genrsa -out privatekey.pem 2048
-
Create an address:
Management console- In the management console
, select Cloud Postbox. - Click Create address.
- In the Domain field, specify the domain to send emails from. The domain can be of any level.
- 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. - In the Private key field, copy the contents of the
privatekey.pem
file. - Click Create address.
- In the management console
Pass 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
-
In the management console
, select the folder containing the address and your domain zone. -
Select Cloud DNS.
-
Select your domain zone.
-
Click Create record.
-
In the Name field, specify the name generated when creating the address, omitting the domain. For example,
postbox._domainkey.
.Note
For other DNS services, you may need to copy the entire record. The final record must look like this:
postbox._domainkey.example.com.
. -
In the Type list, select
TXT
. -
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"
-
Click Create.
-
Go to Cloud Postbox.
-
Select the address you created.
-
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 configuration
-
In the management console
, select Cloud Postbox. -
In the left-hand panel, select
Configurations. -
Click Create configuration.
-
Enter the configuration name:
postbox-events-config
. -
Under Subscriptions, click Add:
- Enter a name for the subscription:
postbox-events-subscribe
. - Select the
postbox-events-stream
data stream. - Toggle the Enabled option on to activate the subscription.
- Enter a name for the subscription:
-
Click Create configuration.
Create 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 code
Downloadpostbox-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:
-
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
andrequirements.txt
files with the function code. -
Create an archive named
postbox-events.zip
and add theindex.py
andrequirements.txt
files to it.
Get the DB connection details
-
Go to the management console
. -
Select the folder hosting your DB and go to Managed Service for YDB.
-
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 function
To create a function, you will need the function code and DB connection details.
-
In the management console
, select Cloud Functions. -
Create a function:
- Click Create function.
- In the Name field, enter a name for the function, e.g.,
postbox-events-function
. - Click Create.
-
Create a function version:
-
In the Editor window that opens, select
Python 3.12
. -
Disable the Add files with code examples option.
-
Click Continue.
-
In the Method field, select
ZIP archive
. -
Click Attach file and select the
postbox-events.zip
archive. -
In the Entry point field, specify
index.handler
. -
Under Parameters, specify:
-
Timeout, sec:
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
-
-
Click Save changes.
-
Create a trigger
- In the management console
, select Cloud Functions. - In the left-hand panel, select
Triggers. - Click Create trigger.
- In the Name field, enter a name for the trigger, e.g.,
postbox-events-trigger
. - In the Type field, select
Data Streams
. - In the Launched resource field, select
Function
. - Under Data Streams settings, select the
postbox-events-stream
data stream and theyds-functions
service account. - Under Function settings, select
postbox-events-function
and theyds-functions
service account. - Click Create trigger.
Send emails
-
Create a service account named
postbox-user
with thepostbox.sender
role and static access keys. -
Send several test emails using the address you created earlier.
-
Make sure the emails were sent: check the inbox of the address you sent the emails to.
-
Make sure data is delivered to the services:
Management console-
Check the data stream:
-
In the management console
, select Data Streams. -
Select the data stream named
postbox-events-stream
. -
Go to the
Data viewer tab.The charts should display data on the emails you sent.
-
-
Check the DB:
-
In the management console
, select Managed Service for YDB. -
Select the
postbox-events-ydb
DB. -
Go to the
Navigation tab. -
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 DataLens
To monitor the emails you send, set up a connection, create a dataset, charts, and a dashboard.
Create a connection
-
Go to the DataLens home page
. -
Click Create connection.
-
Select the YDB connector.
-
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
.
-
Click Create connection.
-
Enter a connection name, e.g.,
postbox-events-connection
, and click Create.
Create a dataset
- Go to the DataLens home page
. - Click Create dataset.
- In the left-hand panel, click
Add. - Select a
postbox-events-connection
connection. - In the left-hand menu, select the
postbox-events
table and drag it to the right. - Click Save.
- In the window that opens, specify
postbox-events-dataset
and click Create.
Create 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 day
-
Go to the DataLens home page
. -
Click Create chart.
-
In the left-hand panel, click
Select dataset and selectpostbox-events-dataset
. -
Select Bar chart as the chart type.
-
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.
-
Click Save.
-
In the window that opens, specify the chart name,
Events by day
, and click Save.
Event list
-
Go to the DataLens home page
. -
Click Create chart.
-
In the left-hand panel, click
Select dataset and selectpostbox-events-dataset
. -
Select Table as the chart type.
-
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.
-
Drag the
delivery_timestamp
dimension to the Sorting section to sort the table by delivery time. -
Click Save.
-
In the window that opens, specify the chart name,
Event list
, and click Save.
Create a dashboard
-
Go to the DataLens home page
. -
Click Create dashboard.
-
Add the
Events by day
,Event list
, and other charts you created earlier to your dashboard:- In the bottom panel, click Chart.
- In the Chart field of the window that opens, click Select and select
Events by day
. - Click Add.
- Repeat these steps to add the other charts.
-
Change the size and position of your charts.
-
Click Save.
-
In the window that opens, enter a name for the dashboard:
postbox-events-dashboard
. -
Click Create.
After completing this tutorial, you can use charts and tables to analyze events.
How to delete the resources you created
To stop paying for the resources you created: