Bird
Raised Fist0
GCPcloud~5 mins

Cloud KMS for key management in GCP - Commands & Configuration

Choose your learning style10 modes available
Introduction
Managing secret keys safely is important to protect your data. Cloud KMS helps you create, store, and control encryption keys in a secure way so only authorized users and services can use them.
When you want to encrypt sensitive data like passwords or API keys before storing them.
When you need to control who can use or manage encryption keys in your cloud projects.
When you want to rotate encryption keys regularly without downtime.
When you want to audit key usage to see who accessed or used your keys.
When you want to integrate encryption keys with other Google Cloud services securely.
Config File - main.tf
main.tf
provider "google" {
  project = "example-project"
  region  = "us-east1"
}

resource "google_kms_key_ring" "my_key_ring" {
  name     = "my-key-ring"
  location = "us-east1"
}

resource "google_kms_crypto_key" "my_crypto_key" {
  name            = "my-crypto-key"
  key_ring        = google_kms_key_ring.my_key_ring.id
  rotation_period = "2592000s" # 30 days
  lifecycle {
    prevent_destroy = true
  }
}

This Terraform file creates a key ring named my-key-ring in the us-east1 region.

It then creates a crypto key named my-crypto-key inside that key ring.

The key is set to rotate every 30 days automatically.

The prevent_destroy setting stops accidental deletion of the key.

Commands
This command creates a new key ring named 'my-key-ring' in the 'us-east1' region. Key rings group your keys and help organize them.
Terminal
gcloud kms keyrings create my-key-ring --location us-east1
Expected OutputExpected
Created keyring [my-key-ring].
→
--location - Specifies the region where the key ring is created.
This command creates a new crypto key named 'my-crypto-key' inside the 'my-key-ring' key ring. It is used for encryption and set to rotate every 30 days (2592000 seconds).
Terminal
gcloud kms keys create my-crypto-key --location us-east1 --keyring my-key-ring --purpose encryption --rotation-period 2592000s
Expected OutputExpected
Created key [my-crypto-key].
→
--purpose - Defines the key's use, here for encryption.
→
--rotation-period - Sets how often the key rotates automatically.
This command lists all crypto keys inside the 'my-key-ring' key ring to verify the key was created.
Terminal
gcloud kms keys list --location us-east1 --keyring my-key-ring
Expected OutputExpected
NAME PURPOSE PRIMARY_VERSION_STATE my-crypto-key ENCRYPT_DECRYPT ENABLED
→
--location - Specifies the region of the key ring.
→
--keyring - Specifies the key ring to list keys from.
This command shows the access policy for the 'my-crypto-key' key. It helps you see who can use or manage the key.
Terminal
gcloud kms keys get-iam-policy my-crypto-key --location us-east1 --keyring my-key-ring
Expected OutputExpected
bindings: - members: - user:example-user@gmail.com role: roles/cloudkms.cryptoKeyEncrypterDecrypter etag: BwW8xYzXxXk= version: 1
→
--location - Specifies the region of the key ring.
→
--keyring - Specifies the key ring containing the key.
Key Concept

If you remember nothing else from this pattern, remember: Cloud KMS lets you safely create and control encryption keys so your data stays protected and access is controlled.

Common Mistakes
Trying to create a crypto key without first creating a key ring.
Keys must belong to a key ring; without it, the command fails.
Always create the key ring first using 'gcloud kms keyrings create' before creating keys inside it.
Not specifying the correct location flag when creating or listing keys.
Keys and key rings are regional; missing or wrong location causes errors or empty results.
Always include the '--location' flag with the correct region for all key-related commands.
Not setting a rotation period for keys that need regular rotation.
Without rotation, keys stay the same and increase risk if compromised.
Use the '--rotation-period' flag to set automatic key rotation for better security.
Summary
Create a key ring to organize your encryption keys.
Create a crypto key inside the key ring with a purpose and optional rotation.
List keys to verify creation and check access policies to control usage.

Practice

(1/5)
1. What is the main purpose of Google Cloud KMS (Key Management Service)?
easy
A. To securely store and manage encryption keys
B. To host virtual machines
C. To manage user identities
D. To monitor network traffic

Solution

  1. Step 1: Understand the role of Cloud KMS

    Cloud KMS is designed to store and manage encryption keys securely in the cloud.
  2. Step 2: Compare with other services

    Hosting virtual machines, managing identities, and monitoring traffic are handled by other GCP services, not KMS.
  3. Final Answer:

    To securely store and manage encryption keys -> Option A
  4. Quick Check:

    Cloud KMS = key management [OK]
