Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Key Management Service
  • Getting started
  • Step-by-step instructions
    • All instructions
    • Keys
    • Key version
    • Data encryption
  • Concepts
    • Overview
    • Keys
    • Key version
    • Encryption
    • Envelope encryption
    • Key consistency
    • Quotas and limits
  • Use cases
    • All use cases
    • Data encryption
      • Which encryption method should I choose?
      • Encrypting data using the Yandex.Cloud CLI and API
      • Encrypting data using the Yandex.Cloud SDK
      • Encrypting data using the AWS Encryption SDK
      • Encrypting data using Google Tink
    • Encrypting secrets in Managed Service for Kubernetes
    • KMS key management with Hashicorp Terraform
    • Encrypting secrets in Hashicorp Terraform
  • Access management
  • Pricing policy
  • API reference
    • Authentication in the API
    • gRPC
      • Overview
      • SymmetricCryptoService
      • SymmetricKeyService
      • OperationService
    • REST
      • Overview
      • SymmetricCrypto
        • Overview
        • decrypt
        • encrypt
        • generateDataKey
        • reEncrypt
      • SymmetricKey
        • Overview
        • cancelVersionDestruction
        • create
        • delete
        • get
        • list
        • listAccessBindings
        • listOperations
        • listVersions
        • rotate
        • scheduleVersionDestruction
        • setAccessBindings
        • setPrimaryVersion
        • update
        • updateAccessBindings
  • Questions and answers
  1. Use cases
  2. KMS key management with Hashicorp Terraform

Managing KMS keys with Hashicorp Terraform

  • Adding keys
  • Managing key access
  • See also

The Terraform provider for Yandex.Cloud supports the use of KMS keys.

Adding keys

You can add a KMS key using the yandex_kms_symmetric_key block:

resource "yandex_kms_symmetric_key" "kms-key" {
  lifecycle {
    prevent_destroy = true
  }

  name              = "example-symetric-key"
  description       = "description for key"
  default_algorithm = "AES_256"
  rotation_period   = "8760h" // equal to 1 year
}

Warning

Deleting a KMS key destroys all data encrypted with that key: the data becomes unrecoverable after the key is deleted. The lifecycle block is necessary to prevent users from destroying keys (with the terraform destroy command, for example).

Managing key access

To manage access to keys in Terraform, assign the necessary roles for the folder that contains the key.

For example, assign a role for the service account, which grants the permission to encrypt and decrypt data with keys from a specific folder:

data "yandex_iam_policy" "encrypter_decrypter_iam_policy" {
  binding {
    role = "kms.keys.encrypterDecrypter"

    members = [
      "serviceAccount:<service account ID>",
    ]
  }
}

resource "yandex_resourcemanager_folder_iam_policy" "folder_iam_policy" {
  folder_id   = "<folder ID>"
  policy_data = "${data.yandex_iam_policy.encrypter_decrypter_iam_policy.policy_data}"
}

See also

  • Getting started with Terraform in Yandex.Cloud.
  • Access management.
  • Yandex.Cloud provider documentation.
In this article:
  • Adding keys
  • Managing key access
  • See also
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC