The CDK Construct Library for AWS::KMS
Deprecated. Its authors have deprecated it and no longer maintain it.
pip install aws-cdk-aws-kmsAWS Key Management Service Construct Library
---AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.
For more information on how to migrate, see the Migrating to AWS CDK v2 guide.
Define a KMS key:
kms.Key(self, "MyKey",
enable_key_rotation=True
)
Define a KMS key with waiting period:
Specifies the number of days in the waiting period before AWS KMS deletes a CMK that has been removed from a CloudFormation stack.
key = kms.Key(self, "MyKey",
pending_window=Duration.days(10)
)
Add a couple of aliases:
key = kms.Key(self, "MyKey")
key.add_alias("alias/foo")
key.add_alias("alias/bar")
Define a key with specific key spec and key usage:
Valid keySpec values depends on keyUsage value.
key = kms.Key(self, "MyKey",
key_spec=kms.KeySpec.ECC_SECG_P256K1, # Default to SYMMETRIC_DEFAULT
key_usage=kms.KeyUsage.SIGN_VERIFY
)
Sharing keys between stacks
To use a KMS key in a different stack in the same CDK application, pass the construct to the other stack:
#
# Stack that defines the key
#
class KeyStack(cdk.Stack):
def __init__(self, scope, id, *, description=None, env=None, stackName=None, tags=None, synthesizer=None, terminationProtection=None, analyticsReporting=None):
super().__init__(scope, id, description=description, env=env, stackName=stackName, tags=tags, synthesizer=synthesizer, terminationProtection=terminationProtection, analyticsReporting=analyticsReporting)
self.key = kms.Key(self, "MyKey", removal_policy=cdk.RemovalPolicy.DESTROY)
#
# Stack that uses the key
#
class UseStack(cdk.Stack):
def __init__(self, scope, id, *, key, description=None, env=None, stackName=None, tags=None, synthesizer=None, terminationProtection=None, analyticsReporting=None):
super().__init__(scope, id, key=key, description=description, env=env, stackName=stackName, tags=tags, synthesizer=synthesizer, terminationProtection=terminationProtection, analyticsReporting=analyticsReporting)
# Use the IKey object here.
kms.Alias(self, "Alias",
alias_name="alias/foo",
target_key=key
)
key_stack = KeyStack(app, "KeyStack")
UseStack(app, "UseStack", key=key_stack.key)