禁用使用Lambda的CloudWatch警报操作

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

Disabling cloud watch alarm actions with lambda

问题

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

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

代码如下:

import logging
import os
import boto3

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def disable_alarms(alarm_names):
  try:
      logger.debug(f'禁用告警')
      disable_alarm = client.disable_alarm_actions(AlarmNames=alarm_names)
      return disable_alarm
  except Exception as e:
    logger.debug(f'禁用告警时出错: {e}')

def enable_alarms(alarm_names):
  try:
      logger.info(f'启用告警: {alarm_names}')
      enable_alarm = client.enable_alarm_actions(AlarmNames=alarm_names)
      return enable_alarm
  except Exception as e:
    logger.debug(f'启用告警时出错: {alarm_names}, {e}')

def lambda_handler(event, context):
    logger.info(f'## 事件: {event}')
        
    global client
    client = boto3.client('cloudwatch')

    global list_of_alarm_arns
    list_of_alarm_names = ['HealthAlarm']
    
    logger.info(f'发送到函数禁用')
    
    # 启用告警
    # enable_alarms(list_of_alarm_names)
    
    # 禁用告警
    disable_alarms(list_of_alarm_names)

    return {
        'statusCode': 200,
    }

要禁用的是名为 "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:

import logging
import os
import boto3

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def disable_alarms(alarm_names):
  try:
      logger.debug(f'Disabling alarms')
      disable_alarm = client.disable_alarm_actions(AlarmNames=alarm_names)
      return disable_alarm
  except Exception as e:
    logger.debug(f'Error while disabling alarms:, {e}')

def enable_alarms(alarm_names):
  try:
      logger.info(f'Enabling alarms: {alarm_names}')
      enable_alarm = client.enable_alarm_actions(AlarmNames=alarm_names)
      return enable_alarm
  except Exception as e:
    logger.debug(f'Error while enable alarms: {alarm_names}, {e}')


def lambda_handler(event, context):

    logger.info(f'## EVENT: {event}')
        
    global client
    client = boto3.client('cloudwatch')

    global list_of_alarm_arns
    list_of_alarm_names = ['HealthAlarm']
    
    logger.info(f'Sending To Function Disable')
    
    #enable_alarms(list_of_alarm_names)
    disable_alarms(list_of_alarm_names)

    return {
        'statusCode': 200,
    }  

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:

确定