(aws-kms): a grant that adds a key policy statement cannot be used as a dependency
Maintainers usually reply within 1 day
@fukunaga-kengo is already working on this.
Since Nov 17, 2026.
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
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
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:
Key.addToResourcePolicy(), used by the L2KeyCfnKeyWithPolicy.addToResourcePolicy(), used byKeyGrants.fromKey()for an L1CfnKey
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
CfnKeypath 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
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from aws/aws-cdk
-
(kinesisanalytics-flink): add support for Flink 2.3Possibly taken @NestDream claimed this today. Open@aws-cdk/aws-kinesisanalytics-flink needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
(rds): Add SqlServerEngineVersion constants for 17.0.5035.5, 16.0.4305.5, 15.0.4500.2, 14.0.3560.2Possibly taken @MarcelNullVier claimed this 7 days ago. Open@aws-cdk/aws-rds feature-request p2
Difficulty 1/5 1-3 hours Newbie friendliness 78/100
aws/aws-cdk#39776 · 1 comment · 4 reactions ·
Maintainers usually reply within 1 day
-
(aws-iam, elbv2-targets, apigatewayv2-authorizers): constructor docs name parameters that don't existPossibly taken @farisbasha claimed this 7 days ago. Open@aws-cdk/aws-apigatewayv2-authorizers @aws-cdk/aws-elasticloadbalancingv2-targets @aws-cdk/aws-iam documentation p2
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
aws/aws-cdk#39775 · 1 comment ·
Maintainers usually reply within 1 day
-
(bedrockagentcore): ApiKeyCredentialLocation.header cannot omit the credential prefixPossibly taken @wasim-builds claimed this 12 days ago. Openbug effort/medium p2
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
aws/aws-cdk#39693 · 2 comments ·
Maintainers usually reply within 1 day
-
(bedrockagentcore): Policy fails with an engine from PolicyEngine.fromPolicyEngineAttributesPossibly taken @wasim-builds claimed this 12 days ago. Open@aws-cdk/aws-iam bug effort/medium p2
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
aws/aws-cdk#39690 · 1 comment ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
lidofinance/validator-ejector#233 ·
Maintainers usually reply within 1 day
-
desktop: app-version.v1 in drafts.sqlite is never refreshed — reports 1.19.4 while running 2.3.2Openarea/desktop platform/windows type/feature version/v2
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
anomalyco/opencode#66079 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
Maintainers usually reply within 1 day
-
ai-driven-qa bug claude Major
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
linagora/twake-mail-admin#742 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
OpenSlides/openslides-client#6728 ·
Maintainers usually reply within 1 day