Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Use EnableKeyRotation
with an Amazon SDK or CLI
The following code examples show how to use EnableKeyRotation
.
- CLI
-
- Amazon CLI
-
To enable automatic rotation of a KMS key
The following enable-key-rotation
example enables automatic rotation of a customer managed KMS key with a rotation period of 180 days. The KMS key will be rotated one year (approximate 365 days) from the date that this command completes and every year thereafter.
The --key-id
parameter identifies the KMS key. This example uses a key ARN value, but you can use either the key ID or the ARN of the KMS key.The --rotation-period-in-days
parameter specifies the number of days between each rotation date. Specify a value between 90 and 2560 days. If no value is specified, the default value is 365 days.
aws kms enable-key-rotation \
--key-id arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab
\
--rotation-period-in-days 180
This command produces no output. To verify that the KMS key is enabled, use the get-key-rotation-status
command.
For more information, see Rotating keys in the Amazon Key Management Service Developer Guide.
- Python
-
- SDK for Python (Boto3)
-
class KeyManager:
def __init__(self, kms_client):
self.kms_client = kms_client
self.created_keys = []
@classmethod
def from_client(cls) -> "KeyManager":
"""
Creates a KeyManager instance with a default KMS client.
:return: An instance of KeyManager initialized with the default KMS client.
"""
kms_client = boto3.client("kms")
return cls(kms_client)
def enable_key_rotation(self, key_id: str) -> None:
"""
Enables rotation for a key.
:param key_id: The ARN or ID of the key to enable rotation for.
"""
try:
self.kms_client.enable_key_rotation(KeyId=key_id)
except ClientError as err:
logging.error(
"Couldn't enable rotation for key '%s'. Here's why: %s",
key_id,
err.response["Error"]["Message"],
)
raise
For a complete list of Amazon SDK developer guides and code examples, see
Using this service with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.