Streaming Yandex Cloud Postbox events to Yandex Data Streams and analyzing them with Yandex DataLens
- Get your cloud ready
- Create service accounts
- Create a static access key
- Set up a Managed Service for YDB database
- Create a data stream in Data Streams
- Create Yandex Cloud Postbox resources
- Create Cloud Functions resources
- Send emails
- Configure visualization in DataLens
- How to delete the resources you created
In this tutorial, you will set up streaming of Yandex Cloud Postbox events to Yandex Data Streams and their visualization in Yandex DataLens for further analysis. Sending emails generates events.
You can implement this solution using the Yandex Cloud management console
To set up event streaming and visualization:
- Get your cloud ready.
- Create service accounts.
- Create a static access key.
- Set up a Managed Service for YDB database.
- Create a data stream in Data Streams.
- Set up Yandex Cloud Postbox resources.
- Set up Cloud Functions resources.
- Send emails.
- Configure visualization in DataLens.
If you no longer need the resources you created, delete them.
Get your cloud ready
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. 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
Learn more about clouds and folders here.
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 function invocation count, computing resources allocated to run the function, and outbound 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 service accounts
Create two service accounts:
yds-functionsto call Cloud Functions and write data to the YDB database.postbox-userto send emails via Yandex Cloud Postbox.
-
Create a
yds-functionsservice account:Management console- In the management console
, select the folder where you are going to create your infrastructure. - In the list of services, select Identity and Access Management and click Create service account.
- In the Name field, enter
yds-functionsfor name. - Click
Add role and select theyds.editorandfunctions.functionInvokerroles. - Click Create.
- In the management console
-
Similarly, create a service account named
postbox-userand assign thepostbox.senderrole to it.
Create a static access key
Create a static access key to use for sending emails:
-
In the management console
, select the folder where you are deploying your infrastructure. -
In the list of services, select Identity and Access Management and then select
postbox-userfrom the list of service acounts. In the window that opens, do the following:-
In the top panel, click
Create new key and select Create static access key. -
Click Create.
-
Save the ID and secret key.
Alert
After you close this dialog, the key value will no longer be available.
-
Set up a Managed Service for YDB database
You can use any DBMS suitable for analytical tasks as event storage. In this tutorial, we use a Managed Service for YDB serverless database. For other 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 folder where you are deploying your infrastructure. - In the list of services, select Managed Service for YDB and click Create a database.
- In the Name field, specify
postbox-events-ydb. - In the Database type field, select
Serverless. - Do not edit the other database settings. Click Create a database.
Wait for the database 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 new
postbox-events-ydbDB. -
To open the DB root directory, navigate to the
Navigation tab. -
To create a DB query, click New SQL query in the top-right corner.
-
In the Query box that opens, enter the following:
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, a new
postbox_eventstable will appear in your database.
Create a data stream in Data Streams
Create a data stream to log Yandex Cloud Postbox events:
- In the management console
, select the folder where you are deploying your infrastructure. - In the list of services, select Data Streams and click Create stream.
- In the Database field, select the
postbox-events-ydbdatabase you created earlier. - In the Name field, enter the stream name:
postbox-events-stream. - In the Pricing Mode field, select
Actual usage. - Do not edit the other data stream settings. Click Create.
Wait for the stream to start. Once the stream is ready for use, its status will change from Creating to Active.
Create Yandex Cloud Postbox resources
In Yandex Cloud Postbox, configure event logging to Data Streams, create a sender address, and verify your domain ownership.
Create a Yandex Cloud Postbox configuration
-
In the management console
, select the folder where you are deploying your infrastructure. -
In the list of services, select Cloud Postbox.
-
In the left-hand panel, select
Configurations and click Create configuration. -
In the Name field, specify the configuration name:
postbox-events-config. -
Under Subscriptions, click Add and fill out the form that opens:
- In the Name field, specify the subscription name:
postbox-events-subscribe. - In the Data stream field, select the data stream named
postbox-events-stream. - Toggle the Enabled option on to activate the subscription.
- In the Name field, specify the subscription name:
-
Do not edit the other configuration settings. Click Create configuration.
Create a Yandex Cloud Postbox address
-
Generate the
privatekey.pemkey to create a DKIM signature by running this command in the terminal:openssl genrsa -out privatekey.pem 2048Note
opensslcomes preinstalled in Linux and macOS. If using Windows, you need to install it manually. For more information, visit the project's website . -
Create a Yandex Cloud Postbox address:
Management console- In the management console
, select the folder where you are deploying your infrastructure. - In the list of services, select Cloud Postbox.
- In the left-hand panel, select
Addresses and click Create address. - In the Domain field, specify the domain you will use to send emails. You can use a domain of any level, as long as you own it.
- In the Selector field, specify a selector, e.g.,
postbox. Make sure to only use this selector in the resource record you create when verifying domain ownership. - In the Configuration field, select
postbox-events-configyou previously created. - In the Private key field, paste the contents of the
privatekey.pemfile from the previous step. - Do not change the other address settings. Click Create address.
- Click the newly created address in the list that opens.
- Under Signature verification on the address info page, copy and save the TXT record from the Value field.
- In the management console
Pass a domain ownership check
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 Yandex Cloud domain.
Example of creating a resource record in Yandex Cloud DNS
-
In the management console
, select the folder containing the address and your domain zone.If you do not have a public DNS zone yet, create one:
-
Select Cloud DNS.
-
Select your domain zone.
-
Click Create record.
-
In the Name field, specify the name portion generated when creating the address, omitting the domain in
<selector>._domainkeyformat, 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.. -
In the Type field, 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 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.gitNavigate to the repository directory. Make sure it contains the
index.pyandrequirements.txtfiles with the function code. -
Create an archive named
postbox-events.zipand addindex.pyandrequirements.txtto it.
Get the DB connection details
-
In the management console
, select the folder where you are deploying your infrastructure. -
In the list of services, select Managed Service for YDB.
-
Select the DB for which you need to get the endpoint and path.
-
The DB endpoint is specified under Connection in the first part of the Endpoint field value (preceding
/?database=):For example, the endpoint of a serverless DB is
grpcs://ydb.serverless.yandexcloud.net:2135. -
The DB path is specified under Connection in the second part of the Endpoint field value (following
/?database=).Here is an 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 the folder where you are deploying your infrastructure. -
In the list of services, 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 Add files with code examples.
-
Click Continue.
-
In the Method field, select
ZIP archive. -
Click Attach file and select
postbox-events.zip. -
In the Entry point field, specify
index.handler. -
Under Parameters, specify:
-
Timeout:
10 seconds. -
Memory:
128 MB. -
Service account:
yds-functions. -
Environment variables:
Key Description Value (example) YDB_DATABASEDB path /ru-central1/b1go123e9vjq********/etnu15kr22********YDB_ENDPOINTDB endpoint grpcs://ydb.serverless.yandexcloud.net:2135YDB_TABLETable name postbox_events
-
-
Click Save changes.
-
Create a trigger
- In the management console
, select the folder where you are deploying your infrastructure. - In the list of services, select Cloud Functions.
- In the left-hand panel, select
Triggers and click Create trigger. - In the Name field, enter the trigger name:
postbox-events-trigger. - In the Type field, select
Data Streams. - In the Launched resource field, select
Function. - Under Data Streams settings, select the data stream named
postbox-events-streamand theyds-functionsservice account. - Under Function settings, select the
postbox-events-functionfunction and theyds-functionsservice account. - Click Create trigger.
Send emails
-
Use the Yandex Cloud Postbox address and static access key you created earlier to send several test emails in any way you prefer.
-
Make sure the emails were delivered by checking the mailbox you sent them to.
-
Make sure the data about sent emails reaches the services:
Management console-
Check the data stream:
-
In the management console
, select the folder where you are deploying your infrastructure. -
In the list of services, select Data Streams.
-
Select the data stream named
postbox-events-stream. -
Navigate to the
Data viewer tab.The charts should show email sending events.
-
-
Check the DB:
-
In the management console
, select the folder where you are deploying your infrastructure. -
In the list of services, select Managed Service for YDB.
-
Select the
postbox-events-ydbdatabase. -
Go to the
Navigation tab. -
Select the
postbox_eventstable.The table should show new records. Some columns may have the
NULLvalue: 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
. -
In the left-hand panel, select
Connections and click Create connection. -
Select the YDB connector.
-
Configure the connection as follows:
- 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. - Enable the SQL query access level option and select
Allow subqueries in datasets.
-
Click Create connection.
-
In the window that opens, select a workbook for your new connection and click Create.
-
Enter a connection name, e.g.,
postbox-events-connection, and click Create.
Create a dataset
- Go to the DataLens home page
. - In the left-hand panel, select
Datasets and click Create dataset. - In the window that opens, select the workbook your new connection is in and click Create.
- Under Connections, click
Add and select thepostbox-events-connectionconnection you created earlier. - Under Tables, select the
postbox-eventstable and drag it to the right. - Click Save.
- In the window that opens, specify
postbox-events-datasetand 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
. -
In the left-hand panel, select
Charts. Click Create chart and select Chart in Wizard. -
In the window that opens, select the workbook your new connection is in and click Create.
-
In the left-hand section, click
Select dataset and selectpostbox-events-dataset. -
Select Column chart as the chart type.
-
Drag the dimensions as follows:
delivery_timestampto the X section: Dates will be plotted along the X axis.eventidto the Y section: Number of events will be plotted on the Y axis.eventtypeto 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
. -
In the left-hand panel, select
Charts. Click Create chart and select Chart in Wizard. -
In the window that opens, select the workbook your new connection is in and click Create.
-
In the left-hand section, 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: Recipient's email address.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 report.
-
Drag the
delivery_timestampdimension 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
. -
In the left-hand panel, select
Dashboards and click Create dashboard. -
In the window that opens, select the workbook your new connection is in and click Create.
-
Add
Events by day,Event list, and other charts you created earlier to your dashboard:- In the bottom panel, click Chart.
- In the window that opens, click Select and select the
Events by daychart. - Click Add.
- Repeat these steps to add to the dashboard the
Event listchart and other charts as needed.
-
Use your mouse to resize and move the charts around.
-
Click Save.
-
In the window that opens, name 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:
- Delete the trigger invoking the function in Cloud Functions.
- Delete the function in Cloud Functions.
- Delete the Managed Service for YDB database.
- Delete the stream in Data Streams.
- Delete the Yandex Cloud Postbox address and configuration.
- Optionally, delete the service accounts.
- Optionally, delete the resource record and public DNS zone.