Delivering data from Yandex Forms to Yandex DataLens using Yandex Cloud Functions and Yandex Query via the management console
To set up integration of Forms and DataLens via the management console, follow these steps:
- Set up your infrastructure.
- Create a Yandex Cloud Functions function.
- Create a form in Forms.
- Configure a connection and binding to data in Yandex Query.
- Set up fetching data in DataLens.
- Test the new resource integration.
If you no longer need the resources you created, delete them.
Get your cloud ready
Sign up in Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or register a new account. - 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 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.
Required paid resources
The infrastructure support cost includes:
- Fee for using the Yandex Object Storage bucket (see Object Storage pricing).
- Fee for storing and requesting Yandex Lockbox secrets (see Yandex Lockbox pricing).
- Fee for function calls and computing resources allocated for executing the functions (see Cloud Functions pricing).
- Fee for the amount of data read from sources when executing Query queries (see Query pricing).
- Fee for using DataLens under the service plan (see DataLens pricing).
Set up your infrastructure
Create a service account
- In the management console
, select the appropriate folder. - From the list of services, select Identity and Access Management.
- Click Create service account.
- Enter a name for the service account:
forms-integration-sa
. - Click
Add role and select the following roles.- lockbox.payloadViewer: For reading Yandex Lockbox secrets.
- functions.functionInvoker: For invoking the Cloud Functions function.
- storage.editor: For reading data from, and writing data to, an Object Storage bucket.
- yq.viewer and yq.invoker: For integrating DataLens and Query.
- Click Create.
Create an Object Storage bucket
The Object Storage bucket will store data from Forms.
- In the management console
, select the appropriate folder. - From the list of services, select Object Storage.
- Click Create bucket.
- Enter the bucket ** Name**.
- Click Create bucket.
Assign access permissions for the bucket to the service account
-
In the management console
, select the appropriate folder. -
From the list of services, select Object Storage.
-
Open the bucket you created earlier.
-
Go to Objects.
-
Click
and select Configure ACL. -
In the ACL editing window that opens:
- Start typing the service account name,
forms-integration
, and select it from the drop-down list. - Select the READ and WRITE access permissions.
- Click Add.
- Click Save.
- Start typing the service account name,
Create a static key for the service account
- In the management console
, select the appropriate folder. - From the list of services, select Identity and Access Management.
- Select the
forms-integration
service account. - Go to Overview.
- Click Create new key and select Create static access key.
- Click Create.
- In the window that opens, New key, you will see Key ID and Your secret key. Save them, as you will need them later.
Create a Yandex Lockbox secret for the static key
- In the management console
, select the appropriate folder. - From the list of services, select Lockbox.
- Click Create secret.
- Enter the secret's Name:
static-key
. - Under Secret data:
- Select Secret type: Custom.
- Create the following key-value pairs:
-
Static key ID:
- Key:
static-key-id
. - Value: Select Text and enter the ID of the static key you created earlier.
- Key:
-
Static key value:
- Key:
static-key-value
. - Value: Select Text and enter the value of the static key you created earlier.
- Key:
-
Create and configure a Cloud Functions function
-
In the management console
, select the appropriate folder. -
From the list of services, select Cloud Functions.
-
Create a function:
- Click Create function in the top-right corner.
- Enter the function Name:
forms-function
. - Click Create.
-
Create a function version:
-
In the Editor window that opens, select the runtime environment: Python.
-
Disable the Add files with code examples option.
-
Click Continue.
-
Select the function creation Method: Code editor.
-
In the function editor window, click Create file.
-
In the window that opens, enter the file Name:
forms-integration.py
. Click Create. -
Paste the function code to the
forms-integration.py
file:import boto3 import os import json from botocore.config import Config def get_answers(raw_answers: json) -> json: result = {} for name, value in raw_answers.get('answer').get('data').items(): if name.startswith('answer_choices_'): choices = [] for c in value.get('value'): choices.append(c.get('text')) result[name] = ','.join(choices) else: result[name] = value.get('value') return result def handler(event, context): headers = event.get('headers') form_id = headers.get('X-Form-Id') answer_id = headers.get('X-Form-Answer-Id') body_json = json.loads(event.get('body')) answers_json = get_answers(body_json) aws_access_key_id = os.environ['KEY'] aws_secret_access_key = os.environ['SECRET_KEY'] bucket_name = os.environ['BUCKET'] s3 = boto3.client(service_name='s3', aws_access_key_id=aws_access_key_id,aws_secret_access_key=aws_secret_access_key, endpoint_url='https://storage.yandexcloud.net', region_name='ru-central1', config=Config(request_checksum_calculation="when_required")) s3.put_object(Bucket=bucket_name, Key=f"{form_id}/{answer_id}.json", Body=json.dumps(answers_json)) return { 'statusCode': 200, 'body': "OK", 'headers': { 'Content-Type': 'text/plain', } }
-
In the function editor window, click Create file.
-
In the window that opens, enter the file Name:
requirements.txt
. Click Create. -
In
requirements.txt
, specify theboto3
library for accessing Object Storage from your function:boto3
-
In the Entry point field, specify
forms-integration.handler
. -
In the Service account field, select the previously created account named
forms-integration-sa
. -
Under Environment variables, create a variable containing the bucket name where the function results will be stored:
- Key:
BUCKET
. - Value: Name of the bucket you created earlier.
- Key:
-
Under Lockbox secrets:
-
Create a
KEY
variable for the static key ID. Set the parameters as follows:- Environment variable:
KEY
. - Secret ID: Select the
static-key-id
secret. - Version ID: Select the version marked
latest
. - Secret key: Select
static-key-id
.
- Environment variable:
-
Create a
SECRET_KEY
variable for the static key ID. Set the parameters as follows:- Environment variable:
SECRET_KEY
. - Secret ID: Select the
static-key-value
secret. - Version ID: Select the version marked
latest
. - Secret key: Select
static-key-value
.
- Environment variable:
-
-
Once your function is created, on the Overview tab, enable Public function:
-
Create and set up a Forms form
Create an API key
You need an API key to set up integration with Forms.
- In the management console
, select the appropriate folder. - From the list of services, select Identity and Access Management.
- Select the
forms-integration
service account. - Go to Overview.
- Click Create new key and select Create API key.
- Click Create.
- In the window that opens, New key, you will see Key ID and Your secret key. Save them, as you will need them later.
Create a form
-
Go to Forms
. -
Click Create form.
-
Add at least two fields to the form and set up any questions in them.
-
Click Publish.
-
Save the link to the published form: you will need it later.
Set up integration with a Cloud Functions function
-
Go to Forms
. -
Open the form you created earlier.
-
Go to the Settings tab.
-
Open the Additional section.
-
Under Cloud function key, enter the data of the previously created API key and click Save.
-
Open the Integrations tab.
-
Click Cloud Functions to add a condition for the integration.
-
In the Function code field, enter the Cloud Functions function ID. You can copy the ID in the Overview section of the Cloud Functions function in the management console.
-
In the Show outcome notification field, select Show.
-
Click Save.
Test the integration you created
-
Follow the public link to the previously created form.
-
Complete the form and click Submit.
-
Go to Forms
. -
Open the form you created earlier.
-
Open the Integrations tab.
-
Go to the Completed integrations page.
-
Open the completed action record and make sure that the Response section contains the
200 — OK
response. -
Navigate to the management console
and open the bucket created earlier. Make sure the bucket now contains a JSON file with the data from the form you completed.The name of the directory the file will reside in matches the form's internal ID. Save this ID for the next steps.
All the future form completion outcomes will be saved in this directory.
-
Complete the form several times more, and make sure you fill one of the fields in the same way in at least two forms. This will improve data visualization when testing the integration.
Configure a connection and binding to data in Query
- Navigate to the management console
and select Query. - In the Connections tab, click
Create new. - Create a connection with the following parameters:
- Name:
forms-connection
. - Type:
Object Storage
. - Bucket: Name of the bucket you created earlier.
- Service account:
forms-integration-sa
.
- Name:
- In the window that opens, set the data binding parameters:
- Type:
Object Storage
. - Connection:
forms-connection
. - Path:
/<form_ID>/
.
You can copy the ID:- Under Objects in the Object Storage bucket. The name of the folder containing the file with results of the completed form matches the form ID.
- In the address bar of the Forms interface
, on the form viewing or editing page.
- Format:
json_each-row
. - Columns: Create columns for the fields you specified in the form.
If you want Query to define columns automatically, click Detect columns.
- Type:
Set up fetching data in DataLens
- Go to DataLens
. - In the left-hand panel, select
Connections and click Create connection. - Select Query. In the window that opens, set the below parameters as follows:
- Cloud and folder: Select the cloud and folder where you created the rest of the resources.
- Service account:
forms-integration-sa
- Click Save changes. In the window that opens, specify
forms-datalens-connection
as the connection name, then click Create. - Go back to the DataLens
main page and click Create dataset. - In the connection panel, click
Add and select theforms-datalens-connection
connection. - Under Tables, select the table and drag it to your DataLens workspace.
After loading, the data from the table will appear in the Preview panel. - Click Save and enter
forms-integration-dataset
as the dataset name, then click Create.
Test the new resource integration
To check how the created resources work together, open the forms-integration-dataset
dataset and click Create chart. In the window that opens, create a chart using the data from the form. For better visualization, create a chart based on a field with repeating values.
Delete the resources you created
Some resources are not free of charge. To avoid paying for them, delete the resources you no longer need:
- DataLens chart.
- DataLens dataset.
- DataLens connection.
- Query binding.
- Query connection.
- Cloud Functions function.
- Yandex Lockbox secret.
- Object Storage buckets.