(aws-kms): a grant that adds a key policy statement cannot be used as a dependency

Open Beginner friendly
#39,780 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@fukunaga-kengo is already working on this.

Since Nov 17, 2026.

  • #39782 by @fukunaga-kengo — open
  • #39814 by @fukunaga-kengo — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
aws, typescript
Domain
backend, cloud

Research direction

Start with Key.addToResourcePolicy() in packages/aws-cdk-lib/aws-kms/lib/key.ts and CfnKeyWithPolicy.addToResourcePolicy() in packages/aws-cdk-lib/aws-kms/lib/private/default-traits.ts; both currently return the policy document as a dependency. Check how the grant resolves its dependables, then run the reproductions in the issue and relevant KMS grant tests. Done means both L1 and L2 stacks synthesize when their grants are used as dependencies.

Written by the indexing model from the issue text.

Description

@aws-cdk/aws-kms bug p2
Describe the bug

When a KMS grant adds a statement to the key policy, using the returned Grant as a dependency (grant.applyBefore() or node.addDependency(grant)) fails at synth.

Both places that add a key policy statement return the PolicyDocument as policyDependable:

PolicyDocument is not a construct and does not implement the dependable trait. IDependable is an empty interface, so this compiles. Grant only resolves its dependables when something depends on it, which is why the same grant works when nothing does.

Grants that only change the principal's policy are not affected, for example an L2 Key granted to a role in the same account.

Regression Issue
  • Select this option if this issue appears to be a regression.
Last Known Working CDK Library Version

No response

Expected Behavior

The stack synthesizes, and the resource depends on the key, since the key's policy holds the statement.

Current Behavior
Error: ${Token[PolicyDocument.19]} does not implement IDependable. Use "Dependable.implement()" to implement
    at Dependable.of (node_modules/constructs/lib/dependency.js:72:19)
    at node_modules/aws-cdk-lib/aws-iam/lib/grant.js:1:6846
    at Array.flatMap (<anonymous>)
    at get dependencyRoots (node_modules/aws-cdk-lib/aws-iam/lib/grant.js:1:6809)
    at get dependencies (node_modules/constructs/lib/construct.js:370:67)
Reproduction Steps

Each of the two stacks fails on its own.

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

const app = new cdk.App();

// L2 key: a service principal is granted through the key policy
const stack1 = new cdk.Stack(app, 'Stack1');
const key1 = new kms.Key(stack1, 'Key');
const topic1 = new sns.Topic(stack1, 'Topic');
key1.grantDecrypt(new iam.ServicePrincipal('sns.amazonaws.com')).applyBefore(topic1);

// L1 key: KeyGrants always adds the statement to the key policy
const stack2 = new cdk.Stack(app, 'Stack2');
const role = new iam.Role(stack2, 'Role', {
  assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
});
const key2 = new kms.CfnKey(stack2, 'Key', {
  keyPolicy: {
    Version: '2012-10-17',
    Statement: [{
      Effect: 'Allow',
      Principal: { AWS: `arn:aws:iam::${stack2.account}:root` },
      Action: 'kms:*',
      Resource: '*',
    }],
  },
});
const topic2 = new sns.Topic(stack2, 'Topic');
topic2.node.addDependency(kms.KeyGrants.fromKey(key2).decrypt(role));

app.synth();
Possible Solution

The policy is part of the key resource, so the key itself can be the dependable: this in Key.addToResourcePolicy() and this.key in CfnKeyWithPolicy.addToResourcePolicy(). I tried both changes locally, and the two stacks above synthesize. I have not run the test suites yet.

I'd like to open a PR for this.

Additional Information/Context
  • #15790 reported this for Key.addToResourcePolicy() in 2021. It was closed for inactivity, and the code has not changed.
  • The CfnKey path was added in #36849.
  • I ran into this in #39772, which applies a KMS grant before another resource with grant.applyBefore().
Acknowledgements
  • I may be able to implement the fix for this bug
AWS CDK Library version (aws-cdk-lib)

2.277.0

AWS CDK CLI version

n/a

Node.js Version

v24.22.1

OS

Linux

Language

TypeScript

Language Version

No response

Other information

No response

Dominant language
TypeScript
Stars
13k
Forks
4.7k
Avg merge
2d 9h
Merged PRs (30d)
109

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from aws/aws-cdk

All issues in aws/aws-cdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.