-
Notifications
You must be signed in to change notification settings - Fork 4.1k
(@aws_cdk/custom_resources): from_sdk_calls() attaches incorrect permissions for Lambda InvokeCommand #34183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hey @nlocascio45, thank you for reporting this behavior. I was able to reproduce this issue using the following import * as cdk from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as cr from "aws-cdk-lib/custom-resources";
import * as iam from "aws-cdk-lib/aws-iam";
import { Construct } from "constructs";
export class LambdaInvokeStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create a simple Lambda function to invoke
const targetFunction = new lambda.Function(this, "TargetFunction", {
runtime: lambda.Runtime.NODEJS_18_X,
handler: "index.handler",
code: lambda.Code.fromInline(`
exports.handler = async () => {
return {
statusCode: 200,
body: 'Hello from Lambda!'
};
};
`),
});
const customResource = new cr.AwsCustomResource(this, "LambdaInvoker", {
onCreate: {
service: "Lambda",
action: "invoke",
parameters: {
FunctionName: targetFunction.functionName,
Payload: JSON.stringify({ message: "Hello" }),
},
physicalResourceId: cr.PhysicalResourceId.of("LambdaInvokerResource"),
},
});
}
} on synth this generated a policy statement as
This currently occurs as the aws-cdk/packages/aws-cdk-lib/custom-resources/lib/helpers-internal/sdk-info.ts Lines 30 to 51 in e43dc25
which currently only strip the As a workaround, you can utilize the import * as cdk from "aws-cdk-lib";
import * as iam from "aws-cdk-lib/aws-iam";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as cr from "aws-cdk-lib/custom-resources";
import { Construct } from "constructs";
export class LambdaInvokeStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create a simple Lambda function to invoke
const targetFunction = new lambda.Function(this, "TargetFunction", {
runtime: lambda.Runtime.NODEJS_18_X,
handler: "index.handler",
code: lambda.Code.fromInline(`
exports.handler = async () => {
return {
statusCode: 200,
body: 'Hello from Lambda!'
};
};
`),
});
// Create custom resource that will try to invoke the Lambda
const customResource = new cr.AwsCustomResource(this, "LambdaInvoker", {
onCreate: {
service: "Lambda",
action: "invoke",
parameters: {
FunctionName: targetFunction.functionName,
Payload: JSON.stringify({ message: "Hello" }),
},
physicalResourceId: cr.PhysicalResourceId.of("LambdaInvokerResource"),
},
policy: cr.AwsCustomResourcePolicy.fromStatements([
new iam.PolicyStatement({
actions: ["lambda:InvokeFunction"],
resources: [targetFunction.functionArn],
}),
]),
});
}
} |
Describe the bug
I created an
AwsCustomResource
and called the Lambda service with theInvokeCommand
action. When attaching a policy usingAwsCustomResourcePolicy.from_sdk_calls()
, it attached a policy with actionlambda:Invoke
, which is not a valid action, the correct action islambda:InvokeFunction
.Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
I expected
lambda:InvokeFunction
to be attached to my custom resource function permission.Current Behavior
lambda:Invoke
was attached to my custom resource function permission.Reproduction Steps
The following template should reproduce the issue once parameters are added:
Possible Solution
I took a quick look in the code and it appears that the logic to normalize an action name strips "Command" if necessary, but it does not account for the scenario where the command name is slightly different from the necessary permission, so this logic will likely need to be modified.
Additional Information/Context
The workaround is easy enough, we can just use
AwsCustomResourcePolicy.from_statements()
instead. However, I figured I would raise this in the event that this could impact other SDK calls.CDK CLI Version
2.1007.0
Framework Version
No response
Node.js Version
v22.11.0
OS
macOS Sequoia 15.4
Language
Python
Language Version
Python 3.11.9
Other information
No response
The text was updated successfully, but these errors were encountered: