禁用使用Lambda的CloudWatch警报操作

huangapple go评论116阅读模式
英文:

Disabling cloud watch alarm actions with lambda

问题

我正在尝试使用 Lambda 函数禁用 CloudWatch 告警操作,如以下文章提供的方式:"https://medium.com/geekculture/enable-or-disable-aws-alarms-at-given-intervals-d2f867aa9aa4"。

然而,在执行过程中,我遇到了超时问题。生成的 HTTP 请求没有返回任何错误。

代码如下:

  1. import logging
  2. import os
  3. import boto3
  4. logger = logging.getLogger()
  5. logger.setLevel(logging.DEBUG)
  6. def disable_alarms(alarm_names):
  7. try:
  8. logger.debug(f'禁用告警')
  9. disable_alarm = client.disable_alarm_actions(AlarmNames=alarm_names)
  10. return disable_alarm
  11. except Exception as e:
  12. logger.debug(f'禁用告警时出错: {e}')
  13. def enable_alarms(alarm_names):
  14. try:
  15. logger.info(f'启用告警: {alarm_names}')
  16. enable_alarm = client.enable_alarm_actions(AlarmNames=alarm_names)
  17. return enable_alarm
  18. except Exception as e:
  19. logger.debug(f'启用告警时出错: {alarm_names}, {e}')
  20. def lambda_handler(event, context):
  21. logger.info(f'## 事件: {event}')
  22. global client
  23. client = boto3.client('cloudwatch')
  24. global list_of_alarm_arns
  25. list_of_alarm_names = ['HealthAlarm']
  26. logger.info(f'发送到函数禁用')
  27. # 启用告警
  28. # enable_alarms(list_of_alarm_names)
  29. # 禁用告警
  30. disable_alarms(list_of_alarm_names)
  31. return {
  32. 'statusCode': 200,
  33. }

要禁用的是名为 "HealthAlarm" 的告警操作。

英文:

I am trying to disable the cloudwatch alarm actions using lambda function as provided by following article "https://medium.com/geekculture/enable-or-disable-aws-alarms-at-given-intervals-d2f867aa9aa4".

However i am facing timeouts during it's execution. The http request for generated does not return any error.

The code is as below:

  1. import logging
  2. import os
  3. import boto3
  4. logger = logging.getLogger()
  5. logger.setLevel(logging.DEBUG)
  6. def disable_alarms(alarm_names):
  7. try:
  8. logger.debug(f'Disabling alarms')
  9. disable_alarm = client.disable_alarm_actions(AlarmNames=alarm_names)
  10. return disable_alarm
  11. except Exception as e:
  12. logger.debug(f'Error while disabling alarms:, {e}')
  13. def enable_alarms(alarm_names):
  14. try:
  15. logger.info(f'Enabling alarms: {alarm_names}')
  16. enable_alarm = client.enable_alarm_actions(AlarmNames=alarm_names)
  17. return enable_alarm
  18. except Exception as e:
  19. logger.debug(f'Error while enable alarms: {alarm_names}, {e}')
  20. def lambda_handler(event, context):
  21. logger.info(f'## EVENT: {event}')
  22. global client
  23. client = boto3.client('cloudwatch')
  24. global list_of_alarm_arns
  25. list_of_alarm_names = ['HealthAlarm']
  26. logger.info(f'Sending To Function Disable')
  27. #enable_alarms(list_of_alarm_names)
  28. disable_alarms(list_of_alarm_names)
  29. return {
  30. 'statusCode': 200,
  31. }

The HealthAlarm existing actions is to be disabled.

答案1

得分: 1

问题出在与VPC连接的Lambda函数上,正如JohnRotenstein所提到的那样。一旦断开连接,函数就可以正常运行,而没有任何超时问题。

英文:

The issue was with the lambda function being connected to the VPC as @JohnRotenstein mentioned. On disconnecting the same, the function ran without any timeouts.

答案2

得分: -1

尝试检查Lambda执行角色:它需要CloudWatch权限来执行警报上的操作。

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/permissions-reference-cw.html

英文:

Try to check Lambda Execution role: it needs Cloudwatch permissions to performs actions on alarms.

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/permissions-reference-cw.html

huangapple
  • 本文由 发表于 2023年3月15日 21:36:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定