# Migrating a YDB database to a different availability zone

Migration to another availability only makes sense for a YDB [dedicated database](../concepts/serverless-and-dedicated.md). Serverless databases are not migrated between availability zones.

A dedicated database is placed in multiple [availability zones](../../overview/concepts/geo-scope.md) of the central Russia `ru-central1` region. You can replace one zone with another. To do this:

1. [Create a subnet](../../vpc/operations/subnet-create.md) in the target availability zone for your database.
1. Move the YDB database to a new set of subnets:

   {% list tabs group=instructions %}

   - Yandex Cloud CLI {#cli}

      If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

      The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

      Run this command:

      ```bash
      yc ydb database update <DB_name> \
         --subnet-id <subnet_ID_in_ru-central1-a_availability_zone> \
         --subnet-id <subnet_ID_in_ru-central1-b_availability_zone> \
         --subnet-id <subnet_ID_in_ru-central1-d_availability_zone>
      ```

   - Terraform {#tf}

      If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
      
      
      To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../terraform/authentication.md) using the appropriate method.

      1. Open the Terraform configuration file describing your infrastructure and edit the fragment with your database:

         ```hcl
         resource "yandex_ydb_database_dedicated" "<DB_name>" {
            ...
            subnet_ids = [<subnet_IDs>]
            ...
         }
         ```

         Under `subnet_ids`, list the subnet IDs in the `ru-central1-a`, `ru-central1-b`, and `ru-central1-d` availability zones, separated by commas.

      1. Apply the changes:

         1. In the terminal, navigate to the configuration file directory.
         1. Make sure the configuration is correct using this command:
         
            ```bash
            terraform validate
            ```
         
            If the configuration is valid, you will get this message:
         
            ```bash
            Success! The configuration is valid.
            ```
         
         1. Run this command:
         
            ```bash
            terraform plan
            ```
         
            You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
         1. Apply the configuration changes:
         
            ```bash
            terraform apply
            ```
         
         1. Type `yes` and press **Enter** to confirm the changes.

   {% endlist %}

## Migrating in Yandex Data Transfer {#data-transfer}

If you are using a [transfer](../../data-transfer/concepts/transfer-lifecycle.md#transfer-types) of the **Replication** or **Snapshot and increment** type with your database as an [endpoint](../../data-transfer/concepts/index.md#endpoint), restart the transfer using one of the two methods after changing subnets in the database:

To restart a transfer, choose one of the two methods:

* [Deactivate](../../data-transfer/operations/transfer.md#deactivate) the transfer and wait for its status to change to **Stopped**. Next, [reactivate](../../data-transfer/operations/transfer.md#activate) the transfer and wait for its status to change to **Replicating**.
* Update any [setting for the transfer](../../data-transfer/operations/transfer.md#update) or [endpoint](../../data-transfer/operations/endpoint/index.md#update).

For more information, see [Migrating a Data Transfer transfer and endpoints to a different availability zone](../../data-transfer/operations/endpoint/migration-to-an-availability-zone.md).