This topic shows how to automatically or manually rotate a key. For more information about key rotation in general, see Key rotation.
Rotating keys requires the Cloud KMS Admin role (roles/cloudkms.admin
).
When you rotate a key, data that was encrypted with previous key versions is not automatically re-encrypted. To learn more, see decrypt and re-encrypt. Rotating a key does not automatically disable or destroy any existing key versions.
Configuring automatic rotation
To configure automatic rotation when creating a new key:
Web UI
When you use the Google Cloud Console to create a key, if you don't specify your own rotation period and next rotation time, Cloud KMS will set the key's rotation period and next rotation time automatically.
To specify a different rotation period and starting time, when you are creating your key, but before you click the Create button:
Click the Rotation period dropdown and then select a value for the rotation period.
Click the date in the Starting on field and then select a date for the next rotation.
Command-line
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Cloud SDK.
gcloud kms keys create key \ --keyring key-ring \ --location location \ --purpose "encryption" \ --rotation-period rotation-period \ --next-rotation-time next-rotation-time
Replace key with a name for the key. Replace key-ring with
the name of the existing key ring where the key will be located. Replace
location with the Cloud KMS location for the key ring.
Replace rotation-period with an interval, such as 30d
to rotate the
key every 30 days. Replace next-rotation-time with a timestamp at
which to begin the first rotation, such as "1970-01-01T01:02:03"
.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
To configure automatic rotation on an existing key:
Web UI
Go to the Cryptographic Keys page in the Cloud Console.
Click the name of the key ring that contains the key for which to add a rotation schedule.
Click the key whose key version you want to add a rotation schedule.
In the header, click Edit Rotation Period.
In the prompt, choose new values for the Rotation period and Starting on fields.
In the prompt, click Save.
Command-line
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Cloud SDK.
gcloud kms keys update key-name \ --location location \ --keyring key-ring-name \ --rotation-period rotation-period \ --next-rotation-time next-rotation-time
Replace key with the name of the key to update. Replace
key-ring with the key ring where the key is located. Replace
location with the Cloud KMS location of the key ring.
Replace rotation-period with a
Duration.
The duration must be within the range of 1 day and 100 years. Replace
next-rotation-time with a
Timestamp
at which to begin the first rotation, or omit --next-rotation-time
to schedule
the first rotation for 7 days from the date when you run the command.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
Manually rotating a key
First, create a new key version:
Web UI
Go to the Cryptographic Keys page in the Cloud Console.
Click the name of the key ring that contains the key for which you will create a new key version.
Click the key for which you will create a new key version.
In the header, click Create version.
In the prompt, click Create version to confirm.
Command-line
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Cloud SDK.
gcloud kms keys versions create \ --key key \ --keyring key-ring \ --location location
Replace key with a name for the key. Replace key-ring with the name of the existing key ring where the key will be located. Replace location with the Cloud KMS location for the key ring.
Key versions are numbered sequentially.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
Next, set this new key version as the primary key version to make it the default for new operations.
If necessary, re-encrypt data that was encrypted using the previous key version.
Setting an existing version as the primary key version
To set different key version as the primary version for a key, update the key with the new primary version information. A key version must be enabled before you can configure it as the primary version.
Web UI
Go to the Cryptographic Keys page in the Cloud Console.
Click the name of the key ring that contains the key whose primary version you want to update.
Click the key whose primary version you want to update.
On the row corresponding to the key version you want to make primary, click View More
.Click Make primary version in menu.
In the confirmation prompt, click Make primary.
Command-line
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Cloud SDK.
gcloud kms keys update key \ --keyring key-ring \ --location location \ --primary-version key-version
Replace key with the name of the key. Replace key-ring with the name of the key ring where the key is located. Replace location with the Cloud KMS location for the key ring. Replace key-version with the new primary key version.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
When you change the primary key version, the change becomes consistent in around 40 minutes on average, and up to three hours. During this time, the prior primary version is used to encrypt data. For more information, see Cloud KMS resource consistency.
Disabling automatic rotation
To disable automatic rotation on a key, clear the rotation schedule of the key:
Web UI
Go to the Cryptographic Keys page in the Cloud Console.
Click the name of the key ring that contains the key for which to remove a rotation schedule.
Click the key whose key version you want to remove a rotation schedule.
In the header, click Edit Rotation Period.
In the prompt, click the Rotation period dropdown and select Never (manual rotation).
In the prompt, click Save.
Command-line
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Cloud SDK.
gcloud kms keys update key \ --keyring key-ring \ --location location \ --remove-rotation-schedule
Replace key with a name for the key. Replace key-ring with the name of the key ring where the key is located. Replace location with the Cloud KMS location for the key ring.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.