AWS CDK AWS KMS Constructs (v1)

1.204.0 · maintenance · verified Thu Apr 16

The `aws-cdk-aws-kms` package provides AWS Cloud Development Kit (CDK) constructs for AWS Key Management Service (KMS). This package is part of AWS CDK v1, which is now in maintenance mode. New projects are strongly encouraged to use AWS CDK v2, where KMS constructs are bundled within the `aws-cdk-lib` package. The last published version for v1 is 1.204.0, with frequent updates during its active lifecycle.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a new KMS Key and an Alias for it using AWS CDK v1 constructs. It outputs the ARN of the created key and its alias.

from aws_cdk import core as cdk
from aws_cdk import aws_kms as kms

class MyKmsStack(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Create a new KMS Key
        key = kms.Key(self, "MyApplicationKey",
            description="My sample KMS key for an application",
            enable_key_rotation=True,
            removal_policy=cdk.RemovalPolicy.DESTROY # Caution: Destroys key on stack deletion
        )

        # Create an alias for the key
        kms.Alias(self, "MyApplicationKeyAlias",
            alias_name="alias/my-app-key",
            target_key=key
        )

        # Output the Key ARN
        cdk.CfnOutput(self, "KeyArn",
            value=key.key_arn,
            description="ARN of the created KMS Key"
        )

        # Output the Key Alias ARN
        cdk.CfnOutput(self, "KeyAliasArn",
            value=f"arn:{{self.partition}}:kms:{{self.region}}:{{self.account}}:alias/my-app-key",
            description="ARN of the KMS Key Alias"
        )

app = cdk.App()
MyKmsStack(app, "MyKmsV1Stack")
app.synth()

view raw JSON →