Skip to content
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): LambdaAction fails if added to multiple action types #29515

18 changes: 11 additions & 7 deletions packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ export class LambdaAction implements cloudwatch.IAlarmAction {
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricAlarm.html
*/
bind(scope: Construct, alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig {
const idPrefix = FeatureFlags.of(scope).isEnabled(LAMBDA_PERMISSION_LOGICAL_ID_FOR_LAMBDA_ACTION) ? alarm.node.id : '';
this.lambdaFunction.addPermission(`${idPrefix}AlarmPermission`, {
sourceAccount: Stack.of(scope).account,
action: 'lambda:InvokeFunction',
sourceArn: alarm.alarmArn,
principal: new iam.ServicePrincipal('lambda.alarms.cloudwatch.amazonaws.com'),
});
const flag = FeatureFlags.of(scope).isEnabled(LAMBDA_PERMISSION_LOGICAL_ID_FOR_LAMBDA_ACTION);
const idPrefix = flag ? alarm.node.id : '';
const permissionId = `${idPrefix}AlarmPermission`;
if (!this.lambdaFunction.permissionsNode.tryFindChild(permissionId) || !flag) {
morrijm4 marked this conversation as resolved.
Show resolved Hide resolved
this.lambdaFunction.addPermission(permissionId, {
sourceAccount: Stack.of(scope).account,
action: 'lambda:InvokeFunction',
sourceArn: alarm.alarmArn,
principal: new iam.ServicePrincipal('lambda.alarms.cloudwatch.amazonaws.com'),
});
}

return {
alarmActionArn: this.lambdaFunction.functionArn,
Expand Down
morrijm4 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ def handler(event, context):
handler: 'index.handler',
});
alarm1.addAlarmAction(new actions.LambdaAction(alarmLambda));
alarm1.addOkAction(new actions.LambdaAction(alarmLambda));
alarm1.addInsufficientDataAction(new actions.LambdaAction(alarmLambda));

alarm2.addAlarmAction(new actions.LambdaAction(alarmLambda));
alarm2.addOkAction(new actions.LambdaAction(alarmLambda));
alarm2.addInsufficientDataAction(new actions.LambdaAction(alarmLambda));

// THEN
Template.fromStack(stack).resourceCountIs('AWS::CloudWatch::Alarm', 2);
Expand Down Expand Up @@ -175,6 +180,9 @@ def handler(event, context):
alarm1.addAlarmAction(new actions.LambdaAction(alarmLambda));

// THEN
expect(() => {
alarm1.addOkAction(new actions.LambdaAction(alarmLambda));
}).toThrow(/There is already a Construct with name 'AlarmPermission' in Function \[alarmLambda\]/);
expect(() => {
alarm2.addAlarmAction(new actions.LambdaAction(alarmLambda));
}).toThrow(/There is already a Construct with name 'AlarmPermission' in Function \[alarmLambda\]/);
Expand Down
Loading