# Migrating data from Yandex Managed Service for OpenSearch to YDB using Yandex Data Transfer

# Migrating data from Yandex Managed Service for OpenSearch to Yandex Managed Service for YDB using Yandex Data Transfer


With Data Transfer, you can transfer data from a Managed Service for OpenSearch cluster to a Managed Service for YDB database.

To transfer data:

1. [Set up your infrastructure](#prepare-infrastructure).
1. [Prepare your test data](#prepare-data).
1. [Prepare and activate your transfer](#prepare-transfer).
1. [Test the transfer](#verify-transfer).

If you no longer need the resources you created, [delete them](#clear-out).


## Getting started {#before-you-begin}

Sign up for Yandex Cloud and create a [billing account](../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../billing/quickstart/index.md) and [link](../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../resource-manager/concepts/resources-hierarchy.md).

### Required paid resources {#paid-resources}

* Managed Service for OpenSearch cluster: use of computing resources, storage and backup size (see [Managed Service for OpenSearch pricing](../../managed-opensearch/pricing.md)).
* Public IP addresses if public access is enabled for cluster hosts (see [Yandex Virtual Private Cloud pricing](../../vpc/pricing.md)).
* Managed Service for YDB database (see [Managed Service for YDB pricing](../pricing/index.md)). Its cost depends on the deployment mode:

    * In serverless mode, you pay for data operations as well as the amount of stored data and backups.
    * In dedicated instance mode, you pay for the use of computing resources allocated to the database, storage size, and backups.


## Set up your infrastructure {#prepare-infrastructure}

{% list tabs group=instructions %}

* Manually {#manual}

    1. [Create a Managed Service for OpenSearch cluster](../../managed-opensearch/operations/cluster-create.md) of any suitable configuration with publicly accessible hosts.

        {% note info %}
        
        Public access to cluster hosts is required if you plan to connect to the cluster via the internet. This connection option is simpler and is recommended for the purposes of this guide. You can connect to non-public hosts as well but only from Yandex Cloud virtual machines located in the same cloud network as the cluster.
        
        {% endnote %}

    1. If there are security groups in your cluster, make sure they are configured correctly and allow connections to the [Managed Service for OpenSearch cluster](../../managed-opensearch/operations/connect/index.md#configuring-security-groups).

    1. [Get an SSL certificate](../../managed-opensearch/operations/connect/index.md#ssl-certificate) to connect to the Managed Service for OpenSearch cluster.

    1. [Create a Managed Service for YDB database](../operations/manage-databases.md) named `ydb1` of your preferred configuration.

    
    1. [Create a service account](../../iam/operations/sa/create.md#create-sa) named `ydb-account` with the `ydb.editor` role. The transfer will use it to access the database.


* Using Terraform {#tf}

    1. If you do not have Terraform yet, [install it](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
    1. [Get the authentication credentials](../../tutorials/infrastructure-management/terraform-quickstart.md#get-credentials). You can add them to environment variables or specify them later in the provider configuration file.
    1. [Configure and initialize a provider](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). There is no need to create a provider configuration file manually, you can [download it](https://github.com/yandex-cloud-examples/yc-terraform-provider-settings/blob/main/provider.tf).
    1. Place the configuration file in a separate working directory and [specify the parameter values](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). If you did not add the authentication credentials to environment variables, specify them in the configuration file.

    1. Download the [opensearch-to-ydb.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-opensearch-to-ydb/blob/main/opensearch-to-ydb.tf) configuration file to the same working directory.

        This file describes:

        * [Network](../../vpc/concepts/network.md#network).
        * [Subnet](../../vpc/concepts/network.md#subnet).
        * [Security group](../../vpc/concepts/security-groups.md) and rules for internet access to the Managed Service for OpenSearch cluster.
        * Managed Service for YDB database.
        * Managed Service for OpenSearch target cluster.
        * Target endpoint.
        * Transfer.

    1. In the `opensearch-to-ydb.tf` file, specify the following settings:

        * `mos_version`: OpenSearch version.
        * `mos_password`: OpenSearch database owner password.
        * `profile_name`: Name of your CLI profile. 

    1. Validate your Terraform configuration files using this command:

        ```bash
        terraform validate
        ```

        Terraform will display any configuration errors detected in your files.

    1. Create the required infrastructure:

        1. Run this command to view the planned changes:
        
           ```bash
           terraform plan
           ```
        
           If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
        
        1. If everything looks correct, apply the changes:
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

        All the required resources will be created in the specified folder. You can check resource availability and their settings in the [management console](https://console.yandex.cloud).

{% endlist %}

## Prepare your test data {#prepare-data}

1. [Connect to the Managed Service for OpenSearch source cluster](../../managed-opensearch/operations/connect/index.md).

1. Create a test index named `people` and define its schema:

    ```bash
    curl --user admin:<password> \
         --cacert ~/.opensearch/root.crt \
         --header 'Content-Type: application/json' \
         --request PUT 'https://<address_of_OpenSearch_host_with_DATA_role>:9200/people' && \
    curl --user admin:<password> \
         --cacert ~/.opensearch/root.crt \
         --header 'Content-Type: application/json' \
         --request PUT 'https://<address_of_OpenSearch_host_with_DATA_role>:9200/people/_mapping?pretty' \
         --data'
         {
               "properties": {
                  "name": {"type": "text"},
                  "age": {"type": "integer"}
               }
         }
         '
    ```

1. Populate the test index with data:

    ```bash
    curl --user admin:<password> \
         --cacert ~/.opensearch/root.crt \
         --header 'Content-Type: application/json' \
         --request POST 'https://<address_of_OpenSearch_host_with_DATA_role>:9200/people/_doc/?pretty' \
         --data'
         {
               "name" : "Alice",
               "age" : "30"
         }
         ' && \
    curl --user admin:<password> \
         --cacert ~/.opensearch/root.crt \
         --header 'Content-Type: application/json' \
         --request POST 'https://<address_of_OpenSearch_host_with_DATA_role>:9200/people/_doc/?pretty' \
         --data'
         {
               "name" : "Robert",
               "age" : "32"
         }
         '
    ```

1. Optionally, check the data in the test index:

    ```bash
    curl --user admin:<password> \
         --cacert ~/.opensearch/root.crt \
         --header 'Content-Type: application/json' \
         --request GET 'https://<address_of_OpenSearch_host_with_DATA_role>:9200/people/_search?pretty'
    ```

## Prepare and activate your transfer {#prepare-transfer}

1. [Create a source endpoint](../../data-transfer/operations/endpoint/source/opensearch.md#endpoint-settings) of the `OpenSearch` type with the following settings:

    * **Connection type**: `Managed Service for OpenSearch cluster`.
    * **Managed Service for OpenSearch cluster**: Select your Managed Service for OpenSearch cluster from the list.
    * **User**: `admin`.
    * **Password**: `<user_password>`.

1. Create a target endpoint and a transfer:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Create a target endpoint](../../data-transfer/operations/endpoint/target/yandex-database.md#endpoint-settings) of the `YDB` type with the following settings:

            * **Database**: Select your `ydb1` database from the list.

            
            * **Service account ID**: Select the `ydb-account` service account from the list.


        1. [Create a transfer](../../data-transfer/operations/transfer.md#create) of the **_Snapshot_** type that will use the endpoints you created.

        1. [Activate the transfer](../../data-transfer/operations/transfer.md#activate) and wait for its status to change to **Completed**.

    - Using Terraform {#tf}

        1. In the `opensearch-to-ydb.tf` file, specify the following settings:

            * `source_endpoint_id`: Source endpoint ID.
            * `transfer_enabled`: Set to `1` to create a target endpoint and transfer.

        1. Make sure the Terraform configuration files are correct using this command:

            ```bash
            terraform validate
            ```

            Terraform will display any configuration errors detected in your files.

        1. Create the required infrastructure:

            1. Run this command to view the planned changes:
            
               ```bash
               terraform plan
               ```
            
               If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
            
            1. If everything looks correct, apply the changes:
               1. Run this command:
            
                  ```bash
                  terraform apply
                  ```
            
               1. Confirm updating the resources.
               1. Wait for the operation to complete.

        1. The transfer will be activated automatically. Wait for its status to change to **Completed**.

    {% endlist %}

## Test the transfer {#verify-transfer}

1. [Connect to the Managed Service for YDB database](../operations/connection.md).

1. Run this query:

    ```sql
    SELECT * FROM people;
    ```

    {% cut "Response example" %}

    ```text
    # |          _id           | age | name
    --+------------------------+-----+---------
    0 | "5wn5BJEBRVOYnL8d13sP" | 30  | "Alice"
    1 | "6An5BJEBRVOYnL8d13uy" | 32  | "Robert"
    ```

    {% endcut %}

## Delete the resources you created {#clear-out}

{% note info %}

Before deleting any resources, [deactivate the transfer](../../data-transfer/operations/transfer.md#deactivate).

{% endnote %}

To reduce the consumption of resources, delete those you do not need:

1. Delete the resources depending on how you created them:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Delete the transfer](../../data-transfer/operations/transfer.md#delete).
        1. [Delete the target endpoint](../../data-transfer/operations/endpoint/index.md#delete).
        1. [Delete the Managed Service for OpenSearch cluster](../../managed-opensearch/operations/cluster-delete.md).
        1. [Delete the Managed Service for YDB database](../operations/manage-databases.md#delete-db).

        
        1. [Delete the service account](../../iam/operations/sa/delete.md).


    - Using Terraform {#tf}

        1. In the terminal window, go to the directory containing the infrastructure plan.
        
            {% note warning %}
        
            Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.
        
            {% endnote %}
        
        1. Delete resources:
        
            1. Run this command:
        
                ```bash
                terraform destroy
                ```
        
            1. Confirm deleting the resources and wait for the operation to complete.
        
            All the resources described in the Terraform manifests will be deleted.

    {% endlist %}

1. [Delete the source endpoint](../../data-transfer/operations/endpoint/index.md#delete).