-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix(cloudwatch-actions): lambda permission use unique prefix id #34269
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
fix(cloudwatch-actions): lambda permission use unique prefix id #34269
Conversation
scorbiere
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution. I would recommend the following changes:
- Add an optional
LambdaActionPropsparameter in the constructor. This new props will include a booleanuseLongPermissionId(or a better name ;) ). - Use the new property in the construction of the
idPrefix. - after
if (permissionNode?.sourceArn !== alarm.alarmArn) {, add a test that will show a warning letting the user know about the propertyuseLongPermissionId, in casepermissionNodeis not undefined (which should case the next line.addPermissionto throw the exception)
Why not another feature flag? Because I think user may not want this behaviour to be at a global (or stack) level.
Why not fixing the actual logic? Because it will trigger the replacement of the existing permissions, and also, I am concerned about the risk of transient permission issues.
064e3b5 to
b035c21
Compare
|
Thanks @scorbiere for your instruction. I can see some similar implementation in the codebase but since this is the first time adding a warning, I must have misunderstand your point. Below pseudocode is what I'm understanding about your suggestion if (permissionNode?.sourceArn !== alarm.alarmArn) {
if (permissionNode !== undefined && !useUniquePermissionId) {
Annotations.of(scope).addWarningV2(permissionId, 'Please use useUniquePermissionId');
}
this.lambdaFunction.addPermission(permissionId, {...});
}Is my understanding correct ? |
Yes, I would add the following:
|
|
The problem with const annotations = Annotations.fromStack(stack); // throw construct id collision exception here
annotations.hasWarning('*', Match.stringLikeRegexp('message here')); // never reach hereBut we can with Let me know your thought on whether we should check |
|
I think it is important to keep on checking the
test('warning is shown when useUniquePermissionId is not set and collision would occur', () => {
// GIVEN
const stack = new Stack();
// Create a lambda function
const alarmLambda = new lambda.Function(stack, 'alarmLambda', {
runtime: lambda.Runtime.PYTHON_3_12,
functionName: 'alarmLambda',
code: lambda.Code.fromInline(`def handler(event, context): pass`),
handler: 'index.handler',
});
// Create an alarm
const alarm = new cloudwatch.Alarm(stack, 'TestAlarm', {
metric: new cloudwatch.Metric({
namespace: 'Test',
metricName: 'TestMetric',
}),
threshold: 1,
evaluationPeriods: 1,
});
// Save the original tryFindChild method
const originalTryFindChild = alarmLambda.permissionsNode.tryFindChild;
// Override tryFindChild to simulate a collision
alarmLambda.permissionsNode.tryFindChild = (id: string) => {
if (id === 'TestAlarmAlarmPermission') {
// Return a mock permission with a different sourceArn to trigger the warning
return {
sourceArn: 'arn:aws:cloudwatch:us-east-1:123456789012:alarm:DifferentAlarm',
} as unknown as lambda.CfnPermission;
}
return originalTryFindChild.call(alarmLambda.permissionsNode, id);
};
// Create a LambdaAction without useUniquePermissionId
const lambdaAction = new actions.LambdaAction(alarmLambda);
// WHEN
alarm.addAlarmAction(lambdaAction);
// THEN
const annotations = Annotations.fromStack(stack);
annotations.hasWarning('*', Match.stringLikeRegexp('Please use \'useUniquePermissionId\' to generate unique Lambda Permission Id'));
// Restore the original method
alarmLambda.permissionsNode.tryFindChild = originalTryFindChild;
});I haven't tested these options myself, if they are not working let's remove the test of the warning |
|
The try catch works great, i never think of it. Thanks for your suggestion @scorbiere |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
Comments on closed issues and PRs are hard for our team to see. |
Issue # (if applicable)
Closes #33958
Reason for this change
Duplicate id of alarm of multiple StepScalingPolicy
Description of changes
Update Lambda permission prefix to use
Names.uniqueIdConsidering this requires resource destruction, please let me know if i need a feature flag for the fix. As there was another feature flag in the exact same place.
Description of how you validated changes
Unit + Integ
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license