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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,36 @@
}
],
"EvaluationPeriods": 1,
"InsufficientDataActions": [
{
"Ref": "alarmLambdaFeatureCurrentVersionCF39751979501d2f67eaf906b2ef0c378303873b"
},
{
"Ref": "alarmLambdaFeatureAliasaliasName16F91D34"
},
{
"Fn::GetAtt": [
"alarmLambdaFeatureD560800F",
"Arn"
]
}
],
"MetricName": "Errors",
"Namespace": "AWS/Lambda",
"OKActions": [
{
"Ref": "alarmLambdaFeatureCurrentVersionCF39751979501d2f67eaf906b2ef0c378303873b"
},
{
"Ref": "alarmLambdaFeatureAliasaliasName16F91D34"
},
{
"Fn::GetAtt": [
"alarmLambdaFeatureD560800F",
"Arn"
]
}
],
"Period": 60,
"Statistic": "Sum",
"Threshold": 1,
Expand Down Expand Up @@ -309,8 +337,36 @@
}
],
"EvaluationPeriods": 1,
"InsufficientDataActions": [
{
"Ref": "alarmLambdaFeatureCurrentVersionCF39751979501d2f67eaf906b2ef0c378303873b"
},
{
"Ref": "alarmLambdaFeatureAliasaliasName16F91D34"
},
{
"Fn::GetAtt": [
"alarmLambdaFeatureD560800F",
"Arn"
]
}
],
"MetricName": "Errors",
"Namespace": "AWS/Lambda",
"OKActions": [
{
"Ref": "alarmLambdaFeatureCurrentVersionCF39751979501d2f67eaf906b2ef0c378303873b"
},
{
"Ref": "alarmLambdaFeatureAliasaliasName16F91D34"
},
{
"Fn::GetAtt": [
"alarmLambdaFeatureD560800F",
"Arn"
]
}
],
"Period": 60,
"Statistic": "Sum",
"Threshold": 1,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,36 @@
}
],
"EvaluationPeriods": 1,
"InsufficientDataActions": [
{
"Ref": "alarmLambdaFeatureCurrentVersionCF39751979501d2f67eaf906b2ef0c378303873b"
},
{
"Ref": "alarmLambdaFeatureAliasaliasName16F91D34"
},
{
"Fn::GetAtt": [
"alarmLambdaFeatureD560800F",
"Arn"
]
}
],
"MetricName": "Errors",
"Namespace": "AWS/Lambda",
"OKActions": [
{
"Ref": "alarmLambdaFeatureCurrentVersionCF39751979501d2f67eaf906b2ef0c378303873b"
},
{
"Ref": "alarmLambdaFeatureAliasaliasName16F91D34"
},
{
"Fn::GetAtt": [
"alarmLambdaFeatureD560800F",
"Arn"
]
}
],
"Period": 60,
"Statistic": "Sum",
"Threshold": 1,
Expand Down Expand Up @@ -309,8 +337,36 @@
}
],
"EvaluationPeriods": 1,
"InsufficientDataActions": [
{
"Ref": "alarmLambdaFeatureCurrentVersionCF39751979501d2f67eaf906b2ef0c378303873b"
},
{
"Ref": "alarmLambdaFeatureAliasaliasName16F91D34"
},
{
"Fn::GetAtt": [
"alarmLambdaFeatureD560800F",
"Arn"
]
}
],
"MetricName": "Errors",
"Namespace": "AWS/Lambda",
"OKActions": [
{
"Ref": "alarmLambdaFeatureCurrentVersionCF39751979501d2f67eaf906b2ef0c378303873b"
},
{
"Ref": "alarmLambdaFeatureAliasaliasName16F91D34"
},
{
"Fn::GetAtt": [
"alarmLambdaFeatureD560800F",
"Arn"
]
}
],
"Period": 60,
"Statistic": "Sum",
"Threshold": 1,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class LambdaAlarmActionIntegrationTestStack extends Stack {
alarm.addAlarmAction(new cloudwatchActions.LambdaAction(version));
alarm.addAlarmAction(new cloudwatchActions.LambdaAction(aliasName));
alarm.addAlarmAction(new cloudwatchActions.LambdaAction(alarmLambda));
alarm.addOkAction(new cloudwatchActions.LambdaAction(version));
alarm.addOkAction(new cloudwatchActions.LambdaAction(aliasName));
alarm.addOkAction(new cloudwatchActions.LambdaAction(alarmLambda));
alarm.addInsufficientDataAction(new cloudwatchActions.LambdaAction(version));
alarm.addInsufficientDataAction(new cloudwatchActions.LambdaAction(aliasName));
alarm.addInsufficientDataAction(new cloudwatchActions.LambdaAction(alarmLambda));

if (isFeature) {
const alarm2 = new cloudwatch.Alarm(this, `Alarm${lambdaIdSuffix}`, {
Expand All @@ -53,6 +59,12 @@ class LambdaAlarmActionIntegrationTestStack extends Stack {
alarm2.addAlarmAction(new cloudwatchActions.LambdaAction(version));
alarm2.addAlarmAction(new cloudwatchActions.LambdaAction(aliasName));
alarm2.addAlarmAction(new cloudwatchActions.LambdaAction(alarmLambda));
alarm2.addOkAction(new cloudwatchActions.LambdaAction(version));
alarm2.addOkAction(new cloudwatchActions.LambdaAction(aliasName));
alarm2.addOkAction(new cloudwatchActions.LambdaAction(alarmLambda));
alarm2.addInsufficientDataAction(new cloudwatchActions.LambdaAction(version));
alarm2.addInsufficientDataAction(new cloudwatchActions.LambdaAction(aliasName));
alarm2.addInsufficientDataAction(new cloudwatchActions.LambdaAction(alarmLambda));
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ export class LambdaAction implements cloudwatch.IAlarmAction {
*/
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 permissionId = `${idPrefix}AlarmPermission`;
const permissionNode = this.lambdaFunction.permissionsNode.tryFindChild(permissionId) as lambda.CfnPermission | undefined;

if (permissionNode?.sourceArn !== alarm.alarmArn) {
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 @@ -173,6 +178,8 @@ 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));
morrijm4 marked this conversation as resolved.
Show resolved Hide resolved

// THEN
expect(() => {
Expand Down
Loading