iam: cannot pass iam.ManagedPolicy or iam.Policy to lambda.Function.grantInvoke #32980
Description
Describe the bug
Following code fails:
const func = new lambda.Function(this, 'Function', { /* ... */ });
const managedPolicy = new iam.ManagedPolicy(this, 'ManagedPolicy');
func.grantInvoke(managedPolicy);
The error message is:
Cannot use a ManagedPolicy 'MyStack/Function' as the 'Principal' or 'NotPrincipal' in an IAM Policy
Regression Issue
- Select this option if this issue appears to be a regression.
Last Known Working CDK Version
No response
Expected Behavior
Grant lambda:InvokeFunction
to ManagedPolicy
or Policy
.
Current Behavior
grantInvoke()
throws an error described above.
Reproduction Steps
See the description above.
Possible Solution
grantInvoke()
calls grantee.grantPrincipal.policyFragment.conditions
to create a dedupe hash.
aws-cdk/packages/aws-cdk-lib/aws-lambda/lib/function-base.ts
Lines 435 to 442 in 2b2443d
But policyFragment
getter of ManagedPolicy
and Policy
throws an error. (see #22712)
It should return a dummy policy fragment like Group
.
aws-cdk/packages/aws-cdk-lib/aws-iam/lib/group.ts
Lines 82 to 84 in 2b2443d
Group
is blocked in PolicyStatement
aws-cdk/packages/aws-cdk-lib/aws-iam/lib/policy-statement.ts
Lines 240 to 244 in 2b2443d
Additional Information/Context
No response
CDK CLI Version
2.176.0
Framework Version
2.176.0
Node.js Version
22.13.0
OS
Ubuntu
Language
TypeScript
Language Version
No response
Other information
Related to #32795