aws-lambda: Function.grantInvoke() not applying permissions for older versions of lambda function
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
when using Function.grantInvoke(), permission for function:Invoke is applied to the unqualified function arn but not to any of the older versions.
### Regression Issue
- [ ] Select this option if this issue appears to be a regression.
### Last Known Working CDK Version
_No response_
### Expected Behavior
Per the documentation here: https://docs.aws.amazon.com/cdk/api/v2/java/software/amazon/awscdk/services/lambda/package-summary.html#resource-based-policies-heading
and the GitHub PR here: https://github.com/aws/aws-cdk/pull/19318
I would expect that both the unqualified [FunctionArn] and [Function.Arn].* to have the permissions applied.
### Current Behavior
Invoke permission only seem to be applied to the unqualified function arn.
cdk synth produces the following snippet:
```
HelloWorldFunctionInvokeFcyXBRX02EWa52GlFECQiCzDt0fdRUDi4mo4foC5aU2283F0E1:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- HelloWorldFunctionB2AB6E79
- Arn
Principal: apigateway.amazonaws.com
```
which just enabled the unqualified ARN
### Reproduction Steps
if you have a stack containing such:
```
// Define the Lambda function resource
const myFunction = new lambda.Function(this, "HelloWorldFunction", {
runtime: lambda.Runtime.NODEJS_20_X, // Provide any supported Node.js runtime
handler: "index.handler",
code: lambda.Code.fromInline(`
exports.handler = async function(event) {
return {
statusCode: 200,
body: JSON.stringify('Hello World on ` + new Date().toLocaleString() + `!'),
};
};
`),
});
const myVersion = new lambda.Version(this, "HelloWorldFunction" + new Date().getTime()/1000,
{
lambda: myFunction,
removalPolicy: cdk.RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE
});
myFunction.grantInvoke(new iam.ServicePrincipal('apigateway.amazonaws.com'));
```
you will notice after the second (or subsequent) deploy that the permissions from the non-$LATEST versions are removed as part of the cdk deploy.
### Possible Solution
_No response_
### Additional Information/Context
_No response_
### CDK CLI Version
2.172.0
### Framework Version
bootstrap version 25
### Node.js Version
v23.3.0
### OS
macOS
### Language
Java
### Language Version
java 17
### Other information
_No response_
Contributor guide
Research direction
Start by reproducing the provided Function.grantInvoke(), Version, and cdk synth/deploy scenario, then inspect the implementation and tests for grantInvoke and versioned Lambda permissions. Done means permissions are applied to both the unqualified function ARN and older version ARNs and remain after subsequent deployments.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100