As @gshpychka says, you should probably use the grantInvoke
method instead. I wrote up an example here with an explanation, but basically all you need to do is as follows:
// the target function will be invoked by the source function
const targetFunction = new Function(this, "my-target-function", {
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
handler: "target.handler",
code: Code.fromAsset("./handlers/invocation"),
});
// the source function will be invoked by some event source
const sourceFunction = new Function(this, "my-source-function", {
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
handler: "source.handler",
code: Code.fromAsset("./handlers/invocation"),
environment: {
// the source function will need the exact function name
// as generated by CDK
TARGET_LAMBDA_NAME: targetFunction.functionName,
},
});
// we grant the invoke permission to the source function's principal,
// so that the source function will be authorized to invoke the target
// function
targetFunction.grantInvoke(sourceFunction.grantPrincipal);