01 Before you can create your own Customer-Managed Encryption Key (CMEK), you have to provision a key ring. A Cloud KMS key ring is a grouping of cryptographic keys made available for organizational purposes in a specific Google Cloud location. Run kms keyrings create command (Windows/macOS/Linux) to create a new Cloud KMS key ring. For secrets configured with an automatic replication policy, your key ring key must be located in the global KMS region (multi-region):
gcloud kms keyrings create cc-project5-key-ring
--location=global
--project=cc-project5-123123
--format="table(name)"
02 The command output should return the resource name of the newly created key ring:
NAME: projects/cc-project5-123123/locations/global/keyRings/cc-project5-key-ring
03 Run kms keys create command (Windows/macOS/Linux) to create a new Customer-Managed Encryption Key (CMEK) within the Cloud KMS key ring created in the previous steps. For secrets configured with an automatic replication policy, your new encryption key must be created in the global KMS region:
gcloud kms keys create cc-project5-cmek
--location=global
--keyring=cc-project5-key-ring
--purpose=encryption
--protection-level=software
--rotation-period=90d
--next-rotation-time=2025-09-15T10:00:00.0000Z
--format="table(name)"
04 The command output should return the full resource name of the new Customer-Managed Encryption Key:
NAME: projects/cc-project5-123123/locations/global/keyRings/cc-project5-key-ring/cryptoKeys/cc-project5-cmek
05 Run kms keys add-iam-policy-binding command (Windows/macOS/Linux) to add the required IAM policy binding to your new CMEK. This assigns the Cloud KMS CryptoKey Encrypter/Decrypter role to the associated service account (i.e., service-\<project-number\>@gcp-sa-secretmanager.iam.gserviceaccount.com). Replace \<project-number\> with your Google Cloud Platform (GCP) project number:
gcloud kms keys add-iam-policy-binding cc-project5-cmek
--location "global"
--member "serviceAccount:service-<project-number>@gcp-sa-secretmanager.iam.gserviceaccount.com"
--role "roles/cloudkms.cryptoKeyEncrypterDecrypter"
--project "cc-project5-123123"
06 The command output should return the updated IAM policy (YAML format):
Updated IAM policy for key [cc-project5-cmek].
bindings:
- members:
- serviceAccount:service-<project-number>@gcp-sa-secretmanager.iam.gserviceaccount.com
role: roles/cloudkms.cryptoKeyEncrypterDecrypter
etag: abcdabcdabcd
version: 1
07 To enable encryption with Cloud KMS Customer-Managed Encryption Keys (CMEKs) for your Secret Manager secrets you have to re-create your secrets using the right encryption configuration. Run secrets versions list command (Windows/macOS/Linux) with the name of the secret that you want to re-create as the identifier parameter, to list all versions available for the selected secret:
gcloud secrets versions list cc-project5-api-key
--format="table(name)"
08 The command output should return the number of each version created for the selected secret:
09 Run secrets versions access command (Windows/macOS/Linux) with the number of the secret version that you want to access as the identifier parameter, to describe the payload (i.e., secret data) of the selected secret version:
gcloud secrets versions access 1
--secret=cc-project5-api-key
--format="value(payload.data)"
10 The command output should return the secret data (payload). Save this secret data to a text file named cc-secret-data.txt:
11 Run secrets create command (Windows/macOS/Linux) to create a new Secret Manager secret using the information (payload) returned in the previous step as secret data (i.e., cc-secret-data.txt). Use the --kms-key-name command parameter to specify the Cloud KMS Customer-Managed Encryption Key (CMEK) that you want to use for secret data encryption:
gcloud secrets create cc-project5-new-api-key
--project "cc-project5-123123"
--replication-policy "automatic"
--kms-key-name "projects/cc-project5-123123/locations/global/keyRings/cc-project5-key-ring/cryptoKeys/cc-project5-cmek"
--data-file=cc-secret-data.txt
12 The command output should return the name of the new Secret Manager secret. From now on, each time a secret version is created in this secret, the secret version's payload is automatically encrypted using the specified CMEK before being written to persistent storage, as long as the service account has access to the CMEK resource:
Created version [1] of the secret [cc-project5-new-api-key].
13 Repeat steps no. 7 – 12 for each Secret Manager secret that you want to configure, available in the selected Google Cloud Platform (GCP) project.
14 Repeat steps no. 1 – 13 for each GCP project deployed in your Google Cloud account.