[Yandex Cloud documentation](../../index.md) > [Yandex StoreDoc](../index.md) > [Step-by-step guides](index.md) > Clusters > Creating a cluster

# Creating a Yandex StoreDoc cluster


A Yandex StoreDoc cluster consists of one or multiple database hosts, with the option to configure [replication](../concepts/replication.md) between them. Any cluster with more than one host has replication enabled by default. In this setup, the primary host accepts write requests and asynchronously replicates changes to secondary hosts.


{% note info %}

* The number of hosts you can create with a Yandex StoreDoc cluster depends on the selected [disk type](../concepts/storage.md#storage-type-selection) and [host class](../concepts/instance-types.md#available-flavors).
* The available disk types [depend](../concepts/storage.md) on the selected [host class](../concepts/instance-types.md).

{% endnote %}


Cluster DB connections are managed by Connection Manager. Creating a cluster automatically creates:

* [Connection Manager connection](../../metadata-hub/concepts/connection-manager.md) with information about the database connection.

* [Yandex Lockbox secret](../../metadata-hub/concepts/secret.md) that stores the DB owner's user password. Storing passwords in Yandex Lockbox ensures their security.

The connection and secret will be created for each new database user. To view all connections, select the **Connections** tab on the cluster page.

You need the `connection-manager.viewer` role to view connection info. You can [use Connection Manager](../../metadata-hub/operations/connection-access.md) to configure access to connections.

You can use Connection Manager and secrets you create there free of charge.





## Creating a cluster {#create-cluster}


To create a Yandex StoreDoc cluster, you need the [vpc.user](../../vpc/security/index.md#vpc-user) role along with the [managed-mongodb.editor role or higher](../security/index.md#roles-list). For information on how to assign roles, see [this Identity and Access Management guide](../../iam/operations/roles/grant.md).


{% list tabs group=instructions %}

- Management console {#console}

  To create a Yandex StoreDoc cluster:

  1. In the [management console](https://console.yandex.cloud), select the folder where you want to create your database cluster.

  1. Navigate to **Yandex StoreDoc**.

  1. Click **Create cluster**.

  1. Under **Basic parameters**:

      * Enter a name in the **Cluster name** field. The cluster name must be unique within the cloud.
      * Optionally, specify your cluster **Description**.
      * Select your cluster environment. Note that you cannot change the environment once the cluster is created:

          * `PRODUCTION`: For stable versions of your applications.
          * `PRESTABLE`: For testing purposes. The prestable environment is similar to the production environment and likewise covered by an SLA, but it is the first to get new features, improvements, and bug fixes. In the prestable environment, you can test new versions for compatibility with your application.

      * Specify the DBMS version.
      * Select the sharding type:
          * **Disabled**: Cluster will consist only of `MONGOD` hosts.
          * **Standard**: Cluster will consist of `MONGOD` and `MONGOINFRA` hosts.
          * **Advanced**: Cluster will consist of `MONGOD`, `MONGOS`, and `MONGOCFG` hosts.

  
  1. Under **Network settings**, select:

      * [Cloud network](../../vpc/concepts/network.md#network) for cluster deployment.
      * Security groups for cluster network traffic. You may need to [configure security groups](connect/index.md#configuring-security-groups) to connect to the cluster.


  1. Specify the computing resource configuration:

      * For a non-sharded cluster, under **Resources**.
      * For a cluster with standard sharding, under **Mongod Resources** and **Mongoinfra Resources**.
      * For a cluster with advanced sharding, under **Mongod Resources**, **Mongos Resources**, and **Mongocfg Resources**.

     To specify your computing resource configuration:

     1. Select the platform, VM type, and host class. The latter determines the technical specifications of the VMs the database hosts will be deployed on. All available options are listed under [Host classes](../concepts/instance-types.md). When you change the host class for a cluster, the specifications of all existing instances also change.

        {% note info %}

        The **memory-optimized** configuration type is unavailable for `MONGOS` hosts.

        {% endnote %}

     
     1. Under **Storage**:

         * Select the [disk type](../concepts/storage.md).

             The type you select determines the increments for changing the disk size:
             
             * Network HDDs and SSDs: In increments of 1 GB.
             * Local SSDs:
                 * For **Intel Broadwell** and **Intel Cascade Lake**: In 100 GB increments.
                 * For **Intel Ice Lake** and **AMD Zen 4**: In 368 GB increments.
             * Non-replicated SSDs and ultra high-speed network SSDs with three replicas: In increments of 93 GB.

         * Select the storage capacity for your data and backups. For more information, see [Backups](../concepts/backup.md).

         * Optionally, select **Encrypted disk** to encrypt the disk with a [custom KMS key](../../kms/concepts/key.md).

             * To [create](../../kms/operations/key.md#create) a new key, click **Create**.

             * To use a previously created key, select it in the **KMS key** field.

             To learn more about disk encryption, see [Storage](../concepts/storage.md#disk-encryption).


     1. Under **Hosts**, add the database hosts to be created alongside the cluster:

         
         * Click **Add host**.
         * Select the [availability zone](../../overview/concepts/geo-scope.md).
         * Select a [subnet](../../vpc/concepts/network.md#subnet) in the chosen availability zone. If your network has no subnets, create one:
         * To allow external access to the host from outside Yandex Cloud, enable **Public access**.

     
         To ensure fault tolerance while using `local-ssd` and `network-ssd-nonreplicated` disks, you need a minimum of 3 hosts. For more information, see [Storage](../concepts/storage.md).

         
         By default, hosts are created across multiple availability zones. For more information, see [host management](hosts.md).


  1. Under **Database**, specify the database details:

      * Database name.

        A database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. Such names as `config`, `local`, `admin`, and `mdb_internal` are reserved for Yandex StoreDoc. You cannot create DBs with these names.

      * Username.

      
      * User password:

        * **Enter manually**: Select this option to set your own password. It must be from 8 to 128 characters long.
        * **Generate**: Select this option to generate a password using Connection Manager.

            To view the generated password after creating a cluster, select the **Users** tab and click **View password** for the relevant user. This will open the page of the Yandex Lockbox secret containing the password. To view passwords, you need the `lockbox.payloadViewer` role.


  1. If needed, configure the advanced cluster settings:

      - **Backup start time (UTC)**: Time interval during which the cluster backup starts. Time is specified in 24-hour UTC format. The default time is `22:00 - 23:00` UTC.
      
      - **Retention period for automatic backups, days**{#setting-backup-saving}
        
        Retention period for automatic backups. Backups are automatically deleted once their retention period expires. The default is 7 days. This feature is in [Preview](../../overview/concepts/launch-stages.md). For more information, see [Backups](../concepts/backup.md).
      
      
        Changing the retention period affects both new and existing automatic backups. For example, if the initial retention period was 7 days, and the remaining lifetime of a separate automatic backup is 1 day, increasing the retention period to 9 days will change the remaining lifetime of this backup to 3 days.
      
        For an existing cluster, automatic backups are stored for a specified number of days whereas manually created ones are stored indefinitely. After a cluster is deleted, all backups persist for 7 days.
      
      - **Maintenance**: [Maintenance](../concepts/maintenance.md) window settings:
      
          * To enable maintenance at any time, select **At any time** (default).
          * To specify the preferred maintenance start time, select **By schedule** and specify the day of the week and the UTC time interval. For example, you can choose a time when the cluster is least loaded.
          
          Both active and stopped clusters are subject to maintenance. Maintenance operations include DBMS updates, patches, etc.
      
      
      
      - **WebSQL access**: This option enables you to [run SQL queries](websql.md) against cluster databases from the Yandex Cloud management console using Yandex WebSQL.
      
      
      - **Statistics sampling**: Enable this option to use the built-in [performance diagnostics](performance-diagnostics.md) tool in the cluster. This feature is at the [Preview](../../overview/concepts/launch-stages.md) stage.
      
      - **Deletion protection**: Cluster protection from accidental deletion.
      
        Even with cluster deletion protection enabled, one can still delete a user or database or connect manually and delete the database contents.

  1. If necessary, configure [database settings](../concepts/settings-list.md#dbms-cluster-settings).

      {% note info %}
      
      Some Yandex StoreDoc settings [depend on the selected host class](../concepts/settings-list.md#settings-instance-dependent).
      
      {% endnote %}

  1. Click **Create cluster**.

- 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.

  To create a Yandex StoreDoc cluster:

  
  1. Check whether the folder has any subnets for cluster hosts:

     ```bash
     yc vpc subnet list
     ```

     If there are no subnets in the folder, [create the right ones](../../vpc/operations/subnet-create.md) in VPC.


  1. View the description of the CLI command for creating a cluster:

      ```bash
      yc managed-mongodb cluster create --help
      ```

  1. Specify the cluster settings in the `cluster create` command. Note that our example does not contain all available settings:

      {% cut "For a non-sharded cluster" %}

      
      ```bash
      yc managed-mongodb cluster create \
        --name <cluster_name> \
        --environment=<environment> \
        --network-name <network_name> \\
        --security-group-ids <security_group_IDs> \ 
        --host zone-id=<availability_zone>,`
              `subnet-id=<subnet_ID>,`
              `assign-public-ip=<allow_public_access_to_host>,`
              `hidden=<hide_host>,`
              `secondary-delay-secs=<replica_lag_in_seconds>,`
              `priority=<host_priority> \
        --mongod-resource-preset <host_class> \
        --user name=<username>,password=<user_password> \
        --database name=<DB_name> \
        --mongod-disk-type <network-hdd|network-ssd|network-ssd-nonreplicated|local-ssd> \
        --mongod-disk-size <storage_size_in_GB> \
        --disk-encryption-key-id <KMS_key_ID> \
        --performance-diagnostics=<enable_diagnostics> \
        --deletion-protection
      ```


      {% endcut %}

      {% cut "For a cluster with standard sharding" %}

      
      ```bash
      yc managed-mongodb cluster create \
        --name <cluster_name> \
        --environment=<environment> \
        --mongodb-version <Yandex_StoreDoc_version> \          
        --network-name <network_name> \
        --security-group-ids <security_group_IDs> \      
        --user name=<username>,password=<user_password> \
        --database name=<DB_name> \
        --mongod-resource-preset <host_class> \
        --mongod-disk-type <network-hdd|network-ssd|network-ssd-nonreplicated|local-ssd> \
        --mongod-disk-size <storage_size_in_GB> \
        --host type=mongod,`
              `zone-id=<availability_zone>,`
              `subnet-id=<subnet_ID>,`
              `hidden=<hide_host>,`
              `secondary-delay-secs=<replica_lag_in_seconds>,`
              `priority=<host_priority> \
        --mongoinfra-resource-preset <host_class> \
        --mongoinfra-disk-type <network-hdd|network-ssd> \
        --mongoinfra-disk-size <storage_size_in_GB> \
        --host type=mongoinfra,`
              `zone-id=<availability_zone>,`
              `subnet-id=<subnet_ID>,`
              `assign-public-ip=<allow_public_access_to_host> \
        --disk-encryption-key-id <KMS_key_ID> \
        --performance-diagnostics=<enable_diagnostics> \
        --deletion-protection
      ```


      {% endcut %}

      {% cut "For a cluster with advanced sharding" %}

      
      ```bash
      yc managed-mongodb cluster create \
        --name <cluster_name> \
        --environment=<environment> \
        --mongodb-version <Yandex_StoreDoc_version> \          
        --network-name <network_name> \
        --security-group-ids <security_group_IDs> \      
        --user name=<username>,password=<user_password> \
        --database name=<DB_name> \
        --mongod-resource-preset <host_class> \
        --mongod-disk-type <network-hdd|network-ssd|network-ssd-nonreplicated|local-ssd> \
        --mongod-disk-size <storage_size_in_GB> \
        --host type=mongod,`
              `zone-id=<availability_zone>,`
              `subnet-id=<subnet_ID>,`
              `hidden=<hide_host>,`
              `secondary-delay-secs=<replica_lag_in_seconds>,`
              `priority=<host_priority> \
        --mongos-resource-preset <host_class> \
        --mongos-disk-type <network-hdd|network-ssd> \
        --mongos-disk-size <storage_size_in_GB> \
        --host type=mongos,`
              `zone-id=<availability_zone>,`
              `subnet-id=<subnet_ID>,`
              `assign-public-ip=<allow_public_access_to_host> \
        --mongocfg-resource-preset <host_class> \
        --mongocfg-disk-type <network-hdd|network-ssd> \
        --mongocfg-disk-size <storage_size_in_GB> \
        --host type=mongocfg,`
              `zone-id=<availability_zone>,`
              `subnet-id=<subnet_ID> \
        --disk-encryption-key-id <KMS_key_ID> \
        --performance-diagnostics=<enable_diagnostics> \
        --deletion-protection
      ```


      {% endcut %}

      Where:

      * `--environment`: Environment, `prestable` or `production`.

      
      * `--security-group-ids`: List of security group IDs.
      * `--database name`: Database name.
        
        {% note info %}

        A database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. Such names as `config`, `local`, `admin`, and `mdb_internal` are reserved for Yandex StoreDoc. You cannot create DBs with these names.

        {% endnote %}

      * `--host`: Host settings:
        * `type`: Host type, i.e., `mongod`, `mongoinfra`, `mongos`, or `mongocfg`. The default host type is `mongod`.
        * `zone-id`: [Availability zone](../../overview/concepts/geo-scope.md).
        * `subnet-id`: [Subnet ID](../../vpc/concepts/network.md#subnet). To be specified if the selected availability zone has more than one subnet.
        * `assign-public-ip`: Internet access to the host via a public IP address, `true` or `false`. In a sharded cluster, it is used only for `MONGOS` and `MONGOINFRA` hosts.
        * `hidden`: Determines whether the host is hidden, `true` or `false`. If the host is hidden, only direct connections will be able to read from it (for example, to make backups from it without adding load to the cluster).
        * `secondary-delay-secs`: Replica's lag behind the master in seconds. This setting can be useful for data recovery after operational errors.
        * `priority`: [Host priority for master promotion](../concepts/replication.md#master-failover).
          
          {% note info %}

          The `hidden`, `secondary-delay-secs`, and `priority` parameters are used for `MONGOD` hosts only.

          {% endnote %}

      * `--mongod-resource-preset`: `MONGOD` host class.
      * `--mongoinfra-resource-preset`, `--mongos-resource-preset`, `--mongocfg-resource-preset`: `MONGOINFRA`, `MONGOS`, and `MONGOCFG` host classes, respectively (for sharded clusters only).
      * `--mongod-disk-type`: Disk type of `MONGOD` hosts.
      * `--mongoinfra-disk-type`, `--mongos-disk-type`, `--mongocfg-disk-type`: Disk types of `MONGOINFRA`, `MONGOS`, and `MONGOCFG` hosts, respectively (for sharded clusters only).
      * `--disk-encryption-key-id`: Disk encryption using a [custom KMS key](../../kms/concepts/key.md).

         To learn more about disk encryption, see [Storage](../concepts/storage.md#disk-encryption).


      * `--performance-diagnostics`: Enables cluster performance diagnostics, `true` or `false`.
      
      * `--deletion-protection`: Cluster protection from accidental deletion, `true` or `false`.

        Even with cluster deletion protection enabled, one can still delete a user or database or connect manually and delete the database contents.

      {% note info %}

      The default [maintenance](../concepts/maintenance.md) mode for new clusters is `anytime`. You can set a specific maintenance period when [updating the cluster settings](update.md#change-additional-settings).

      {% endnote %}


- Terraform {#tf}

  With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
  
  Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the guides on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../terraform/index.md).

  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.

  To create a Yandex StoreDoc cluster:

  1. In the configuration file, describe the resources you want to create:

     * Database cluster: Description of the cluster and its hosts.

     * Network: Description of the [cloud network](../../vpc/concepts/network.md#network) where a cluster will be located. If you already have a suitable network, you don't have to describe it again.

     * Subnets: Description of the [subnets](../../vpc/concepts/network.md#subnet) to connect the cluster hosts to. If you already have suitable subnets, you don't have to describe them again.

     Here is an example of the configuration file structure:

     {% cut "For a non-sharded cluster" %}

     ```hcl
     resource "yandex_mdb_mongodb_cluster" "<cluster_name>" {
       name                = "<cluster_name>"
       environment         = "<environment>"
       network_id          = yandex_vpc_network.<network_name>.id
       security_group_ids  = [ "<list_of_security_group_IDs>" ]
       deletion_protection = <protect_cluster_from_deletion>

       cluster_config {
         version = "<Yandex_StoreDoc_version>"
       }

       resources_mongod {
         resource_preset_id = "<host_class>"
         disk_type_id       = "<disk_type>"
         disk_size          = <storage_size_in_GB>
       }

       host {
         type             = "mongod"
         zone_id          = "<availability_zone>"
         subnet_id        = yandex_vpc_subnet.<subnet_name>.id
         assign_public_ip = <allow_public_access_to_host>
         host_parameters {
           hidden               = <hide_host>
           secondary_delay_secs = <replica_lag_in_seconds>
           priority             = <host_priority>
         }
       }

       resources_mongoinfra {
         resource_preset_id = "<host_class>"
         disk_type_id       = "<disk_type>"
         disk_size          = <storage_size_in_GB>
       }

       host {
         type             = "mongoinfra"
         zone_id          = "<availability_zone>"
         subnet_id        = yandex_vpc_subnet.<subnet_name>.id
         assign_public_ip = <allow_public_access_to_host>
       }
     }

     resource "yandex_mdb_mongodb_database" "<DB_name>" {
       cluster_id = yandex_mdb_mongodb_cluster.<cluster_name>.id
       name       = "<DB_name>"
     }

     resource "yandex_mdb_mongodb_user" "<username>" {
       cluster_id = yandex_mdb_mongodb_cluster.<cluster_name>.id
       name       = "<username>"
       password   = "<password>"
       permission {
         database_name = "<DB_name>"
         roles         = [ "<list_of_user_roles>" ]
       }
       depends_on = [
         yandex_mdb_mongodb_database.<DB_name>
       ]
     }

     resource "yandex_vpc_network" "<network_name>" { name = "<network_name>" }

     resource "yandex_vpc_subnet" "<subnet_name>" {
       name           = "<subnet_name>"
       zone           = "<availability_zone>"
       network_id     = yandex_vpc_network.<network_name>.id
       v4_cidr_blocks = ["<range>"]
     }
     ```

     {% endcut %}

     {% cut "For a cluster with standard sharding" %}

     ```hcl
     resource "yandex_mdb_mongodb_cluster" "<cluster_name>" {
       name                = "<cluster_name>"
       environment         = "<environment>"
       network_id          = yandex_vpc_network.<network_name>.id
       security_group_ids  = [ "<list_of_security_group_IDs>" ]
       deletion_protection = <protect_cluster_from_deletion>

       cluster_config {
         version = "<Yandex_StoreDoc_version>"
       }

       resources_mongod {
         resource_preset_id = "<host_class>"
         disk_type_id       = "<disk_type>"
         disk_size          = <storage_size_in_GB>
       }

       host {
         type             = "mongod"
         zone_id          = "<availability_zone>"
         subnet_id        = yandex_vpc_subnet.<subnet_name>.id
         host_parameters {
           hidden               = <hide_host>
           secondary_delay_secs = <replica_lag_in_seconds>
           priority             = <host_priority>
         }
       }

       resources_mongoinfra {
         resource_preset_id = "<host_class>"
         disk_type_id       = "<disk_type>"
         disk_size          = <storage_size_in_GB>
       }

       host {
         type             = "mongoinfra"
         zone_id          = "<availability_zone>"
         subnet_id        = yandex_vpc_subnet.<subnet_name>.id
         assign_public_ip = <allow_public_access_to_host>
       }
     }

     resource "yandex_mdb_mongodb_database" "<DB_name>" {
       cluster_id = yandex_mdb_mongodb_cluster.<cluster_name>.id
       name       = "<DB_name>"
     }

     resource "yandex_mdb_mongodb_user" "<username>" {
       cluster_id = yandex_mdb_mongodb_cluster.<cluster_name>.id
       name       = "<username>"
       password   = "<password>"
       permission {
         database_name = "<DB_name>"
         roles         = [ "<list_of_user_roles>" ]
       }
       depends_on = [
         yandex_mdb_mongodb_database.<DB_name>
       ]
     }

     resource "yandex_vpc_network" "<network_name>" { name = "<network_name>" }

     resource "yandex_vpc_subnet" "<subnet_name>" {
       name           = "<subnet_name>"
       zone           = "<availability_zone>"
       network_id     = yandex_vpc_network.<network_name>.id
       v4_cidr_blocks = ["<range>"]
     }
     ```

     {% endcut %}

     {% cut "For a cluster with advanced sharding" %}

     ```hcl
     resource "yandex_mdb_mongodb_cluster" "<cluster_name>" {
       name                = "<cluster_name>"
       environment         = "<environment>"
       network_id          = yandex_vpc_network.<network_name>.id
       security_group_ids  = [ "<list_of_security_group_IDs>" ]
       deletion_protection = <protect_cluster_from_deletion>

       cluster_config {
         version = "<Yandex_StoreDoc_version>"
       }

       resources_mongod {
         resource_preset_id = "<host_class>"
         disk_type_id       = "<disk_type>"
         disk_size          = <storage_size_in_GB>
       }

       host {
         type             = "mongod"
         zone_id          = "<availability_zone>"
         subnet_id        = yandex_vpc_subnet.<subnet_name>.id
         host_parameters {
           hidden               = <hide_host>
           secondary_delay_secs = <replica_lag_in_seconds>
           priority             = <host_priority>
         }
       }

       resources_mongos {
         resource_preset_id = "<host_class>"
         disk_type_id       = "<disk_type>"
         disk_size          = <storage_size_in_GB>
       }

       host {
         type             = "mongos"
         zone_id          = "<availability_zone>"
         subnet_id        = yandex_vpc_subnet.<subnet_name>.id
         assign_public_ip = <allow_public_access_to_host>
       }

       resources_mongocfg {
         resource_preset_id = "<host_class>"
         disk_type_id       = "<disk_type>"
         disk_size          = <storage_size_in_GB>
       }

       host {
         type             = "mongocfg"
         zone_id          = "<availability_zone>"
         subnet_id        = yandex_vpc_subnet.<subnet_name>.id
       }
     }

     resource "yandex_mdb_mongodb_database" "<DB_name>" {
       cluster_id = yandex_mdb_mongodb_cluster.<cluster_name>.id
       name       = "<DB_name>"
     }

     resource "yandex_mdb_mongodb_user" "<username>" {
       cluster_id = yandex_mdb_mongodb_cluster.<cluster_name>.id
       name       = "<username>"
       password   = "<password>"
       permission {
         database_name = "<DB_name>"
         roles         = [ "<list_of_user_roles>" ]
       }
       depends_on = [
         yandex_mdb_mongodb_database.<DB_name>
       ]
     }

     resource "yandex_vpc_network" "<network_name>" { name = "<network_name>" }

     resource "yandex_vpc_subnet" "<subnet_name>" {
       name           = "<subnet_name>"
       zone           = "<availability_zone>"
       network_id     = yandex_vpc_network.<network_name>.id
       v4_cidr_blocks = ["<range>"]
     }
     ```

     {% endcut %}

     Where:

     * `environment`: Environment, `PRESTABLE` or `PRODUCTION`.
     * `host`: Host settings:
       * `zone_id`: Availability zone.
       * `subnet_id`: ID of the subnet in the selected availability zone.
       * `assign_public_ip`: Public access to the host, `true` or `false`. In a sharded cluster, it is used only for `MONGOS` and `MONGOINFRA` hosts.
       * `host_parameters`: Additional host settings:
         * `hidden`: Determines whether the host is hidden, `true` or `false`. If the host is hidden, only direct connections will be able to read from it (for example, to make backups from it without adding load to the cluster).
         * `secondary_delay_secs`: Replica's lag behind the master in seconds. This setting can be useful for data recovery after operational errors.
         * `priority`: [Host priority for master promotion](../concepts/replication.md#master-failover).

        {% note info %}

        The `hidden`, `secondary_delay_secs`, and `priority` parameters are used for `MONGOD` hosts only.

        {% endnote %}
     
     * `deletion_protection`: Cluster deletion protection, `true` or `false`.

        Even with cluster deletion protection enabled, one can still delete a user or database or connect manually and delete the database contents.

     * `version`: Yandex StoreDoc version, 6.0, 7.0, or 8.0.

     A database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. Such names as `config`, `local`, `admin`, and `mdb_internal` are reserved for Yandex StoreDoc. You cannot create DBs with these names.

     To set up the [maintenance window](../concepts/maintenance.md) that will also apply to stopped clusters, add the `maintenance_window` section to the cluster description:
     
     ```hcl
     resource "yandex_mdb_mongodb_cluster" "<cluster_name>" {
       ...
       maintenance_window {
         type = "<maintenance_type>"
         day  = "<day_of_week>"
         hour = <hour>
       }
       ...
     }
     ```
     
     Where:
     
     * `type`: Maintenance type. The possible values include:
         * `ANYTIME`: Any time.
         * `WEEKLY`: On a schedule.
     * `day`: Day of week for the `WEEKLY` type, i.e., `MON`, `TUE`, `WED`, `THU`, `FRI`, `SAT`, or `SUN`.
     * `hour`: Sequence number of UTC hour interval for the `WEEKLY` type, from `1` to `24`.
     
       > For example, `1` stands for the interval from `00:00` to `01:00`, and `5`, from `04:00` to `05:00`.

     To encrypt the disk with a [custom KMS key](../../kms/concepts/key.md), add the `disk_encryption_key_id` argument:

       ```hcl
       resource "yandex_mdb_mongodb_cluster" "<cluster_name>" {
         ...
         disk_encryption_key_id = <KMS_key_ID>
         ...
       }
       ```

       To learn more about disk encryption, see [Storage](../concepts/storage.md#disk-encryption).

     For more information about the resources you can create with Terraform, see [this provider guide](../../terraform/resources/mdb_mongodb_cluster.md).

  1. Make sure the settings are correct.

      1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
      1. Run this command:
      
         ```bash
         terraform validate
         ```
      
         Terraform will show any errors found in your configuration files.

  1. Create the cluster.

      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.

      This will create all required resources in the specified folder, and you will see the [host FQDNs](../concepts/network.md#hostname) in the terminal. You can check the new resources and their settings in the [management console](https://console.yandex.cloud).

      {% note warning "Timeouts" %}
      
      The Terraform provider sets the following timeouts for Yandex StoreDoc cluster operations:
      
      * Creating a cluster, including by restoring one from a backup: 30 minutes.
      * Editing a cluster: 60 minutes.
      
      Operations exceeding the set timeout are interrupted.
      
      {% cut "How do I change these limits?" %}
      
      Add the `timeouts` block to the cluster description, for example:
      
      ```hcl
      resource "yandex_mdb_mongodb_cluster" "<cluster_name>" {
        ...
        timeouts {
          create = "1h30m" # An hour and a half
          update = "2h"    # Two hours
        }
      }
      ```
      
      {% endcut %}
      
      {% endnote %}


- REST API {#api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Create a file named `body.json` and paste the following code into it:

        {% cut "For a non-sharded cluster" %}

        
        ```json
        {
          "folderId": "<folder_ID>",
          "name": "<cluster_name>",
          "environment": "<environment>",
          "networkId": "<network_ID>",
          "securityGroupIds": [
            "<security_group_1_ID>",
            "<security_group_2_ID>",
            ...
            "<security_group_N_ID>"
          ],
          "deletionProtection": <protect_cluster_from_deletion>,
          "maintenanceWindow": {
            "weeklyMaintenanceWindow": {
              "day": "<day_of_week>",
              "hour": "<hour>"
            }
          },
          "configSpec": {
            "version": "<Yandex_StoreDoc_version>",
            "mongodb": {
              "mongod": {
                "resources": {
                  "resourcePresetId": "<host_class>",
                  "diskSize": "<storage_size_in_bytes>",
                  "diskTypeId": "<disk_type>"
                }
              }
            },
            "backupWindowStart":  {
              "hours": "<hours>",
              "minutes": "<minutes>",
              "seconds": "<seconds>",
              "nanos": "<nanoseconds>"
            },  
            "backupRetainPeriodDays": "<backup_retention_in_days>",
            "performanceDiagnostics": {
              "profilingEnabled": <enable_profiler>
            }
          },
          "databaseSpecs": [
            {
              "name": "<DB_name>"
            },
            { <similar_settings_for_DB_2> },
            { ... },
            { <similar_settings_for_DB_N> }
          ],
          "userSpecs": [
            {
              "name": "<username>",
              "password": "<user_password>",
              "permissions": [
                {
                  "databaseName": "<DB_name>",
                  "roles": [
                    "<role_1>", "<role_2>", ..., "<role_N>"
                  ]
                }
              ]
            },
            { <similar_settings_for_user_2> },
            { ... },
            { <similar_settings_for_user_N> }
          ],
          "hostSpecs": [
            {
              "zoneId": "<availability_zone>",
              "subnetId": "<subnet_ID>",
              "assignPublicIp": <allow_public_access_to_host>,
              "type": "MONGOD",
              "hidden": <hide_host>,
              "secondaryDelaySecs": "<replica_lag_in_seconds>",
              "priority": "<host_priority>",
              "tags": "<host_labels>"
            },
            { <similar_settings_for_host_2> },
            { ... },
            { <similar_settings_for_host_N> }
          ],
        }
        ```


        {% endcut %}

        {% cut "For a cluster with standard sharding" %}

        
        ```json
        {
          "folderId": "<folder_ID>",
          "name": "<cluster_name>",
          "environment": "<environment>",
          "networkId": "<network_ID>",
          "securityGroupIds": [
            "<security_group_1_ID>",
            "<security_group_2_ID>",
            ...
            "<security_group_N_ID>"
          ],
          "deletionProtection": <protect_cluster_from_deletion>,
          "maintenanceWindow": {
            "weeklyMaintenanceWindow": {
              "day": "<day_of_week>",
              "hour": "<hour>"
            }
          },
          "configSpec": {
            "version": "<Yandex_StoreDoc_version>",
            "mongodb": {
              "mongod": {
                "resources": {
                  "resourcePresetId": "<host_class>",
                  "diskSize": "<storage_size_in_bytes>",
                  "diskTypeId": "<disk_type>"
                }
              },
              "mongoinfra": {
                "resources": {
                  "resourcePresetId": "<host_class>",
                  "diskSize": "<storage_size_in_bytes>",
                  "diskTypeId": "<disk_type>"
                }
              }
            },
            "backupWindowStart":  {
              "hours": "<hours>",
              "minutes": "<minutes>",
              "seconds": "<seconds>",
              "nanos": "<nanoseconds>"
            },
            "backupRetainPeriodDays": "<backup_retention_in_days>",
            "performanceDiagnostics": {
              "profilingEnabled": <enable_profiler>
            }
          },
          "databaseSpecs": [
            {
              "name": "<DB_name>"
            },
            { <similar_settings_for_DB_2> },
            { ... },
            { <similar_settings_for_DB_N> }
          ],
          "userSpecs": [
            {
              "name": "<username>",
              "password": "<user_password>",
              "permissions": [
                {
                  "databaseName": "<DB_name>",
                  "roles": [
                    "<role_1>", "<role_2>", ..., "<role_N>"
                  ]
                }
              ]
            },
            { <similar_settings_for_user_2> },
            { ... },
            { <similar_settings_for_user_N> }
          ],
          "hostSpecs": [
            {
              "zoneId": "<availability_zone>",
              "subnetId": "<subnet_ID>",
              "type": "MONGOD",
              "shardName": "<shard_name>",
              "hidden": <hide_host>,
              "secondaryDelaySecs": "<replica_lag_in_seconds>",
              "priority": "<host_priority>",
              "tags": "<host_labels>"
            },
            {
              "zoneId": "<availability_zone>",
              "subnetId": "<subnet_ID>",
              "type": "MONGOINFRA",
              "assignPublicIp": <allow_public_access_to_host>,
              "tags": "<host_labels>"
            },
            { <similar_settings_for_host_3> },
            { ... },
            { <similar_settings_for_host_N> }
          ]
        }
        ```


        {% endcut %}

        {% cut "For a cluster with advanced sharding" %}

        
        ```json
        {
          "folderId": "<folder_ID>",
          "name": "<cluster_name>",
          "environment": "<environment>",
          "networkId": "<network_ID>",
          "securityGroupIds": [
            "<security_group_1_ID>",
            "<security_group_2_ID>",
            ...
            "<security_group_N_ID>"
          ],
          "deletionProtection": <protect_cluster_from_deletion>,
          "maintenanceWindow": {
            "weeklyMaintenanceWindow": {
              "day": "<day_of_week>",
              "hour": "<hour>"
            }
          },
          "configSpec": {
            "version": "<Yandex_StoreDoc_version>",
            "mongodb": {
              "mongod": {
                "resources": {
                  "resourcePresetId": "<host_class>",
                  "diskSize": "<storage_size_in_bytes>",
                  "diskTypeId": "<disk_type>"
                }
              },
              "mongos": {
                "resources": {
                  "resourcePresetId": "<host_class>",
                  "diskSize": "<storage_size_in_bytes>",
                  "diskTypeId": "<disk_type>"
                }
              },
              "mongocfg": {
                "resources": {
                  "resourcePresetId": "<host_class>",
                  "diskSize": "<storage_size_in_bytes>",
                  "diskTypeId": "<disk_type>"
                }
              }
            },
            "backupWindowStart":  {
              "hours": "<hours>",
              "minutes": "<minutes>",
              "seconds": "<seconds>",
              "nanos": "<nanoseconds>"
            },  
            "backupRetainPeriodDays": "<backup_retention_in_days>",
            "performanceDiagnostics": {
              "profilingEnabled": <enable_profiler>
            }
          },
          "databaseSpecs": [
            {
              "name": "<DB_name>"
            },
            { <similar_settings_for_DB_2> },
            { ... },
            { <similar_settings_for_DB_N> }
          ],
          "userSpecs": [
            {
              "name": "<username>",
              "password": "<user_password>",
              "permissions": [
                {
                  "databaseName": "<DB_name>",
                  "roles": [
                    "<role_1>", "<role_2>", ..., "<role_N>"
                  ]
                }
              ]
            },
            { <similar_settings_for_user_2> },
            { ... },
            { <similar_settings_for_user_N> }
          ],
          "hostSpecs": [
            {
              "zoneId": "<availability_zone>",
              "subnetId": "<subnet_ID>",
              "type": "MONGOD",
              "shardName": "<shard_name>",
              "hidden": <hide_host>,
              "secondaryDelaySecs": "<replica_lag_in_seconds>",
              "priority": "<host_priority>",
              "tags": "<host_labels>"
            },
            {
              "zoneId": "<availability_zone>",
              "subnetId": "<subnet_ID>",
              "type": "MONGOS",
              "assignPublicIp": <allow_public_access_to_host>,
              "tags": "<host_labels>"
            },
            {
              "zoneId": "<availability_zone>",
              "subnetId": "<subnet_ID>",
              "type": "MONGOCFG",
              "tags": "<host_labels>"
            },
            { <similar_settings_for_host_4> },
            { ... },
            { <similar_settings_for_host_N> }
          ]
        }
        ```


        {% endcut %}

        Where:

        * `folderId`: Folder ID. You can get it from the [list of your cloud folders](../../resource-manager/operations/folder/get-id.md).
        * `name`: Cluster name.
        * `environment`: Cluster environment, `PRODUCTION` or `PRESTABLE`.
        * `networkId`: ID of the [network](../../vpc/concepts/network.md#network) where your cluster will be deployed.

        
        * `securityGroupIds`: [Security group](../concepts/network.md#security-groups) IDs.


        * `deletionProtection`: Cluster deletion protection, `true` or `false`.

          Even with cluster deletion protection enabled, one can still delete a user or database or connect manually and delete the database contents.

        * `maintenanceWindow`: [Maintenance window](../concepts/maintenance.md) settings, applying to both running and stopped clusters. In `maintenanceWindow`, provide one of these two parameters:

          * `anytime`: Maintenance takes place at any time.
          * `weeklyMaintenanceWindow`: Maintenance takes place once a week at the specified time:

            * `day`: Day of the week, in `DDD` format.
            * `hour`: Hour of the day, in `HH` format. Allowed values range from `1` to `24` hours.

        * `configSpec`: Cluster settings:

            * `version`: Yandex StoreDoc version, 5.0, 6.0, or 7.0.
            * `mongod`, `mongoinfra`, `mongos`, `mongocfg`: Host types.

              * `resources`: Cluster resources:

                * `resourcePresetId`: [Host class](../concepts/instance-types.md).
                * `diskSize`: Disk size in bytes.
                * `diskTypeId`: [Disk type](../concepts/storage.md).

            * `backupWindowStart`: [Backup](../concepts/backup.md) window settings.

              Here, specify the backup start time:

              * `hours`: Between `0` and `23` hours.
              * `minutes`: Between `0` and `59` minutes.
              * `seconds`: Between `0` and `59` seconds.
              * `nanos`: Between `0` and `999999999` nanoseconds.

            * `backupRetainPeriodDays`: Backup retention time in days.

            * `performanceDiagnostics`: [Statistics collection](performance-diagnostics.md#activate-stats-collector) settings:
              * `profilingEnabled`: Enable [profiler](tools.md#explore-profiler), `true` or `false`.

        * `databaseSpecs`: Database settings as an array of elements, one per database. Each element contains a database `name`.

            A database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. Such names as `config`, `local`, `admin`, and `mdb_internal` are reserved for Yandex StoreDoc. You cannot create DBs with these names.

        * `userSpecs`: User settings as an array of elements, one per user. Each element has the following structure:

          * `name`: Username.
          * `password`: Password.
          * `permissions`: User permissions:

            * `databaseName`: Name of the database the user can access.
            * `roles`: Array of user roles. Each role is provided as a separate string in the array. For the list of possible values, see [Users and roles](../concepts/users-and-roles.md).

            In the `permissions` array, add a separate element with permission settings for each database.

        * `hostSpecs`: Cluster host settings as an array of elements, one per host. Each element has the following structure:

          
          * `zoneId`: [Availability zone](../../overview/concepts/geo-scope.md).
          * `subnetId`: [Subnet ID](../../vpc/concepts/network.md#subnet).
          * `assignPublicIp`: Internet access to the host via a public IP address, `true` or `false`. In a sharded cluster, it is used only for `MONGOS` and `MONGOINFRA` hosts.
          * `type`: Host type in a sharded cluster, `MONGOD`, `MONGOINFRA`, `MONGOS`, or `MONGOCFG`.
          * `tags`: Host tags.
          * `shard_name`: Shard name in a sharded cluster (for `MONGOD` hosts only).
          * `hidden`: Determines whether the host is hidden, `true` or `false`. If the host is hidden, only direct connections will be able to read from it (for example, to make backups from it without adding load to the cluster).
          * `secondaryDelaySecs`: Replica's lag behind the master in seconds. This setting can be useful for data recovery after operational errors.
          * `priority`: [Host priority for master promotion](../concepts/replication.md#master-failover).

          {% note info %}

          The `shardName`, `hidden`, `secondaryDelaySecs`, and `priority` parameters are used for `MONGOD` hosts only.

          {% endnote %}


  1. Call the [Cluster.Create](../api-ref/Cluster/create.md) method, e.g., via the following [cURL](https://curl.se/) request:

      ```bash
      curl \
          --request POST \
          --header "Authorization: Bearer $IAM_TOKEN" \
          --header "Content-Type: application/json" \
          --url 'https://mdb.api.cloud.yandex.net/managed-mongodb/v1/clusters' \
          --data "@body.json"
      ```

  1. Check the [server response](../api-ref/Cluster/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

      ```bash
      export IAM_TOKEN="<IAM_token>"
      ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Create a file named `body.json` and paste the following code into it:

        {% cut "For a non-sharded cluster" %}

        
        ```json
        {
          "folder_id": "<folder_ID>",
          "name": "<cluster_name>",
          "environment": "<environment>",
          "network_id": "<network_ID>",
          "security_group_ids": [
            "<security_group_1_ID>",
            "<security_group_2_ID>",
            ...
            "<security_group_N_ID>"
          ],
          "deletion_protection": <protect_cluster_from_deletion>,
          "maintenance_window": {
            "weekly_maintenance_window": {
              "day": "<day_of_week>",
              "hour": "<hour>"
            }
          },
          "config_spec": {
            "version": "<Yandex_StoreDoc_version>",
            "mongodb": {
              "mongod": {
                "resources": {
                  "resource_preset_id": "<host_class>",
                  "disk_size": "<storage_size_in_bytes>",
                  "disk_type_id": "<disk_type>"
                }
              }
            },
            "backup_window_start": {
              "hours": "<hours>",
              "minutes": "<minutes>",
              "seconds": "<seconds>",
              "nanos": "<nanoseconds>"
            },
            "backup_retain_period_days": "<backup_retention_in_days>",
            "performance_diagnostics": {
              "profiling_enabled": <enable_profiler>
            }
          },
          "database_specs": [
            {
              "name": "<DB_name>"
            },
            { <similar_settings_for_DB_2> },
            { ... },
            { <similar_settings_for_DB_N> }
          ],
          "user_specs": [
            {
              "name": "<username>",
              "password": "<user_password>",
              "permissions": [
                {
                  "database_name": "<DB_name>",
                  "roles": [
                    "<role_1>", "<role_2>", ..., "<role_N>"
                  ]
                }
              ]
            },
            { <similar_settings_for_user_2> },
            { ... },
            { <similar_settings_for_user_N> }
          ],
          "host_specs": [
            {
              "zone_id": "<availability_zone>",
              "subnet_id": "<subnet_ID>",
              "assign_public_ip": <allow_public_access_to_host>,
              "type": "MONGOD",
              "hidden": <hide_host>,
              "secondary_delay_secs": "<replica_lag_in_seconds>",
              "priority": "<host_priority>",
              "tags": "<host_labels>"
            },
            { <similar_settings_for_host_2> },
            { ... },
            { <similar_settings_for_host_N> }
          ]
        }
        ```


        {% endcut %}

        {% cut "For a cluster with standard sharding" %}

        
        ```json
        {
          "folder_id": "<folder_ID>",
          "name": "<cluster_name>",
          "environment": "<environment>",
          "network_id": "<network_ID>",
          "security_group_ids": [
            "<security_group_1_ID>",
            "<security_group_2_ID>",
            ...
            "<security_group_N_ID>"
          ],
          "deletion_protection": <protect_cluster_from_deletion>,
          "maintenance_window": {
            "weekly_maintenance_window": {
              "day": "<day_of_week>",
              "hour": "<hour>"
            }
          },
          "config_spec": {
            "version": "<Yandex_StoreDoc_version>",
            "mongodb": {
              "mongod": {
                "resources": {
                  "resource_preset_id": "<host_class>",
                  "disk_size": "<storage_size_in_bytes>",
                  "disk_type_id": "<disk_type>"
                }
              },
              "mongoinfra": {
                "resources": {
                  "resource_preset_id": "<host_class>",
                  "disk_size": "<storage_size_in_bytes>",
                  "disk_type_id": "<disk_type>"
                }
              }
            },
            "backup_window_start": {
              "hours": "<hours>",
              "minutes": "<minutes>",
              "seconds": "<seconds>",
              "nanos": "<nanoseconds>"
            },
            "backup_retain_period_days": "<backup_retention_in_days>",
            "performance_diagnostics": {
              "profiling_enabled": <enable_profiler>
            }
          },
          "database_specs": [
            {
              "name": "<DB_name>"
            },
            { <similar_settings_for_DB_2> },
            { ... },
            { <similar_settings_for_DB_N> }
          ],
          "user_specs": [
            {
              "name": "<username>",
              "password": "<user_password>",
              "permissions": [
                {
                  "database_name": "<DB_name>",
                  "roles": [
                    "<role_1>", "<role_2>", ..., "<role_N>"
                  ]
                }
              ]
            },
            { <similar_settings_for_user_2> },
            { ... },
            { <similar_settings_for_user_N> }
          ],
          "host_specs": [
            {
              "zone_id": "<availability_zone>",
              "subnet_id": "<subnet_ID>",
              "type": "MONGOD",
              "shard_name": "<shard_name>",
              "hidden": <hide_host>,
              "secondary_delay_secs": "<replica_lag_in_seconds>",
              "priority": "<host_priority>",
              "tags": "<host_labels>"
            },
            {
              "zone_id": "<availability_zone>",
              "subnet_id": "<subnet_ID>",
              "type": "MONGOINFRA",
              "assign_public_ip": <allow_public_access_to_host>,
              "tags": "<host_labels>"
            },
            { <similar_settings_for_host_3> },
            { ... },
            { <similar_settings_for_host_N> }
          ]
        }
        ```


        {% endcut %}

        {% cut "For a cluster with advanced sharding" %}

        
        ```json
        {
          "folder_id": "<folder_ID>",
          "name": "<cluster_name>",
          "environment": "<environment>",
          "network_id": "<network_ID>",
          "security_group_ids": [
            "<security_group_1_ID>",
            "<security_group_2_ID>",
            ...
            "<security_group_N_ID>"
          ],
          "deletion_protection": <protect_cluster_from_deletion>,
          "maintenance_window": {
            "weekly_maintenance_window": {
              "day": "<day_of_week>",
              "hour": "<hour>"
            }
          },
          "config_spec": {
            "version": "<Yandex_StoreDoc_version>",
            "mongodb": {
              "mongod": {
                "resources": {
                  "resource_preset_id": "<host_class>",
                  "disk_size": "<storage_size_in_bytes>",
                  "disk_type_id": "<disk_type>"
                }
              },
              "mongos": {
                "resources": {
                  "resource_preset_id": "<host_class>",
                  "disk_size": "<storage_size_in_bytes>",
                  "disk_type_id": "<disk_type>"
                }
              },
              "mongocfg": {
                "resources": {
                  "resource_preset_id": "<host_class>",
                  "disk_size": "<storage_size_in_bytes>",
                  "disk_type_id": "<disk_type>"
                }
              }
            },
            "backup_window_start": {
              "hours": "<hours>",
              "minutes": "<minutes>",
              "seconds": "<seconds>",
              "nanos": "<nanoseconds>"
            },
            "backup_retain_period_days": "<backup_retention_in_days>",
            "performance_diagnostics": {
              "profiling_enabled": <enable_profiler>
            }
          },
          "database_specs": [
            {
              "name": "<DB_name>"
            },
            { <similar_settings_for_DB_2> },
            { ... },
            { <similar_settings_for_DB_N> }
          ],
          "user_specs": [
            {
              "name": "<username>",
              "password": "<user_password>",
              "permissions": [
                {
                  "database_name": "<DB_name>",
                  "roles": [
                    "<role_1>", "<role_2>", ..., "<role_N>"
                  ]
                }
              ]
            },
            { <similar_settings_for_user_2> },
            { ... },
            { <similar_settings_for_user_N> }
          ],
          "host_specs": [
            {
              "zone_id": "<availability_zone>",
              "subnet_id": "<subnet_ID>",
              "type": "MONGOD",
              "shard_name": "<shard_name>",
              "hidden": <hide_host>,
              "secondary_delay_secs": "<replica_lag_in_seconds>",
              "priority": "<host_priority>",
              "tags": "<host_labels>"
            },
            {
              "zone_id": "<availability_zone>",
              "subnet_id": "<subnet_ID>",
              "type": "MONGOS",
              "assign_public_ip": <allow_public_access_to_host>,
              "tags": "<host_labels>"
            },
            {
              "zone_id": "<availability_zone>",
              "subnet_id": "<subnet_ID>",
              "type": "MONGOCFG",
              "tags": "<host_labels>"
            },
            { <similar_settings_for_host_4> },
            { ... },
            { <similar_settings_for_host_N> }
          ]
        }
        ```


        {% endcut %}

        Where:

        * `folder_id`: Folder ID. You can get it with the [list of folders in the cloud](../../resource-manager/operations/folder/get-id.md).
        * `name`: Cluster name.
        * `environment`: Cluster environment, `PRODUCTION` or `PRESTABLE`.
        * `network_id`: ID of the [network](../../vpc/concepts/network.md#network) where your cluster will be deployed.

        
        * `security_group_ids`: [Security group](../concepts/network.md#security-groups) IDs.


        * `deletion_protection`: Cluster deletion protection, `true` or `false`.

          Even with cluster deletion protection enabled, one can still delete a user or database or connect manually and delete the database contents.

        * `maintenance_window`: [Maintenance window](../concepts/maintenance.md) settings, applying to both running and stopped clusters. In `maintenance_window`, provide one of these two parameters:

          * `anytime`: Maintenance takes place at any time.
          * `weekly_maintenance_window`: Maintenance takes place once a week at the specified time:

            * `day`: Day of the week, in `DDD` format.
            * `hour`: Hour of the day, in `HH` format. Allowed values range from `1` to `24` hours.

        * `config_spec`: Cluster settings:

          * `version`: Yandex StoreDoc version, 5.0, 6.0, or 7.0.
            * `mongod`, `mongoinfra`, `mongos`, `mongocfg`: Host types.

              * `resources`: Cluster resources:

                * `resource_preset_id`: [Host class](../concepts/instance-types.md).
                * `disk_size`: Disk size, in bytes.
                * `disk_type_id`: [Disk type](../concepts/storage.md).

            * `backup_window_start`: [Backup](../concepts/backup.md) window settings.

              Here, specify the backup start time:

              * `hours`: Between `0` and `23` hours.
              * `minutes`: Between `0` and `59` minutes.
              * `seconds`: Between `0` and `59` seconds.
              * `nanos`: Between `0` and `999999999` nanoseconds.

            * `backup_retain_period_days`: Backup retention time in days.

            * `performance_diagnostics`: [Statistics collection](performance-diagnostics.md#activate-stats-collector) settings:
              * `profiling_enabled`: Enable the [profiler](tools.md#explore-profiler), `true` or `false`.

        * `database_specs`: Database settings as an array of elements, one per database. Each element contains a database `name`.

            A database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. Such names as `config`, `local`, `admin`, and `mdb_internal` are reserved for Yandex StoreDoc. You cannot create DBs with these names.

        * `user_specs`: User settings as an array of elements, one per user. Each element has the following structure:

          * `name`: Username.
          * `password`: Password.
          * `permissions`: User permissions:

            * `database_name`: Name of the database the user can access.
            * `roles`: Array of user roles. Each role is provided as a separate string in the array. For the list of possible values, see [Users and roles](../concepts/users-and-roles.md).

            In the `permissions` array, add a separate element with permission settings for each database.

        * `host_specs`: Cluster host settings as an array of elements, one per host. Each element has the following structure:

          
          * `zone_id`: [Availability zone](../../overview/concepts/geo-scope.md).
          * `subnet_id`: [Subnet ID](../../vpc/concepts/network.md#subnet).
          * `assign_public_ip`: Internet access to the host via a public IP address, `true` or `false`. In a sharded cluster, it is used only for `MONGOS` and `MONGOINFRA` hosts.
          * `type`: Host type in a sharded cluster, `MONGOD`, `MONGOINFRA`, `MONGOS`, or `MONGOCFG`.
          * `tags`: Host tags.
          * `shard_name`: Shard name in a sharded cluster.
          * `hidden`: Determines whether the host is hidden, `true` or `false`. If the host is hidden, only direct connections will be able to read from it (for example, to make backups from it without adding load to the cluster).
          * `secondaryDelaySecs`: Replica's lag behind the master in seconds. This setting can be useful for data recovery after operational errors.
          * `priority`: [Host priority for master promotion](../concepts/replication.md#master-failover).

          {% note info %}

          The `shard_name`, `hidden`, `secondaryDelaySecs`, and `priority` parameters are used for `MONGOD` hosts only.

          {% endnote %}


  1. Call the [ClusterService.Create](../api-ref/grpc/Cluster/create.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

      ```bash
      grpcurl \
          -format json \
          -import-path ~/cloudapi/ \
          -import-path ~/cloudapi/third_party/googleapis/ \
          -proto ~/cloudapi/yandex/cloud/mdb/mongodb/v1/cluster_service.proto \
          -rpc-header "Authorization: Bearer $IAM_TOKEN" \
          -d @ \
          mdb.api.cloud.yandex.net:443 \
          yandex.cloud.mdb.mongodb.v1.ClusterService.Create \
          < body.json
      ```

  1. Check the [server response](../api-ref/grpc/Cluster/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}


{% note warning %}

If you specified security group IDs when creating a cluster, you may need to [configure these security groups](connect/index.md#configuring-security-groups) to enable cluster access.

{% endnote %}



## Creating a cluster copy {#duplicate}

You can create a Yandex StoreDoc cluster with the settings of another one created earlier. Do this by importing the original Yandex StoreDoc cluster configuration into Terraform. This lets you either create an exact replica or use the imported configuration as a baseline for customization. The import feature is handy when the original Yandex StoreDoc cluster has a lot of settings and you need to create a similar one.

To create a Yandex StoreDoc cluster copy:

{% list tabs group=instructions %}

- 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. In your current working directory, create a `.tf` file with the following contents:

        ```hcl
        resource "yandex_mdb_mongodb_cluster" "old" { }
        ```

    1. Save the ID of the original Yandex StoreDoc cluster to an environment variable:

        ```bash
        export STOREDOC_CLUSTER_ID=<cluster_ID>
        ```

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

    1. Import the original Yandex StoreDoc cluster configuration into Terraform:

        ```bash
        terraform import yandex_mdb_mongodb_cluster.old ${STOREDOC_CLUSTER_ID}
        ```

    1. Get the imported configuration:

        ```bash
        terraform show
        ```

    1. Copy it from your terminal and paste it into the `.tf` file.
    1. Create a new directory `imported-cluster` and move your configuration file there.
    1. Modify the configuration so that you can use it to create a new cluster:

        * Specify the new cluster name in the `resource` string and in the `name` argument.
        * Delete the `created_at`, `health`, `id`, `sharded`, and `status` arguments.
        * In the `host` sections, delete the `health` and `name` arguments.
        * If the `maintenance_window` section contains `type = "ANYTIME"`, delete the `hour` argument.
        * Delete all `user` sections. Use `yandex_mdb_mongodb_user` resource to add database users.
        * Optionally, you can customize the configuration further as needed.

    1. [Get the authentication credentials](../../tutorials/infrastructure-management/terraform-quickstart.md#get-credentials) in the `imported-cluster` directory.

    1. In the same directory, [configure and initialize the provider](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). Instead of manually creating the provider configuration file, you can [download it](https://github.com/yandex-cloud-examples/yc-terraform-provider-settings/blob/main/provider.tf).

    1. Move the configuration file to the `imported-cluster` directory and [specify the arguments](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). If you have not set the authentication credentials as environment variables, specify them in the configuration file.

    1. Validate your Terraform configuration:

        ```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).

    {% note warning "Timeouts" %}
    
    The Terraform provider sets the following timeouts for Yandex StoreDoc cluster operations:
    
    * Creating a cluster, including by restoring one from a backup: 30 minutes.
    * Editing a cluster: 60 minutes.
    
    Operations exceeding the set timeout are interrupted.
    
    {% cut "How do I change these limits?" %}
    
    Add the `timeouts` block to the cluster description, for example:
    
    ```hcl
    resource "yandex_mdb_mongodb_cluster" "<cluster_name>" {
      ...
      timeouts {
        create = "1h30m" # An hour and a half
        update = "2h"    # Two hours
      }
    }
    ```
    
    {% endcut %}
    
    {% endnote %}

{% endlist %}


## Examples {#examples}

### Creating a single-host cluster {#creating-a-single-host-cluster}

{% list tabs group=instructions %}

- CLI {#cli}

  Create a Yandex StoreDoc cluster with the following test specifications:

  
  * Name: `mymg`.
  * Environment: `production`.
  * Network: `default`.
  * Security group ID: `enp6saqnq4ie244g67sb`.
  * `s2.micro` host in the `b0rcctk2rvtr********` subnet and the `ru-central1-a` availability zone.
  * Network SSD storage (`network-ssd`): 20 GB.
  * Username: `user1`, password: `user1user1`.
  * Database: `db1`.
  * Deletion protection: Enabled.


  Run this command:

  
  ```bash
  yc managed-mongodb cluster create \
    --name mymg \
    --environment production \
    --network-name default \
    --security-group-ids enp6saqnq4ie244g67sb \
    --mongod-resource-preset s2.micro \
    --host zone-id=ru-central1-a,subnet-id=b0rcctk2rvtr******** \
    --mongod-disk-size 20 \
    --mongod-disk-type network-ssd \
    --user name=user1,password=user1user1 \
    --database name=db1 \
    --deletion-protection
  ```



- Terraform {#tf}

  Create a Yandex StoreDoc cluster and its supporting network, using the following test specifications:

  * Name: `mymg`.
  * Version: `8.0`.
  * Environment: `PRODUCTION`.
  * Cloud ID: `b1gq90dgh25bebiu75o`.
  * Folder ID: `b1gia87mbaomkfvsleds`.
  * Network: `mynet`.
  * Host class: `s2.micro`.
  * Number of `host` blocks: 1.
  * Subnet: `mysubnet`. Network settings: 

    * Availability zone: `ru-central1-a`.
    * CIDR: `10.5.0.0/24`.

  * Security group: `mymg-sg`. Rules in this security group must allow incoming public TCP connections on port `27018`.
  * Network SSD storage: `network-ssd`.
  * Storage size: 20 GB.
  * Username: `user1`.
  * Password: `user1user1`.
  * Database: `db1`.
  * Deletion protection: Enabled.

  Configuration file for a single-host cluster:

  ```hcl
  resource "yandex_mdb_mongodb_cluster" "mymg" {
    name                = "mymg"
    environment         = "PRODUCTION"
    network_id          = yandex_vpc_network.mynet.id
    security_group_ids  = [ yandex_vpc_security_group.mymg-sg.id ]
    deletion_protection = true

    cluster_config {
      version = "8.0"
    }

    resources_mongod {
      resource_preset_id = "s2.micro"
      disk_type_id       = "network-ssd"
      disk_size          = 20
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
    }
  }

  resource "yandex_mdb_mongodb_database" "db1" {
    cluster_id = yandex_mdb_mongodb_cluster.mymg.id
    name       = "db1"
  }

  resource "yandex_mdb_mongodb_user" "user1" {
    cluster_id = yandex_mdb_mongodb_cluster.mymg.id
    name       = "user1"
    password   = "user1user1"
    permission {
      database_name = "db1"
    }
    depends_on = [
      yandex_mdb_mongodb_database.db1
    ]
  }

  resource "yandex_vpc_network" "mynet" {
    name = "mynet"
  }

  resource "yandex_vpc_security_group" "mymg-sg" {
    name       = "mymg-sg"
    network_id = yandex_vpc_network.mynet.id

    ingress {
      description    = "Yandex StoreDoc"
      port           = 27018
      protocol       = "TCP"
      v4_cidr_blocks = [ "0.0.0.0/0" ]
    }
  }

  resource "yandex_vpc_subnet" "mysubnet" {
    name           = "mysubnet"
    zone           = "ru-central1-a"
    network_id     = yandex_vpc_network.mynet.id
    v4_cidr_blocks = ["10.5.0.0/24"]
  }
  ```


{% endlist %}

### Creating a sharded cluster {#creating-a-sharded-cluster}

You can create Yandex StoreDoc clusters with [standard](#std-sharding) or [advanced](#adv-sharding) sharding. To learn more about sharding types, see [Sharding management](../concepts/sharding.md#shard-management).

##### **Standard sharding** {#std-sharding}

Create a multi-host Yandex StoreDoc cluster along with its supporting network:

* One `MONGOD` host.
* Three `MONGOINFRA` hosts.

Cluster test specifications:

* Name: `mymg`.
* Environment: `PRODUCTION`.
* Deletion protection: Enabled.
* Version: `8.0`.
* Database: `db1`.
* Username: `user1`.
* Password: `user1user1`.
* `MONGOD` host class: `s2.micro`.
* `MONGOINFRA` host class: `c3-c2-m4`.
* Network SSD storage: `network-ssd`.
* Storage size: 10 GB.

Network specifications:


* Availability zone: `ru-central1-a`.
* Network: `mynet`.
* Security group: `mymg-sg` with `enp6saqnq4ie244g67sb` ID. Terraform creates a group containing a rule allowing incoming public TCP connections on port `27018`.
* Subnet: `mysubnet`. 
* CIDR: `10.5.0.0/24` (only for Terraform).


{% list tabs group=instructions %}

- CLI {#cli}

  To create a Yandex StoreDoc cluster with standard sharding, run this command:


  ```bash
  yc managed-mongodb cluster create \
     --name mymg \
     --environment production \
     --deletion-protection \
     --mongodb-version 8.0 \
     --database name=db1 \
     --user name=user1,password=user1user1 \
     --mongod-resource-preset s2.micro \
     --mongod-disk-type network-ssd \
     --mongod-disk-size 10 \
     --host type=mongod,`
       `zone-id=ru-central1-a,`
       `subnet-name=mysubnet \
     --mongoinfra-resource-preset c3-c2-m4 \
     --mongoinfra-disk-type network-ssd \
     --mongoinfra-disk-size 10 \
     --host type=mongoinfra,`
       `zone-id=ru-central1-a,`
       `subnet-name=mysubnet \
     --host type=mongoinfra,`
       `zone-id=ru-central1-a,`
       `subnet-name=mysubnet \
     --host type=mongoinfra,`
       `zone-id=ru-central1-a,`
       `subnet-name=mysubnet \
     --network-name mynet \
     --security-group-ids enp6saqnq4ie244g67sb
  ```



- Terraform {#tf}

  The configuration file for a cluster with standard sharding is as follows:

  ```hcl
  resource "yandex_mdb_mongodb_cluster" "mymg" {
    name                = "mymg"
    environment         = "PRODUCTION"
    network_id          = yandex_vpc_network.mynet.id
    security_group_ids  = [ yandex_vpc_security_group.mymg-sg.id ]
    deletion_protection = true

    cluster_config {
      version = "8.0"
    }

    resources_mongod {
      resource_preset_id = "s2.micro"
      disk_type_id       = "network-ssd"
      disk_size          = 10
    }

    resources_mongoinfra {
      resource_preset_id = "c3-c2-m4"
      disk_type_id       = "network-ssd"
      disk_size          = 10
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
      type      = "mongod"
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
      type      = "mongoinfra"
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
      type      = "mongoinfra"
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
      type      = "mongoinfra"
    }
  }

  resource "yandex_mdb_mongodb_database" "db1" {
    cluster_id = yandex_mdb_mongodb_cluster.mymg.id
    name       = "db1"
  }

  resource "yandex_mdb_mongodb_user" "user1" {
    cluster_id = yandex_mdb_mongodb_cluster.mymg.id
    name       = "user1"
    password   = "user1user1"
    permission {
      database_name = "db1"
    }
    depends_on = [
      yandex_mdb_mongodb_database.db1
    ]
  }

  resource "yandex_vpc_network" "mynet" {
    name = "mynet"
  }

  resource "yandex_vpc_security_group" "mymg-sg" {
    name       = "mymg-sg"
    network_id = yandex_vpc_network.mynet.id

    ingress {
      description    = "Yandex StoreDoc"
      port           = 27018
      protocol       = "TCP"
      v4_cidr_blocks = [ "0.0.0.0/0" ]
    }
  }

  resource "yandex_vpc_subnet" "mysubnet" {
    name           = "mysubnet"
    zone           = "ru-central1-a"
    network_id     = yandex_vpc_network.mynet.id
    v4_cidr_blocks = ["10.5.0.0/24"]
  }
  ```


{% endlist %}

##### **Advanced sharding** {#adv-sharding}

Create a multi-host Yandex StoreDoc cluster along with its supporting network:

* One `MONGOD` host.
* Two `MONGOS` hosts.
* Three `MONGOCFG` hosts.

Cluster test specifications:

* Name: `mymg`.
* Environment: `PRODUCTION`.
* Deletion protection: Enabled.
* Version: `8.0`.
* Database: `db1`.
* Username: `user1`.
* Password: `user1user1`.
* Host class: `s2.micro`.
* Network SSD storage: `network-ssd`.
* Storage size: 10 GB.

Network specifications:


* Availability zone: `ru-central1-a`.
* Network: `mynet`.
* Security group: `mymg-sg` with `enp6saqnq4ie244g67sb` ID. Terraform creates a group containing a rule allowing incoming public TCP connections on port `27018`.
* Subnet: `mysubnet`. 
* CIDR: `10.5.0.0/24` (only for Terraform).


{% list tabs group=instructions %}

- CLI {#cli}

  To create a Yandex StoreDoc cluster with advanced sharding, run this command:


  ```bash
  yc managed-mongodb cluster create \
    --name mymg \
    --environment production \
    --deletion-protection \
    --mongodb-version 8.0 \
    --database name=db1 \
    --user name=user1,password=user1user1 \
    --mongod-resource-preset s2.micro \
    --mongod-disk-type network-ssd \
    --mongod-disk-size 10 \
    --host type=mongod,`
      `zone-id=ru-central1-a,`
      `subnet-name=mysubnet \
    --mongos-resource-preset s2.micro \
    --mongos-disk-type network-ssd \
    --mongos-disk-size 10 \
    --host type=mongos,`
      `zone-id=ru-central1-a,`
      `subnet-name=mysubnet \
    --host type=mongos,`
      `zone-id=ru-central1-a,`
      `subnet-name=mysubnet \
    --mongocfg-resource-preset s2.micro \
    --mongocfg-disk-type network-ssd \
    --mongocfg-disk-size 10 \
    --host type=mongocfg,`
      `zone-id=ru-central1-a,`
      `subnet-name=mysubnet \
    --host type=mongocfg,`
      `zone-id=ru-central1-a,`
      `subnet-name=mysubnet \
    --host type=mongocfg,`
      `zone-id=ru-central1-a,`
      `subnet-name=mysubnet \
    --network-name mynet \
    --security-group-ids enp6saqnq4ie244g67sb
  ```



- Terraform {#tf}

  The configuration file for a cluster with advanced sharding is as follows:

  ```hcl
  resource "yandex_mdb_mongodb_cluster" "mymg" {
    name                = "mymg"
    environment         = "PRODUCTION"
    network_id          = yandex_vpc_network.mynet.id
    security_group_ids  = [ yandex_vpc_security_group.mymg-sg.id ]
    deletion_protection = true

    cluster_config {
      version = "8.0"
    }

    resources_mongod {
      resource_preset_id = "s2.micro"
      disk_type_id       = "network-ssd"
      disk_size          = 10
    }

    resources_mongos {
      resource_preset_id = "s2.micro"
      disk_type_id       = "network-ssd"
      disk_size          = 10
    }

    resources_mongocfg {
      resource_preset_id = "s2.micro"
      disk_type_id       = "network-ssd"
      disk_size          = 10
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
      type      = "mongod"
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
      type      = "mongos"
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
      type      = "mongos"
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
      type      = "mongocfg"
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
      type      = "mongocfg"
    }

    host {
      zone_id   = "ru-central1-a"
      subnet_id = yandex_vpc_subnet.mysubnet.id
      type      = "mongocfg"
    }
  }

  resource "yandex_mdb_mongodb_database" "db1" {
    cluster_id = yandex_mdb_mongodb_cluster.mymg.id
    name       = "db1"
  }

  resource "yandex_mdb_mongodb_user" "user1" {
    cluster_id = yandex_mdb_mongodb_cluster.mymg.id
    name       = "user1"
    password   = "user1user1"
    permission {
      database_name = "db1"
    }
    depends_on = [
      yandex_mdb_mongodb_database.db1
    ]
  }

  resource "yandex_vpc_network" "mynet" {
    name = "mynet"
  }

  resource "yandex_vpc_security_group" "mymg-sg" {
    name       = "mymg-sg"
    network_id = yandex_vpc_network.mynet.id

    ingress {
      description    = "Yandex StoreDoc"
      port           = 27018
      protocol       = "TCP"
      v4_cidr_blocks = [ "0.0.0.0/0" ]
    }
  }

  resource "yandex_vpc_subnet" "mysubnet" {
    name           = "mysubnet"
    zone           = "ru-central1-a"
    network_id     = yandex_vpc_network.mynet.id
    v4_cidr_blocks = ["10.5.0.0/24"]
  }
  ```


{% endlist %}