[Yandex Cloud documentation](../../../index.md) > [Yandex Compute Cloud](../../index.md) > [Step-by-step guides](../index.md) > Disk snapshots > Encrypting an image

# Encrypting an image

If you deactivate the key used to encrypt a disk, image, or snapshot, access to the data will be suspended until you reactivate the key.

{% note alert %}

If you destroy the key or its [version](../../../kms/concepts/version.md) used to encrypt a disk, image, or snapshot, you will irrevocably lose access to the data. For details, see [Destroying key version](../../../kms/concepts/version.md#version-distruct).

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

  1. [Create](../../../kms/operations/key.md#create) a Yandex Key Management Service encryption key. For more, see [Encryption in Compute Cloud](../../concepts/encryption.md).
  1. Create an encrypted disk from the snapshot you want to encrypt:

      1. In the [management console](https://console.yandex.cloud), select the folder with your source snapshot.
      1. Navigate to **Compute Cloud**.
      1. In the left-hand panel, select ![image](../../../_assets/console-icons/hard-drive.svg) **Disks**.
      1. Click **Create disk**.
      1. Enter a name for the disk.

          * Length: between 3 and 63 characters.
          * It can only contain lowercase Latin letters, numbers, and hyphens.
          * It must start with a letter and cannot end with a hyphen.

      1. Set the disk parameters, such as [disk type](../../concepts/disk.md#disks_types), [block size](../../concepts/disk.md#maximum-disk-size), and [disk size](../../concepts/disk.md#maximum-disk-size).
      1. In the **Contents** field, select `Snapshot` and then select the snapshot you created earlier from the list below. Use the filter to find the snapshot.
      1. Under **Encryption**, enable **Encrypted disk** and select the [key](../../../kms/concepts/key.md) you created earlier in the **KMS key** field.

      1. Click **Create disk**.

      Once created, the disk will get the `Creating` status. Wait until the disk status changes to `Ready` before using it.

  1. [Create](../disk-control/create-snapshot.md) a snapshot from the encrypted disk created earlier.
  1. [Delete](../disk-control/delete.md) the encrypted disk.
  1. [Delete](delete.md) the source snapshot.

- 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. Create a Yandex Key Management Service encryption key:

      ```bash
      yc kms symmetric-key create \
        --name <key_name> \
        --default-algorithm aes-256 \
        --rotation-period 24h \
        --deletion-protection
      ```

      Where `--name` is the name of the new Key Management Service key.

      Result:

      ```text
      id: abj73fd9mekk********
      folder_id: b1geoelk7fld********
      created_at: "2025-05-20T17:27:35Z"
      name: my-key1
      status: ACTIVE
      primary_version:
        id: abjdno4pqi67********
        key_id: abj73fd9mekk********
        status: ACTIVE
        algorithm: AES_256
        created_at: "2025-05-20T17:27:35Z"
        primary: true
      default_algorithm: AES_256
      rotation_period: 86400s
      deletion_protection: true
      ```

  1. Get a list of all snapshots in the default folder:

      ```bash
      yc compute snapshot list
      ```

      Result:
      ```text
      +----------------------+-----------------------+----------------------+----------+
      |          ID          |         NAME          |     PRODUCT IDS      |  STATUS  |
      +----------------------+-----------------------+----------------------+----------+
      | fd823vsvcmop******** | snap-ubuntu-24-04-lts | f2etq3erab3o******** | READY    |
      | fd8p8l3asgud******** | snap-debian-2025      | f2etq3erab3o******** | READY    |
      +----------------------+-----------------------+----------------------+----------+
      ```

  1. Create an encrypted disk from the snapshot you want to encrypt:

      ```bash
      yc compute disk create <encrypted_disk_name> \
        --source-snapshot-name <snapshot_name> \
        --kms-key-name <key_name>
      ```

      Where:
      * `--source-snapshot-name`: Name of the snapshot used to create the encrypted disk.
      * `--kms-key-name`: Encryption key name.

      Result:

      ```text
      done (53s)
      id: fhmihpagi991********
      folder_id: b1geoelk7fld********
      created_at: "2025-05-20T17:39:01Z"
      name: fromcliencrypted
      type_id: network-hdd
      zone_id: ru-central1-a
      size: "21474836480"
      block_size: "4096"
      status: READY
      source_snapshot_id: fd8lb5jnr2m2********
      disk_placement_policy: {}
      hardware_generation:
        legacy_features:
          pci_topology: PCI_TOPOLOGY_V1
      kms_key:
        key_id: abj73fd9mekk********
        version_id: abjdno4pqi67********
      ```

      Once created, the disk will get the `Creating` status. Wait until the disk status changes to `Ready` before using it.

  1. Get a list of all disks in the default folder:

      ```bash
      yc compute disk list
      ```

      Result:
      ```text
      +----------------------+--------------+-------------+---------------+--------+----------------------+-------------------------+
      |          ID          |     NAME     |    SIZE     |     ZONE      | STATUS |     INSTANCE IDS     |       DESCRIPTION       |
      +----------------------+--------------+-------------+---------------+--------+----------------------+-------------------------+
      | a7lqgbt0bb9s******** | first-disk   | 20401094656 | ru-central1-a | READY  | a7lcvu28njbh******** |                         |
      | a7lv5j5hm1p1******** | second-disk  | 21474836480 | ru-central1-a | READY  |                      |                         |
      +----------------------+--------------+-------------+---------------+--------+----------------------+-------------------------+
      ```

  1. Create a snapshot from the encrypted disk you created earlier:
  
      ```bash
      yc compute snapshot create \
        --name <name_of_new_snapshot> \
        --disk-name <encrypted_disk_name>
      ```

      Result:

      ```text
      id: fhmu3pfcpicd********
      description: Create snapshot
      created_at: "2025-06-02T16:14:07.523160478Z"
      created_by: ajevfb0tjfts********
      modified_at: "2025-06-02T16:14:07.523160478Z"
      metadata:
        '@type': type.googleapis.com/yandex.cloud.compute.v1.CreateSnapshotMetadata
        snapshot_id: fd8pb5en4j9m********
        disk_id: fhmqgef7vh76********
      ```

  1. Delete the encrypted disk:
      ```bash
      yc compute disk delete <encrypted_disk_name>
      ```

      Result:

      ```text
      done (5s)
      ```

  1. Delete the source snapshot:
      ```bash
      yc compute snapshot delete <unencrypted_snapshot_name>
      ```

      Result:

      ```text
      done (7s)
      ```

- 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 encrypt a disk using Terraform:
  1. In the Terraform configuration file, describe the resources you want to create:

      ```hcl
      # Creating a Yandex Key Management Service key

      resource "yandex_kms_symmetric_key" "my-key" {
        name                = "encrypt-key"
        default_algorithm   = "AES_256"
        rotation_period     = "8760h"
        deletion_protection = true
        lifecycle {
          prevent_destroy = true
        }
      }

      # Creating an encrypted disk

      resource "yandex_compute_disk" "encrypted-disk" {
        name        = "new-encrypted-disk"
        type        = "network-hdd"
        zone        = "ru-central1-a"
        size        = 20
        block_size  = 4096
        snapshot_id = "<unencrypted_snapshot_ID>"
        kms_key_id  = yandex_kms_symmetric_key.my-key.id
      }

      # Creating an encrypted snapshot

      resource "yandex_compute_snapshot" "encrypted-snapshot" {
        name           = "<encrypted_snapshot_name>"
        source_disk_id = yandex_compute_disk.encrypted-disk.id
        depends_on     = [yandex_compute_disk.encrypted-disk]
      }
      ```

      Where:
      * `snapshot_id`: Unencrypted snapshot ID.
      * `name`: Name of the encrypted snapshot you are creating.

      For more information about `yandex_compute_snapshot` properties, see [this provider guide](../../../terraform/resources/compute_snapshot.md).

  1. Create the resources:

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

      Once created, the disk will get the `Creating` status. Wait until the disk status changes to `Ready` before using it.

  1. [Delete](../disk-control/delete.md) the encrypted disk.
  1. [Delete](delete.md) the source snapshot.


- API {#api}

  1. Create a Yandex Key Management Service encryption key using the [create](../../../kms/api-ref/SymmetricKey/create.md) REST API method for the [SymmetricKey](../../../kms/api-ref/SymmetricKey/index.md) resource or the [SymmetricKeyService/Create](../../../kms/api-ref/grpc/SymmetricKey/create.md) gRPC API call.

  1. Create an encrypted disk from a snapshot using the [create](../../api-ref/Disk/create.md) REST API method for the [Disk](../../api-ref/Disk/index.md) resource or the [DiskService/Create](../../api-ref/grpc/Disk/create.md) gRPC API call.

      To request a list of available snapshots, use the [list](../../api-ref/Snapshot/list.md) REST API method or the [SnapshotService/List](../../api-ref/grpc/Snapshot/list.md) gRPC API call.

      Once created, the disk will get the `Creating` status. Wait until the disk status changes to `Ready` before using it.

  1. Create a snapshot for or the encrypted disk using the [create](../../api-ref/Snapshot/create.md) REST API method for the [Snapshot](../../api-ref/Snapshot/index.md) resource or the [SnapshotService/Create](../../api-ref/grpc/Snapshot/create.md) gRPC API call.

  1. Delete the encrypted disk using the [delete](../../api-ref/Disk/delete.md) REST API method for the [Disk](../../api-ref/Disk/index.md) resource or the [DiskService/Delete](../../api-ref/grpc/Disk/delete.md) gRPC API call.

  1. Delete the source snapshot using the [delete](../../api-ref/Snapshot/delete.md) REST API method for the [Snapshot](../../api-ref/Snapshot/index.md) resource or the [SnapshotService/Delete](../../api-ref/grpc/Snapshot/delete.md) gRPC API call.

{% endlist %}


#### Useful links {#see-also}

* [Encryption in Compute Cloud](../../concepts/encryption.md)
* [Encrypting a disk](../disk-control/disk-encrypt.md)
* [Encrypting an image](../image-control/encrypt.md)