Hint: Cloud KMS is about keys, not machines or users [OK]
Common Mistakes:
  • Confusing KMS with Compute Engine
  • Thinking KMS manages users
  • Assuming KMS monitors networks
2. Which of the following is the correct command to create a key ring named my-key-ring in location us-central1 using gcloud CLI?
easy
A. gcloud kms keyrings create my-key-ring --location=us-central1
B. gcloud kms create keyring my-key-ring --region=us-central1
C. gcloud kms keyrings new my-key-ring --location=us-central1
D. gcloud kms keyring create my-key-ring --zone=us-central1

Solution

  1. Step 1: Recall gcloud syntax for key ring creation

    The correct command uses 'gcloud kms keyrings create' with the --location flag.
  2. Step 2: Verify option details

    gcloud kms keyrings create my-key-ring --location=us-central1 matches the correct syntax and uses the correct flag '--location=us-central1'. Other options use wrong verbs or flags.
  3. Final Answer:

    gcloud kms keyrings create my-key-ring --location=us-central1 -> Option A
  4. Quick Check:

    Correct gcloud syntax = gcloud kms keyrings create my-key-ring --location=us-central1 [OK]
Hint: Use 'keyrings create' with --location flag for key rings [OK]
Common Mistakes:
  • Using --region instead of --location
  • Wrong command order like 'create keyring'
  • Using --zone which is for compute resources
3. Given the following Python snippet using Google Cloud KMS client, what will be the output if the key does not exist?
from google.cloud import kms_v1
client = kms_v1.KeyManagementServiceClient()
key_name = client.crypto_key_path('my-project', 'us-central1', 'my-key-ring', 'nonexistent-key')
response = client.get_crypto_key(request={'name': key_name})
print(response.name)
medium
A. Prints the key name string
B. Raises a NotFound exception
C. Prints None
D. Returns an empty response object

Solution

  1. Step 1: Understand get_crypto_key behavior

    When requesting a key that does not exist, the client raises a NotFound error.
  2. Step 2: Check the code flow

    The code calls get_crypto_key with a nonexistent key name, so it will not print but raise an exception.
  3. Final Answer:

    Raises a NotFound exception -> Option B
  4. Quick Check:

    Missing key triggers NotFound error [OK]
Hint: Missing keys cause NotFound exceptions, not None [OK]
Common Mistakes:
  • Assuming it prints the key name anyway
  • Expecting None instead of error
  • Thinking it returns empty object
4. You try to rotate a key in Cloud KMS using the command:
gcloud kms keys rotate my-key --keyring=my-key-ring --location=us-central1
But you get an error. What is the most likely cause?
medium
A. The key is already rotated
B. The location flag is misspelled
C. The key ring does not exist
D. The key does not have rotation policy set

Solution

  1. Step 1: Understand prerequisites for key rotation

    The keyring must exist in the specified location before you can perform operations like rotating a key in it.
  2. Step 2: Analyze the error cause

    If the key ring does not exist, the command fails with a resource not found error. Manual rotation does not require a rotation policy, the location flag is correct, and keys can be rotated multiple times.
  3. Final Answer:

    The key ring does not exist -> Option C
  4. Quick Check:

    Keyring must exist for key rotation [OK]
Hint: Create keyring before rotating keys [OK]
Common Mistakes:
  • Thinking rotation policy is required
  • Believing key can't be rotated if already rotated
  • Assuming location flag typo
5. You want to ensure your encryption keys in Cloud KMS are rotated automatically every 30 days. Which configuration should you apply to the key to achieve this?
hard
A. Set the key ring's location to a region that supports auto-rotation
B. Manually run the rotate command every 30 days
C. Create a new key every 30 days and delete the old one
D. Set the key's rotation period to 30 days using the --rotation-period flag

Solution

  1. Step 1: Understand automatic rotation in Cloud KMS

    Automatic rotation is configured by setting the key's rotation period property.
  2. Step 2: Evaluate options

    Set the key's rotation period to 30 days using the --rotation-period flag correctly sets the rotation period. Manual rotation or creating new keys are manual processes. Location does not control rotation.
  3. Final Answer:

    Set the key's rotation period to 30 days using the --rotation-period flag -> Option D
  4. Quick Check:

    Rotation period config enables automatic key rotation [OK]
Hint: Use --rotation-period to automate key rotation [OK]
Common Mistakes:
  • Relying on manual rotation only
  • Confusing key ring location with rotation
  • Deleting and recreating keys unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard