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

cloudwatch-action: support alarm action lambda #28483

Closed
1 of 2 tasks
neilkuan opened this issue Dec 24, 2023 · 4 comments · Fixed by #28484 · May be fixed by stack-spot/app-handler-functions-template#2, stack-spot/eks-env-ts-template#2 or stack-spot/web-react-deploy#4
Closed
1 of 2 tasks
Labels
@aws-cdk/aws-cloudwatch-actions effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@neilkuan
Copy link
Contributor

Describe the feature

Cloudwatch alarm action support Lambda now.
Let's add new alarm action for lambda
https://aws.amazon.com/tw/about-aws/whats-new/2023/12/amazon-cloudwatch-alarms-lambda-change-action/

cfn-cloudwatch-alarm-alarmactions

API_PutMetricAlarm

image

Use Case

For cloudwatch alarm action support Lambda now.

Proposed Solution

Now I already used this way to setting AlarmAction to Lambda Function.

const alarm = new cdk.aws_cloudwatch.Alarm(this, 'Errors', {
      comparisonOperator: cdk.aws_cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
      threshold: 1,
      evaluationPeriods: 1,
      metric: alias.metricErrors({period: cdk.Duration.minutes(1)}),
      actionsEnabled: true,
      treatMissingData: cdk.aws_cloudwatch.TreatMissingData.NOT_BREACHING
    });
    const sns = new cdk.aws_sns.Topic(this, 'Topic', {
      displayName: 'alarmTopic',
    });
    alarmLambda.addPermission('addCloudWatch', {
      principal: new cdk.aws_iam.ServicePrincipal('lambda.alarms.cloudwatch.amazonaws.com'),
      sourceAccount: this.account,
      sourceArn: alarm.alarmArn,
      action: 'lambda:InvokeFunction',
    });
    alarm.addAlarmAction(new cdk.aws_cloudwatch_actions.SnsAction(sns));
    this.node.tryRemoveChild(sns.node.id);
    (alarm.node.defaultChild as cdk.aws_cloudwatch.CfnAlarm).addPropertyOverride(
      'AlarmActions',  [alarmLambda.functionArn]
    )

I will Create another class like LambdaAction(alarmLambda) to support it !

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.116.0

Environment details (OS name and version, etc.)

macOS 14.2.1

@neilkuan neilkuan added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Dec 24, 2023
@pahud
Copy link
Contributor

pahud commented Dec 24, 2023

This is awesome! Thank you @neilkuan

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Dec 24, 2023
@rslowinski
Copy link

rslowinski commented Jan 3, 2024

hey @neilkuan, sorry for off-topic but where did you find this "lambda.alarms.cloudwatch.amazonaws.com"? on aws docs i've only seen "events.amazonaws.com" and it wasn't working for my case where i needed cloudwatch alarm to trigger a lambda, it started working with "lambda.alarms.cloudwatch.amazonaws.com" which i found by accident here 😅

@neilkuan
Copy link
Contributor Author

neilkuan commented Jan 3, 2024

hey @neilkuan, sorry for off-topic but where did you find this "lambda.alarms.cloudwatch.amazonaws.com"? on aws docs i've only seen "events.amazonaws.com" and it wasn't working for my case where i needed cloudwatch alarm to trigger a lambda, it started working with "lambda.alarms.cloudwatch.amazonaws.com" which i found by accident here 😅

Here. 😎

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html#alarms-and-actions

@mergify mergify bot closed this as completed in #28484 Jan 11, 2024
mergify bot pushed a commit that referenced this issue Jan 11, 2024
feat(cloudwatch-action): support alarm lambda action

Closes #28483 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment