[Yandex Cloud documentation](../../../index.md) > [Yandex Compute Cloud](../../index.md) > [Step-by-step guides](../index.md) > Placement groups > Creating an instance group in a placement group

# Creating an instance group in a placement group


You can create an [instance group](../../concepts/instance-groups/index.md) in a [placement group](../../concepts/placement-groups.md).

In Instance Groups, all operations are performed under a service account. If you don't have a service account, [create one](../../../iam/operations/sa/create.md).

To be able to create, update, and delete VMs in the group, [assign](../../../iam/operations/sa/assign-role-for-sa.md) the [compute.editor](../../security/index.md#compute-editor) role to the service account.

{% list tabs group=instructions %}

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

  1. See the description of the CLI command for creating a placement group:

      ```bash
      yc compute placement-group create --help
      ```

  1. Create a placement group in the default folder with one of the following placement strategies:
  
     * [Spread placement](../../concepts/placement-groups.md#spread):
     
       ```bash
       yc compute placement-group create \
         --spread-strategy \
         --name <placement_group_name>
       ```
     
       Where:
     
       * `--spread-strategy`: Shows that spread placement is selected.
       * `--name`: Placement group name.
     
     * [Partition placement](../../concepts/placement-groups.md#partition):
     
       ```bash
       yc compute placement-group create \
         --partitions <number_of_partitions> \
         --name <placement_group_name>
       ```
     
       Where:
     
       * `--partitions`: Shows that partition placement is selected, indicating the number of partitions from `1` to `5`.
       * `--name`: Placement group name.
  
  1. See the description of the CLI command for creating an instance group:

     ```bash
     yc compute instance-group create --help
     ```

  1. Make sure the [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) contains at least one [network](../../../vpc/concepts/network.md#network):

      ```bash
      yc vpc network list
      ```

     If there are no networks, [create one](../../../vpc/operations/network-create.md).

  1. Select one of the Yandex Cloud Marketplace public images, e.g., [CentOS 7](https://yandex.cloud/en/marketplace/products/yc/centos-7).

     To get a list of available images using the CLI, run this command:
     
     ```bash
     yc compute image list --folder-id standard-images
     ```
     
     Result:
     
     ```text
     +----------------------+-------------------------------------+--------------------------+----------------------+--------+
     |          ID          |                NAME                 |          FAMILY          |     PRODUCT IDS      | STATUS |
     +----------------------+-------------------------------------+--------------------------+----------------------+--------+
     ...
     | fdvk34al8k5n******** | centos-7-1549279494                 | centos-7                 | dqni65lfhvv2******** | READY  |
     | fdv7ooobjfl3******** | windows-2016-gvlk-1548913814        | windows-2016-gvlk        | dqnnc72gj2is******** | READY  |
     | fdv4f5kv5cvf******** | ubuntu-1604-lts-1549457823          | ubuntu-1604-lts          | dqnnb6dc7640******** | READY  |
     ...
     +----------------------+-------------------------------------+--------------------------+----------------------+--------+
     ```
     
     Where:
     
     * `ID`: Image ID.
     * `NAME`: Image name.
     * `FAMILY`: ID of the [image family](../../concepts/image.md#family) the image belongs to.
     * `PRODUCT IDS`: IDs of Yandex Cloud Marketplace [products](../../../marketplace/concepts/product.md) associated with the image.
     * `STATUS`: Current status of the image. It may take one of the following values:
     
         * `STATUS_UNSPECIFIED`: Image status is not defined.
         * `CREATING`: Image is being created.
         * `READY`: Image is ready to use.
         * `ERROR`: You cannot use the image due to an issue.
         * `DELETING`: Image is being deleted.

  1. Create a YAML file with any name, e.g., `specification.yaml`.

  1. Define the following in the file you created:

      * General information about the instance group:

        ```yaml
        name: first-fixed-group
        service_account_id: <service_account_ID>
        description: "This instance group was created from YAML config."
        ```

        Where:

        * `name`: Instance group name. The name must be unique within the folder. It can only contain lowercase Latin letters, numbers, and hyphens. The first character must be a letter. The last character cannot be a hyphen. The name can be up to 63 characters long.
        * `service_account_id`: [Service account](../../../iam/concepts/users/service-accounts.md) ID.

          To be able to create, update, and delete VMs in the group, [assign](../../../iam/operations/sa/assign-role-for-sa.md) the [compute.editor](../../security/index.md#compute-editor) role to the service account.

          You cannot delete a service account while it is linked to an instance group.

        * `description`: Instance group description.
     
      * [Instance template](../../concepts/instance-groups/instance-template.md), such as the following:

        ```yaml
        instance_template:
          platform_id: standard-v3
          resources_spec:
            memory: 2g
            cores: 2
          boot_disk_spec:
            mode: READ_WRITE
            disk_spec:
              image_id: fdvk34al8k5n********
              type_id: network-hdd
              size: 32g
          network_interface_specs:
            - network_id: c64mknqgnd8a********
              primary_v4_address_spec: {}
              security_group_ids:
                - enps0ar5s3ti********
          scheduling_policy:
            preemptible: false
          placement_policy:
            placement_group_id: fdvte50kv3nc********
        ```

        By default, the disk size is specified in bytes. You can specify a different unit of measurement using the applicable suffix.
        
        | Suffix | Prefix and multiplier | Example |
        | ----- | ----- | ----- |
        | `k` | kilo- (2^10^) | `640k` = 640 × 2^10^ = `655360` |
        | `m` | mega- (2^20^) | `48m` = 48 × 2^20^ = `50331648` |
        | `g` | giga- (2^30^) | `10g` = 10 × 2^30^ = `10737418240` |
        | `t` | tera- (2^40^) | `4t` = 4 × 2^40^ = `4398046511104` |
        | `p` | peta- (2^50^) | `2p` = 2 × 2^50^ = `2251799813685248` |

        Where:

        * `platform_id`: [Platform](../../concepts/vm-platforms.md) ID.
        * `memory`: Amount of RAM.
        * `cores`: Number of vCPUs.
        * `mode`: [Disk](../../concepts/disk.md) access mode.
          * `READ_ONLY`: Read-only access.
          * `READ_WRITE`: Read/write access.
        * `image_id`: Public image ID. You can view image IDs in the [management console](https://console.yandex.cloud) when creating an instance or in [Cloud Marketplace](https://yandex.cloud/en/marketplace) on the image page under **Product IDs**.
        * `type_id`: Disk type.
        * `size`: Disk size.
        * `network_id`: `default-net` network ID.
        * `primary_v4_address_spec`: IPv4 specification. You can allow public access to the group instances by specifying the IP version for the [public IP address](../../../vpc/concepts/address.md#public-addresses). For more information, see [Template description in a YAML file](../../concepts/instance-groups/instance-template.md#instance-template).
        * `security_group_ids`: List of [security group](../../../vpc/concepts/security-groups.md) IDs.
        * `scheduling_policy`: Scheduling policy configuration.
        * `preemptible`: Flag for creating [preemptible instances](../../concepts/preemptible-vm.md).
          * `true`: Create a preemptible instance.
          * `false` (default): Create a regular instance.

          When creating a preemptible instance group, keep in mind that the VM instances will terminate after 24 hours of continuous operation or earlier. Instance Groups may not be able to restart them immediately due to insufficient resources. This may occur in the event of a sharp increase in the use of Yandex Cloud computing resources.

        * `placement_policy`: [Instance placement group](../../concepts/placement-groups.md) parameters:
          * `placement_group_id`: Placement group ID.
      * [Policies](../../concepts/instance-groups/policies/index.md):

        ```yaml
        deploy_policy:
          max_unavailable: 1
          max_expansion: 0
        scale_policy:
          fixed_scale:
            size: 3
        allocation_policy:
          zones:
            - zone_id: ru-central1-a
              instance_tags_pool:
              - first
              - second
              - third
        ```

        Where:

        * `deploy_policy`: Instance [deployment policy](../../concepts/instance-groups/policies/deploy-policy.md) for the group.
        * `scale_policy`: Instance [scaling policy](../../concepts/instance-groups/policies/scale-policy.md) for the group.
        * `allocation_policy`: [Policy for allocating](../../concepts/instance-groups/policies/allocation-policy.md) instances across [availability zones](../../../overview/concepts/geo-scope.md).

      Full code for the `specification.yaml` file:

      ```yaml
      name: first-fixed-group
      service_account_id: <service_account_ID>
      description: "This instance group was created from YAML config."
      instance_template:
        platform_id: standard-v3
        resources_spec:
          memory: 2g
          cores: 2
        boot_disk_spec:
          mode: READ_WRITE
          disk_spec:
            image_id: fdvk34al8k5n********
            type_id: network-hdd
            size: 32g
        network_interface_specs:
          - network_id: c64mknqgnd8a********
            primary_v4_address_spec: {}
            security_group_ids:
              - enps0ar5s3ti********
         scheduling_policy:
           preemptible: false
         placement_policy:
           placement_group_id: fdvte50kv3nc********
      deploy_policy:
        max_unavailable: 1
        max_expansion: 0
      scale_policy:
        fixed_scale:
          size: 3
      allocation_policy:
        zones:
          - zone_id: ru-central1-a
            instance_tags_pool:
            - first
            - second
            - third
      ```

  1. Create an instance group in the default folder:

     ```bash
     yc compute instance-group create --file specification.yaml
     ```

     This command will create a group of three same-type instances with the following configuration:
     * Name: `first-fixed-group`.
     * OS: CentOS 7.
     * Network: `default-net`.
     * Placement group: `my-group`.
     * Availability zone: `ru-central1-a`.
     * vCPUs: 2; RAM: 2 GB.
     * Network HDD: 32 GB.

- API {#api}

  Use the [create](../../instancegroup/api-ref/InstanceGroup/create.md) REST API method for the [InstanceGroup](../../instancegroup/api-ref/InstanceGroup/index.md) resource or the [InstanceGroupService/Create](../../instancegroup/api-ref/grpc/InstanceGroup/create.md) gRPC API call.

{% endlist %}