Cloud Key Management Service (Cloud KMS) allows you to manage encryption keys on Google Cloud. Encryption keys are created by Cloud KMS and managed by you in the same manner you would manage them on-premises.
Using Cloud KMS you can generate, use, rotate and destroy AES256 symmetric encryption keys for direct use by all of your cloud services.
In this lab, you will use Cloud KMS to create KeyRings and CryptoKeys and then use those keys with Cloud Storage to set default keys on buckets, and encrypt individual objects with a Cloud KMS key.
Additionally, you will manually perform server-side encryption with your Cloud KMS keys, and upload encrypted data to Cloud Storage.
Cloud KMS permissions will be managed with IAM, and Cloud Audit Logs will be used to view all activity for CryptoKeys and KeyRings.
Objectives
In this lab, you will learn how to do the following:
Manage keys and encrypted data using Cloud KMS.
Create KeyRings and CryptoKeys.
Set a default encryption key for a storage bucket.
Encrypt an object with a Cloud KMS key.
Rotate encryption keys.
Perform server-side encryption manually with Cloud KMS keys.
Setup and requirements
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
Note the lab's access time (for example, 1:15:00), and make sure you can finish within that time.
There is no pause feature. You can restart if needed, but you have to start at the beginning.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
Click Use another account and copy/paste credentials for this lab into the prompts.
If you use other credentials, you'll receive errors or incur charges.
Accept the terms and skip the recovery resource page.
Task 1: Configure required resources
In this task, you configure the required resources for this lab.
Create a Cloud Storage bucket
Note: A bucket must have a globally unique name. For this lab, you will use your Google Cloud project ID as part of the bucket name to help ensure it will be unique. Your Google Cloud project ID is automatically stored in a Cloud Shell environment variable named DEVSHELL_PROJECT_ID.
On the Google Cloud console title bar, click Activate Cloud Shell (). If prompted, click Continue.
Note: Before using Cloud KMS you need to enable it in your project. In the Qwiklab Google Cloud Project you have been provisioned, Cloud KMS should already have been enabled. Just to be safe, you will issue the command to enable it anyway.
Run the following command in Cloud shell to enable Cloud KMS:
gcloud services enable cloudkms.googleapis.com
Click Check my progress to verify the objective.
Create a Cloud Storage bucket
Task 2. Use Cloud KMS
In this task, you use Cloud KMS to create a keyring and a cryptokey.
Create a KeyRing and CryptoKey
Note: In order to encrypt data, you need to create a KeyRing and a CryptoKey. KeyRings are useful for grouping keys. Keys can be grouped by environment (like test, staging, and prod) or by some other conceptual grouping. For this lab, your KeyRing will be called test and your CryptoKey will be called labkey.
In Cloud Shell, run the following commands to create variables to hold the KeyRing name and CryptoKey name:
You can view the KeyRing and keys in the Google Cloud console.
In the Google Cloud console, in the Navigation menu (), click Security > Key Management.
You will see the KeyRing named lab-keyring.
Click on the KeyRing named lab-keyring to view the encryption keys named labkey-1 and labkey-2.
Click Check my progress to verify the objective.
Create a Keyring and Cryptokey
Task 3. Add a default key for a bucket
In this task, you add a default key for your bucket.
View the current default key for a bucket
Run the following command to view the default encryption key for your bucket:
gsutil kms encryption gs://$DEVSHELL_PROJECT_ID-kms
Note: The bucket should not currently have a default encryption key. This means all data in the bucket will be encrypted by Google-managed encryption keys.
Assign Cloud KMS keys to a service account
Run the following commands to give your Cloud Storage service account permission to use both of your Cloud KMS keys:
A Cloud KMS key can be set as the default key when objects are written to a bucket.
When setting the default key, the key resource must be specified in the same format as the previous command:
projects/[PROJECT_STORING_KEYS]/locations/[LOCATION]/keyRings/ [KEY_RING_NAME]/cryptoKeys/[KEY_NAME].
Run the following command to set the default key for your bucket to the first key you generated:
In the Google Cloud console, on the Navigation menu (), click Cloud Storage > Buckets, and then click on your bucket for this lab.
You will see file 1 was encrypted with a Google-managed key and file 2 was encrypted with a customer-managed key.
Click Check my progress to verify the objective.
Add a default key for the bucket
Task 4. Encrypt individual objects with a Cloud KMS key
In this task, you encrypt an individual object with a Cloud KMS key. This is useful if you want to use a different key from the default key set on the bucket, or if you don't have a default key set on the bucket. This can be done by passing the key to use in each gsutil command by using the -o flag: -o "GSUtil:encryption_key=[KEY_RESOURCE]"
Run the following command to copy file3.txt to the bucket, encrypting it with your second encryption key:
In the Google Cloud console, refresh the Bucket details screen and you will see file3.txt is also encrypted with a customer-managed key.
Identify the key used to encrypt an object
In the console, look at the encryption column. It shows you what kind of key is used: a Google-managed key or a customer-managed key. If you hover over the key, you can get details about the key. You can also get key details using gsutil, which you will do in the next step.
Run the following command to display details about an object (the -L option causes gsutil ls to display all file details):
gsutil ls -L gs://$DEVSHELL_PROJECT_ID-kms/file3.txt
In the information returned, locate the KMS key line.
This displays the encryption key being used by that file.
Run the previous command again for file1.txt and file2.txt.
Click Check my progress to verify the objective.
Encrypt individual objects with a Cloud KMS key
Task 5. Perform key rotation
In this task, you perform automatic and manual key rotation.
Note: In Cloud KMS, a key rotation is triggered by generating a new version of a key, and marking that version as the primary version. Each key has a designated primary version at any point in time, which Cloud KMS uses to encrypt data. After rotating a key, its previous key versions (which no longer are primary) are neither disabled or destroyed, and remain available for decrypting data.
Automatically rotate keys
Note: By providing a rotation schedule, Cloud KMS will automatically rotate your keys for you. A key's rotation schedule can be set using the gcloud command-line tool or via the Google Cloud console.
In the Google Cloud console, on the Navigation menu (), click Security > Key Management, and then click on the KeyRing named lab-keyring to view your encryption keys named labkey-1 and labkey-2.
Note: If you do not see lab-keyring, click Refresh.
Click on the key named labkey-1 to view all versions.
Currently you only have one version.
Click the Edit Rotation Period button.
Set the rotation period dropdown to 30 days.
Notice that the rotation period can also be set to a Custom period that allows you to specify any desired period.
Click Save.
The console now displays the next rotation date for this key.
Manually rotate keys
Note: Manually rotating keys can also be done with the gcloud command-line tool or via the Google Cloud console.
In the console, go back to the KeyRing named lab-keyring and click on the key named labkey-2 to view all versions.
Click the Rotate Key button and then click Rotate.
You now have two versions of this key, version 2 is the primary one.
Note: Using the key rotation commands above, key rotation does NOT re-encrypt already encrypted data with the newly generated key version. If you suspect unauthorized use of a key, you should re-encrypt the data protected by that key and then disable or schedule destruction of the prior key version.
Destroy old keys
Note: If you destroy a key that encrypts existing objects, you will be unable to recover that data, but you will continue to be charged for storage of your objects until you delete them.
In this part, you will not actually destroy a key, but you will investigate the process for doing so.
From the labkey-2 versions screen, click the three vertical dots on the far right of the line for version 1 of the key and select Destroy.
Read the message in the Schedule key version 1 for destruction and click Cancel when done.
You have successfully used Cloud KMS keys to encrypt data in Cloud Storage.
Click Check my progress to verify the objective.
Key Rotation
Bonus task. Encrypt data with the REST API
The Cloud KMS service also provides a REST API to perform encryption and decryption. The content to be encrypted is specified as part of a JSON document in the REST request, and this content must be encoded using Base64 encoding. This JSON document has the following form:
{"plaintext":"Base64 encoded data to encrypt"}.
In this bonus section to the lab, you will manually invoke the REST api using curl commands to demonstrate the capability of the API.
This section assumes you still have the Cloud Shell session open and the following environment variables are defined:
KEYRING_NAME
CRYPTOKEY_1_NAME
CRYPTOKEY_2_NAME.
If these variables are no longer defined, go back to earlier in the lab and run the commands to create these variables.
Run the following command to encode some sample text as base64 and store it in a variable named PLAIN_TEXT:
PLAIN_TEXT=$(echo -n "Some text to be encrypted" | base64)
Echo the PLAIN_TEXT variable to verify the text was encoded:
echo $PLAIN_TEXT
You should see the base64-encoded text.
Use the REST API to encrypt the encoded text by calling the encrypt method of your key.
Supply the base64-encoded content in the plaintext field of the JSON for your request:
The response will be a JSON payload containing the encrypted text in the ciphertext field.
Note: The encrypted text can easily be extracted from the JSON response, and saved to a file by using the command-line utility jq. The response from the previous call can be piped into jq, which can parse out the ciphertext property and save to data1.encrypted.
Run the following command that repeats the encryption, but this time parses out the ciphertext property and saves it to the data1.encrypted file:
View the contents of the data1.encrypted file with the following command:
more data1.encrypted
Note: The encrypted text can be decrypted by calling the decrypt method of your key. You must use the same key that was used to encrypt the content.
Run the following command to decrypt the contents in the data1.encrypted file and save it into the file named data1.decrypted:
View the contents of the data1.decrypted file with the following command:
more data1.decrypted
You have successfully used Cloud KMS keys.
Congratulations!
In this lab, you have done the following:
Managed keys and encrypted data using Cloud KMS.
Created KeyRings and CryptoKeys.
Set a default encryption key for a storage bucket.
Encrypted an object with a Cloud KMS key.
Rotated encryption keys.
Manually performed server-side encryption with Cloud KMS keys.
End your lab
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
1 star = Very dissatisfied
2 stars = Dissatisfied
3 stars = Neutral
4 stars = Satisfied
5 stars = Very satisfied
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2025 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
Labs create a Google Cloud project and resources for a fixed time
Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
On the top left of your screen, click Start lab to begin
Use private browsing
Copy the provided Username and Password for the lab
Click Open console in private mode
Sign in to the Console
Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
Accept the terms, and skip the recovery resource page
Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one
Use private browsing to run the lab
Use an Incognito or private browser window to run this lab. This
prevents any conflicts between your personal account and the Student
account, which may cause extra charges incurred to your personal account.
Using Customer-Managed Encryption Keys with Cloud Storage and Cloud KMS