aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

(aws-kms): Key Alias does not inherit the key's removal policy

Open kishiel opened this issue 5 months ago • 1 comments

Describe the bug

Aliases defined on a KMS key do not inherit the retention policy of the key. KMS Key's default is to RETAIN. This occurs both in the interface alias?, and in the addAlias function:

  /**
   * Defines a new alias for the key.
   */
  public addAlias(aliasName: string): Alias {
    const aliasId = this.aliases.length > 0 ? `Alias${aliasName}` : 'Alias';

    const alias = new Alias(this, aliasId, { aliasName, targetKey: this });
    this.aliases.push(alias);

    return alias;
  }

The consequence for is that although a KMS key can be protected from deletion the alias will not. The deletion of the alias causes trust policies that use the alias for permission scoping to fail. For my service this resulted in an outage.

Regression Issue

  • [ ] Select this option if this issue appears to be a regression.

Last Known Working CDK Library Version

No response

Expected Behavior

I expected the KMS key and its properties to be retained

Current Behavior

The key has the removal policy applied here, but that same setting is not applied to alias. The stack events demonstrate this disconnect:

Endpointuswest2KmsKeyD3380D41 | DELETE_SKIPPED
Endpointuswest2KmsKeyAliasD3520F07 | DELETE_IN_PROGRESS
Endpointuswest2KmsKeyAliasD3520F07 | DELETE_COMPLETE

Reproduction Steps

Define a key, give it an alias, and then delete the stack.

new kms.Key(scope, `some-id`, {
        alias: 'very-important-name',
      })

Possible Solution

The interface option for alias? should inherit the removal policy of the key as should the addAlias function.

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

2.259.0

AWS CDK CLI version

2.1019.1

Node.js Version

20.0

OS

5.10.258-259.1041.amzn2int.x86_64

Language

TypeScript

Language Version

No response

Other information

No response

kishiel avatar Jun 30 '26 16:06 kishiel

Hi @kishiel,

This is a good catch — confirmed the bug is reproducible. The key gets Retain but the alias silently defaults to Delete.

Reproduction
import * as cdk from 'aws-cdk-lib';
import * as kms from 'aws-cdk-lib/aws-kms';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'TestStack');

new kms.Key(stack, 'MyKey', {
  alias: 'my-important-alias',
  removalPolicy: cdk.RemovalPolicy.RETAIN,
});

Synthesized output:

MyKey6AB29FA6 (AWS::KMS::Key):
  DeletionPolicy: Retain
  UpdateReplacePolicy: Retain

MyKeyAlias1B45D9DA (AWS::KMS::Alias):
  DeletionPolicy: NOT SET       ← defaults to Delete
  UpdateReplacePolicy: NOT SET  ← defaults to Delete

Thanks for your PR #38222 — we welcome the contribution. A maintainer will review the rationale and design (e.g., whether inheritance should be the unconditional default or opt-in). The main thing to be mindful of is backward compatibility — this changes behavior for existing stacks that currently delete aliases on teardown, so please make sure the approach accounts for that (feature flag, explicit opt-in, or a clear justification for why the current behavior is a defect rather than a feature). Labeling p2.

pahud avatar Jul 06 '26 15:07 pahud